使用maven-dependency-plugin运行带有eclipse swt依赖项的java jar(Running a java jar with eclipse swt dependencies with maven-dependency-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不存在一样。
我错过了什么?
I'm making an eclipse plugin that I also want to make it work command line. As normal the usual problem is encountering the eclipse dependencies in command-line:
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
Now I know there is an eclipse FAQ for this problem however here is a bit different. I'm using Maven for build with the maven-dependency-plugin to add jars I need from other project to this jar as dependencies:
<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>
The dependencies in my Manifest file looks like this:
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
As you can see I added an swt.jar to a lib inside my project and am referencing it there as to have all my dependencies inside my jar.
Still I get the above error when I try to run it command line as if the swt.jar isn't there.
What am I missing?
原文:https://stackoverflow.com/questions/39706557
最满意答案
打包课程时需要考虑几个因素。 首先,课程在哪里使用? 第二,它们应该如何包装? 关于第一个,您说您的变压器,比较器和相关的杂项类仅供您的服务类使用。 如果是这样,那么将它们放入您的服务包中是有意义的。
关于第二个问题,您应该考虑是否需要在本项目或其他项目的其他地方使用它们。 如果是这样,那么您可能希望将它们打包到更高的独立级别。 这使得将它们打包到自己的库中以便在其他地方使用更容易。
如果您使用的是现代IDE,我不会想太多,因为随着需求的变化,稍后重构您的代码是微不足道的。 例如,在Eclipse中添加和优化import语句,或者将类从一个包移动到另一个包是一些按键。
There are a couple of considerations to be made when packaging your classes. First, where are the classes used? And, second, how should they be packaged? Regarding the first, you say that your transformers, comparators and associated miscellaneous classes are only used by your service classes. If so, then it makes sense to put them inside your service package.
Regarding the second question, you should think about whether they would ever need to be used elsewhere in this project or another. If so, then you might want to package them at a higher independent level. That makes it easier to package them up into their own library for use elsewhere.
If your using a modern IDE, I wouldn't think too hard about it since it is trivial to refactor your code later as needs change. For example, in Eclipse adding and optimizing import statements, or moving classes from one package to another is a few keystrokes.
相关问答
更多-
是不是有一款什么手机软件,可以很自然的拉长腿部?不是美图秀秀,有知道的不?[2022-10-20]
spring -
春天的英语怎么读?[2022-05-21]
spring -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
在Spring中命名线程(Naming threads in Spring)[2022-03-21]
Spring中的TaskExecutor是一个直接从Java的Executor扩展的功能接口。 根据文件: 执行提交的Runnable任务的对象。 该界面提供了一种将任务提交与每个任务如何运行的机制解耦的方式,包括线程使用 ,调度等细节 。 这意味着它不可能(也不应该被要求)为你的线程命名,因为你不负责启动和管理它。 也就是说,出于调试的目的,如果你想提供一些名字,你应该通过设置threadNamePrefix和/或threadGroupName属性来实现线程池本身。 TaskExecutor in Spr ... -
惯例总是如此 domain.domainname.package[.subpackage].Class (其中subpackage是可选的)。 目标是为每个项目设置一个唯一的命名空间,以避免名称空间冲突,因此缩短它并不是一个好主意。 回答你的问题:如果第二个foo是你项目的名字, com.foo.foo是一个正确的名字。 如果此项目现在包含类Bar ,则路径为com.foo.foo.Bar 。 the convention is always domain.domainname.package[.subp ...
-
查看spring remoting: http : //static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html使用各种协议可以轻松地远程暴露您的spring bean。 Check out spring remoting: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/rem ...
-
有没有办法让我告诉Spring不要使用bean的方法名称? 简答: 不 。 我认为插入一个自定义的BeanNameGenerator可以解决你的问题,但显然没有使用BeanNameGenerator来生成在@Configuration类中定义的bean的bean名称(或者至少不在spring 3.2.x和spring 4.0.x中) 。 当您查看ConfigurationClassBeanDefinitionReader (第184行)时 ,您会注意到确定bean名称是非常硬编码的; 如果可用,则使用@Be ...
-
打包课程时需要考虑几个因素。 首先,课程在哪里使用? 第二,它们应该如何包装? 关于第一个,您说您的变压器,比较器和相关的杂项类仅供您的服务类使用。 如果是这样,那么将它们放入您的服务包中是有意义的。 关于第二个问题,您应该考虑是否需要在本项目或其他项目的其他地方使用它们。 如果是这样,那么您可能希望将它们打包到更高的独立级别。 这使得将它们打包到自己的库中以便在其他地方使用更容易。 如果您使用的是现代IDE,我不会想太多,因为随着需求的变化,稍后重构您的代码是微不足道的。 例如,在Eclipse中添加和优 ...
-
将基于Spring的java模块集成到独立的Java应用程序中(Integrate Spring-based java module into a standalone java application)[2022-10-28]
好的,我必须直接使用ApplicationContext ,这样我就可以在我的应用程序中管理Spring框架。 public class MyApplication { private static final Logger LOGGER = LoggerFactory.getLogger(MyApplication.class); public static void main(String[] args) { MyAuthenticationProvider authProvider; ... -
属性文件在类名周围有"" 。 删除它,开始工作。 The property file has "" around the class name. removed it, started working.