eclipse背后的魔力生成了可执行的jar文件 [英] The magic behind the eclipse generated executable jar file
问题描述
我可以用Eclipse制作一个可执行的jar文件。假设它叫做 ast.jar
,而Main类是ASTExplorer。我可以验证这适用于 java -jar ast.jar
。
I can make a executable jar file with Eclipse. Let's say it is called ast.jar
, and the Main class is ASTExplorer. I can verify that this works with java -jar ast.jar
.
然后,我将jar文件解压缩到一个单独的目录中,并验证ASTExplorer是否位于astexplorer目录中。但是当我执行这个命令 java -cp时。 astexplorer.ASTExplorer
我收到此错误。
Then, I unzip the jar file in a separate directory and verify that the ASTExplorer is in astexplorer directory. But when I executed this command java -cp . astexplorer.ASTExplorer
I get this error.
java -cp . astexplorer/ASTExplorer
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Composite
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
问题是jar文件中没有org / eclipse / swt目录。
The thing is that there is no org/eclipse/swt directory in the jar file.
可执行jar文件背后的魔力是什么?解压缩时为什么不运行?
What's the magic behind the executable jar file? Why doesn't it run when unzipped?
推荐答案
当Eclipse打包一个可运行的jar时,它包含作为嵌套jar文件的依赖项,还包括一个特殊的类加载器,它可以理解如何在嵌套的jar中查找类。它仅适用于嵌套的jar,当你提取所有东西时,它阻止它工作。
When Eclipse packages a runnable jar, it includes the dependencies as nested jar files, and also includes a special classloader that understands how to find classes in nested jars. It works only with nested jars and when you extract everything you prevent it from working.
我看过Eclipse生产的可运行jar,这就是我认为你需要做的事情:
I've looked in an Eclipse-produced runnable jar, and here's what I think you'd need to do:
- 将所有内容解压缩到目录。依赖关系jar在顶级目录中提取
-
删除org / eclipse / jdt / internal目录 -
从顶级目录运行你的主类
- Extract everything to a directory. The dependency jars are extracted at the top level directory
Delete the org/eclipse/jdt/internal directoryRun your main class from the top level directory with
java -cp .; dep-jar; dep-jar; ...你的.main.class
我用可运行的罐子试过这个我碰巧躺着周围,它的工作原理。第2步并非绝对必要。
I tried this with a runnable jar I happen to have lying around and it works. Step 2 isn't strictly necessary.
这篇关于eclipse背后的魔力生成了可执行的jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!