eclipse Runnable Jar 找不到资源和其他库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13989785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:40:04  来源:igfitidea点击:

Runnable Jar cannot find Resources and Other Libraries

javaeclipseswtexecutable-jar

提问by unixhead

I created a desktop app and I have run into a problem with my generated runnable jar. Everything works fine in the Eclipse environment, but when I generate the jar it only shows theswtcomponents (menu, tabs, etc..). The other libraries location is a blank area (library to generate gallery). The same does not appearset ToolBar(containing buttons with images),GoogleMap.htmldoes not appear.

我创建了一个桌面应用程序,但我遇到了生成的可运行 jar 的问题。在 Eclipse 环境中一切正常,但是当我生成 jar 时,它只显示swt组件(菜单、选项卡等)。其他库位置是一个空白区域(库生成图库)。同样不会出现set ToolBar(包含带有图像的按钮),GoogleMap.html不会出现。

How can I correctly generate an executable jar that will include these external sources?

如何正确生成包含这些外部源的可执行 jar?

ToolBar image loading code :

工具栏图片加载代码:

folderSearchIcon = new Image(display, this.getClass().getResourceAsStream("images/search_folder.png"));

GoogleMap.html loading code :

GoogleMap.html 加载代码:

File mapFile = new File("resources/GoogleMap.html");        
if(!mapFile.exists()) {
    System.out.println("File doesn't exist! " + mapFile.getAbsolutePath()); 
    return;
}

Generating runnable jar:

生成可运行的 jar:

enter image description here

在此处输入图片说明

My app structure in Eclipse and generated jar structure:

我在 Eclipse 中的应用程序结构和生成的 jar 结构:

enter image description here

在此处输入图片说明

Generated manifest :

生成的清单:

Manifest-Version: 1.0
Rsrc-Class-Path: ./ swt.jar commons-imaging-1.0-SNAPSHOT.jar org.eclip
  se.nebula.widgets.gallery_0.5.3.201210262156.jar xmpcore.jar metadata
  -extractor-2.6.3.jar
Class-Path: .
Rsrc-Main-Class: geotagger.AppInit
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

采纳答案by Ian Roberts

For the toolbar image you need to add a slash, i.e. instead of

对于工具栏图像,您需要添加斜杠,即代替

this.getClass().getResourceAsStream("images/search_folder.png")

you need

你需要

this.getClass().getResourceAsStream("/images/search_folder.png")

This is because, as explained in the JavaDocs, Class.getResourceAsStreamresolves relative paths against the package of the class in question, so if thisis a com.example.Foothen getResourceAsStream("images/search_folder.png")would look for com/example/images/search_folder.pnginside your JAR. Prepending the slash would make it look for images/search_folder.pnginstead, which is what your screenshot suggests you need.

这是因为,正如 JavaDocs 中所解释的Class.getResourceAsStream针对相关类的包解析相对路径,所以 if thisis a com.example.FoothengetResourceAsStream("images/search_folder.png")com/example/images/search_folder.png在您的 JAR 中查找。预置斜杠会使它寻找images/search_folder.png,这就是您的屏幕截图表明您需要的。

You will need to use a similar trick for the GoogleMap.html- you can't load items from inside a JAR using java.io.File, but you could use this.getClass().getResource("/GoogleMap.html")to get a java.net.URLpointing to the HTML file inside your JAR.

您将需要使用类似伎俩的GoogleMap.html-你不能使用一个JAR中加载的项目java.io.File,但你可以使用this.getClass().getResource("/GoogleMap.html")得到一个java.net.URL指向JAR文件中的HTML文件。

回答by izaera

As you can see here:

正如你在这里看到的:

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)

getResourceAsStream() looks for the file in the same package as the class from which it is called. Thus it will look for a file named "com/whatever/more/images/search_folder.png" inside the JAR, which of course won't exist, because the contents of the "resources" directory are directly put in the root of the JAR file by the JAR exporter.

getResourceAsStream() 在与调用它的类相同的包中查找文件。因此它会在 JAR 中寻找一个名为“com/whatever/more/images/search_folder.png”的文件,这个文件当然不会存在,因为“resources”目录的内容是直接放在根目录下的JAR 导出器的 JAR 文件。

Use http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)instead.

请改用http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)

The second part: when you load the HTML will never work. You try to find a File named "resources/GoogleMap.html", but that will look outside of the JAR, in the working directory of your java program process. You should again use the previous function to load the HTML:

第二部分:当你加载 HTML 时将永远不会工作。您尝试在 Java 程序进程的工作目录中查找名为“resources/GoogleMap.html”的文件,但该文件将位于 JAR 之外。您应该再次使用前面的函数来加载 HTML:

http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)

http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)

回答by DDRider62

I lost many hours on this same issue. If you created your runnable jar using the options in the "Export" functionality in Eclipse, it just won't work. At least it didn't for me. No matter what number of options I tried, nothing worked. When trying to read the resources, the resourceUrl would return null. Finally, I decided to create the runnable jar using Maven, and leave the Eclipse kind help aside. I needed to have the dependent jars in a separate lib folder. I created the runnable jar as explained in http://www.mkyong.com/maven/how-to-create-a-manifest-file-with-maven/and the solution worked for me. Somehow, when Eclipse creates the runnable jar package, the resources cannot be read, even though they are present in the jar file. Hope this will help others.

我在同样的问题上浪费了很多时间。如果您使用 Eclipse 中“导出”功能中的选项创建了可运行的 jar,它将无法工作。至少它不适合我。无论我尝试了多少选项,都没有奏效。尝试读取资源时,resourceUrl 将返回 null。最后,我决定使用 Maven 创建可运行的 jar,而将 Eclipse 的帮助放在一边。我需要将依赖的 jars 放在一个单独的 lib 文件夹中。我按照http://www.mkyong.com/maven/how-to-create-a-manifest-file-with-maven/ 中的说明创建了可运行的 jar ,该解决方案对我有用。不知何故,当 Eclipse 创建可运行的 jar 包时,无法读取资源,即使它们存在于 jar 文件中。希望这会帮助其他人。

回答by Corey Byrum

Since exporting into a runnable jar essentially creates one file, the file/image/resource in your resources folder can no longer be found with FileReader because the file location is now non-existent..

由于导出到可运行的 jar 基本上会创建一个文件,因此 FileReader 无法再找到资源文件夹中的文件/图像/资源,因为该文件位置现在不存在..

Changing from using FileReader fr = new FileReader(resource_file_path)to loading with an InputStream, InputStream is = getClass().getResourceAsStream(resource_file_path)and then using an InputStreamReaderto actually use the resource in other locations. Hope this helps.

从使用更改为使用FileReader fr = new FileReader(resource_file_path)InputStream 加载,InputStream is = getClass().getResourceAsStream(resource_file_path)然后使用InputStreamReader实际使用其他位置的资源。希望这可以帮助。