You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven打包时无法将org.eclipse.swt纳入项目Jar的问题求助

SWT Maven项目打包依赖缺失问题

问题描述

基于SWT构建的Maven UI项目,打包时无法将SWT依赖纳入最终Jar包,运行生成的Jar时抛出异常:

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Layout
at cc.derick.YouTubeMemberEmojiDownloader.App.main(App.java:11)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Layout
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
... 1 more

最初引入SWT依赖时,还出现POM无效错误:

[WARNING] The POM for org.eclipse.platform:org.eclipse.swt:jar:3.127.0 is invalid, transitive dependencies (if any) will not be available: 1 problem was encountered while building the effective model for org.eclipse.platform:org.eclipse.swt:3.127.0
[ERROR] 'dependencies.dependency.artifactId' for org.eclipse.platform:org.eclipse.swt.${osgi.platform}:jar with value 'org.eclipse.swt.${osgi.platform}' does not match a valid id pattern. @

原始依赖配置:

<dependency>
    <groupId>org.eclipse.platform</groupId>
    <artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
    <version>3.127.0</version>
</dependency>

尝试手动将SWT Jar放入项目lib目录,使用system scope依赖:

<dependency>
    <groupId>org.eclipse.platform</groupId>
    <artifactId>org.eclipse.swt</artifactId>
    <version>1.0.0</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/swt.jar</systemPath>
</dependency>

已配置maven-assembly-plugin打包可执行Jar:

<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.4.2</version>
  <configuration>
    <archive>
        <manifest>
            <mainClass>cc.demo.SwtDemo.App</mainClass>
        </manifest>
    </archive>
   </configuration>
</plugin>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>3.7.0</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
        <mainClass>cc.demo.SwtDemo.App</mainClass>
    </archive>
  </configuration>
</plugin>

无论是使用Eclipse内置Maven工具还是终端执行mvn clean package,结果一致。

解决方案

1. 修复SWT依赖引入方式(推荐)

放弃手动导入Jar和system scope,直接使用Maven中央仓库的正确SWT依赖,通过属性自动匹配当前系统:

<properties>
    <swt.version>3.127.0</swt.version>
    <!-- 根据系统映射对应SWT artifactId -->
    <swt.platform>${os.name}-${os.arch}</swt.platform>
    <swt.platform.windows-x86_64>win32.win32.x86_64</swt.platform.windows-x86_64>
    <swt.platform.linux-x86_64>gtk.linux.x86_64</swt.platform.linux-x86_64>
    <swt.platform.macosx-x86_64>cocoa.macosx.x86_64</swt.platform.macosx-x86_64>
    <swt.platform.macosx-aarch64>cocoa.macosx.aarch64</swt.platform.macosx-aarch64>
</properties>

<dependencies>
    <dependency>
        <groupId>org.eclipse.platform</groupId>
        <artifactId>org.eclipse.swt.${swt.platform.${swt.platform}}</artifactId>
        <version>${swt.version}</version>
    </dependency>
</dependencies>

也可以直接指定对应系统的依赖(无需自动匹配):

<dependency>
    <groupId>org.eclipse.platform</groupId>
    <artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
    <version>3.127.0</version>
</dependency>

2. 让maven-assembly-plugin包含system scope依赖

如果坚持使用system scope,修改assembly插件配置,强制包含system scope依赖:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>3.7.0</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
        <mainClass>cc.demo.SwtDemo.App</mainClass>
    </archive>
    <!-- 新增:强制包含system scope依赖 -->
    <includeScope>system</includeScope>
  </configuration>
</plugin>

3. 替换为maven-shade-plugin(更可靠的打包方式)

maven-assembly-plugin对部分依赖场景支持有限,改用maven-shade-plugin打包:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.5.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>cc.demo.SwtDemo.App</mainClass>
                    </transformer>
                </transformers>
                <!-- 若使用system scope,需明确包含SWT依赖 -->
                <artifactSet>
                    <includes>
                        <include>org.eclipse.platform:org.eclipse.swt</include>
                    </includes>
                </artifactSet>
            </configuration>
        </execution>
    </executions>
</plugin>

验证步骤

  1. 执行mvn clean package
  2. 运行生成的可执行Jar:java -jar target/[项目名称]-jar-with-dependencies.jar(或shade插件生成的Jar)

内容的提问来源于stack exchange,提问作者user23381600

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 10:29:56