简体   繁体   English

可运行的jar找不到库

[英]Runnable jar not finding libraries

I have a project which has some dependent jars. 我有一个有一些依赖罐子的项目。 I've added the dependent jars to project and then added them to build path. 我已经将依赖的jar添加到项目中,然后将其添加到构建路径中。 When I run it from eclipse, it runs fine. 当我从Eclipse运行它时,它运行良好。

Now I want to make this into a runnable jar. 现在,我要将其放入可运行的jar中。 Once I export it as Runnable jar along with option "Package required libraries into generated Jar". 一旦将其与“将所需的库打包到生成的Jar中”选项一起导出为Runnable jar。

When I run the jar, It throws me Cannot find symbols error. 当我运行jar时,它引发我Cannot find symbols错误。 These symbols are there in the dependent jars and I don't know why it's happening. 这些符号位于相关的jar中,我不知道为什么会这样。

Can anyone help me out ? 谁能帮我吗 ?

You should use the option Extract required libraries into generated JAR rather than Package required libraries into generated Jar when you are exporting the project to a Runnable JAR file . 将项目导出到可运行的JAR文件时,应该使用选项将所需的库提取到生成的JAR中,而不是将所需的库打包到生成的Jar中

The difference is that for packaging the required libraries it will take the JAR files and put them inside your JAR file. 不同之处在于,对于打包所需的库,它将获取JAR文件并将其放入您的JAR文件中。 So the JAR file have other JAR files in it. 因此,JAR文件中包含其他JAR文件。 For this to work your main program has to have intimate knowledge on how to read those JAR files in the main JAR file. 为此,您的主程序必须对如何读取主JAR文件中的那些JAR文件有深入的了解。

To clarify, the result file will look like this: 为了澄清,结果文件将如下所示:

project.jar/
   com/yourproject/Main.class
   dependencyA.jar
   dependencyB.jar

For the other option, the Extract required libraries into generated JAR , what will happen is that those dependencies will be unpacked and then packed into the JAR files. 对于另一个选项,将所需的库提取到生成的JAR中 ,将发生的情况是将那些依赖项解压缩然后打包到JAR文件中。 This time not as JAR files but as the class and resources files. 这次不是作为JAR文件,而是作为类和资源文件。 This method will allow Java to find dependent class files from your main JAR file. 该方法将允许Java从您的主JAR文件中找到依赖类文件。

The result now will look like this 现在的结果将如下所示

project.jar/
   com/yourproject/Main.class
   com/dependencyA/SomeClass.class
   com/dependencyA/SomeOtherClass.class
   ...

This format will be readable to the Java process that will look for class and resource files in a JAR not the nested JAR files. Java进程将读取此格式,该Java进程将在JAR中查找类和资源文件,而不是嵌套的JAR文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM