使用maven-dependency-plugin运行带有eclipse swt依赖项的java jar

时间:2016-09-26 15:18:20

标签: java eclipse maven jar eclipse-plugin

我制作了一个eclipse插件,我也想让它成为命令行。正常情况下,通常的问题是在命令行中遇到eclipse依赖项:

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Layout
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:56)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Layout
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 3 more

现在我知道这个问题有一个日食常见问题解答,但这里有点不同。我使用maven进行构建,使用maven-dependency-plugin将我需要的jar从其他项目添加到此jar作为依赖项:

<build>
    <plugins>
        <plugin>
            <!-- Copy non-Eclipse plugins to target/dependency so that may be referenced 
                for runtime use. -->
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <!-- seems the line below is needed otherwise every second run the 
                            copy-dependencies fails , link to bug : https://bugs.eclipse.org/bugs/show_bug.cgi?id=393978 -->
                        <includeScope>runtime</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

我的Manifest文件中的依赖项如下所示:

Rsrc-Class-Path: ./ 
 lib/jcommon-1.0.23.jar
 lib/jfreechart-1.0.19.jar
 lib/jfreechart-1.0.19-swt.jar
 lib/jfreechart-1.0.19-experimental.jar
 lib/swt.jar
 target/dependency/my.first.package.jar
 target/dependency/my.second.package.jar
Class-Path: .
Rsrc-Main-Class: my.package.Main
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

Bundle-ClassPath: .,
 lib/jcommon-1.0.23.jar,
 lib/jfreechart-1.0.19-experimental.jar,
 lib/jfreechart-1.0.19-swt.jar,
 lib/jfreechart-1.0.19.jar,
 lib/swt.jar

Require-Bundle: org.eclipse.core.runtime,
 org.eclipse.ui,
 my.first.package;visibility:=reexport,
 my.second.package;visibility:=reexport,
 ca.odell.glazedlists,
 org.eclipse.nebula.widgets.nattable.core,
 org.eclipse.core.resources,
 org.eclipse.swt

正如您所看到的,我在项目中的lib中添加了一个swt.jar,并在那里引用它,以便在我的jar中包含所有依赖项。

当我尝试运行命令行时,我仍然遇到上述错误,就像swt.jar不在那里一样。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

如果你想使Eclipse插件命令行友好,最好的事情是让插件还定义一个Eclipse应用程序,然后你可以从命令行调用它:https://wiki.eclipse.org/FAQ_How_do_I_create_an_application%3F