简体   繁体   English

运行可执行maven jar文件时Java加载资源失败:无法解析“file:/...”

[英]Java loading of resources fails when running executable maven jar file: unable to resolve “file:/…”

My issue is that when I build an executable jar with maven (using the shade plugin) and attempt to run the jar, I receive an error:我的问题是,当我使用 maven(使用 shade 插件)构建一个可执行 jar 并尝试运行该 jar 时,我收到一个错误:

java.io.IOException: Unable to resolve "file:/C:/Users/name/Desktop/nameofjar-SNAPSHOT.jar!/path/to/file.gz

When I run the main program within eclipse, the GUI loads the resource file correctly.当我在 eclipse 中运行主程序时,GUI 正确加载了资源文件。 I printed the output of the directory of the file:我打印了文件目录的输出:

Loading parser from serialized file /C:/Users/name/git/guis/target/classes/path/to/file.gz

The IOException occurs at this line of code: IOException 发生在这行代码处:

URL url = SentenceParser.class.getResource("file.gz");
props.setProperty("file", url.getPath());

The file.gz is located in src/main/resources/path/to/file.gz and the class is defined in a java src directory of src/main/java/path/to. file.gz 位于 src/main/resources/path/to/file.gz 中,类定义在 src/main/java/path/to 的 java src 目录中。

After research, my problem is almost exactly like this person's question: java, loading of resources fails: unable to resolve file:/my-jar.jar!/folder/my-file .经过研究,我的问题几乎和这个人的问题一模一样: java,加载资源失败:无法解析 file:/my-jar.jar!/folder/my-file They state that they must have the path of the file in order to put into a properties file.他们声明他们必须有文件的路径才能放入属性文件。 I, also, need the path of the file and cannot retrieve the resource as a stream.我也需要文件的路径,不能以流的形式检索资源。 I need the path in order to add that path to a properties file which is then read by an external library.我需要路径以便将该路径添加到属性文件,然后由外部库读取。

Maybe it's best to place your file external to your jar file if it needs to be read in and manipulated by other jars in your application.如果您的应用程序中的其他 jar 文件需要读入和操作它,那么最好将您的文件放在 jar 文件的外部。 Like so:像这样:

file.gz文件.gz
myjar.jar我的jar文件
myjar2.jar myjar2.jar

That way you can obtain an absolute path to the common resource file by using code like:这样您就可以使用以下代码获取公共资源文件的绝对路径:

static final String WORKINGDIR = System.getProperty("user.dir");
static final String SEPARATOR = System.getProperty("file.separator");
String path =  WORKINGDIR + SEPARATOR + "file.gz";
URL url = SentenceParser.class.getResource(path);

Try this and see if it works.试试这个,看看它是否有效。

The thing is that when you run your application directly from within Eclipse, the application itself is not inside a jar.问题是,当您直接从 Eclipse 中运行应用程序时,应用程序本身并不在 jar 中。 You're just running the class files, unpackaged.您只是在运行未打包的类文件。 Therefore, it can find the resource file just fine.因此,它可以很好地找到资源文件。 But when you jar your application and run it, the resource file resides inside the jar and the application has difficulty locating it.但是,当您 jar 应用程序并运行它时,资源文件驻留在 jar 中,应用程序很难找到它。 If you keep your resource files external to the jar, they are easier to locate using the code I provided above.如果您将资源文件保存在 jar 外部,则使用我上面提供的代码更容易找到它们。 I find this works best for me, anyway.无论如何,我发现这最适合我。

Oh boy!好家伙!

I ran into the same/similar issue yesterday;我昨天遇到了相同/类似的问题; and scratched my (bald) head for better part of a day as to why the file in resources folder is accessible in Eclipse - but not by the executable JAR.并在一天的大部分时间里挠了挠我的(秃头)头,以了解为什么可以在 Eclipse 中访问资源文件夹中的文件 - 但不能通过可执行 JAR 访问。

Reading project properties....
Jul 14, 2021 2:03:12 PM main.helper.CALotteryLogger logAbort
SEVERE: *******************************************************
Jul 14, 2021 2:03:12 PM main.helper.CALotteryLogger logAbort
SEVERE: * Alert:          InputStream for main/Resources/Common config With IDs.properties is NULL.
Jul 14, 2021 2:03:12 PM main.helper.CALotteryLogger logAbort
SEVERE: *******************************************************

Today, I found out that the path of the resource file is case-sensitive - even on Windows systems - when it comes to accessing it from the executable JAR.今天,我发现资源文件的路径是区分大小写的——即使在 Windows 系统上——当涉及到从可执行 JAR 访问它时。

My original path for the resource was:我原来的资源路径是:

private static final String CONFIG_PROPERTIES_FILEPATH = "main/Resources/Common config With IDs.properties";

which resulted in failure.这导致了失败。 But when I changed it to但是当我把它改成

private static final String CONFIG_PROPERTIES_FILEPATH = "main/resources/Common config With IDs.properties";

the problem was resolved.问题解决了。

Note that "Resources" in the file name was changed to "resources" to resolve the issue.请注意,文件名中的“Resources”已更改为“resources”以解决该问题。

Usually, Windows is case-agnostic, but not when it comes to executable JAR files.通常,Windows 不区分大小写,但对于可执行 JAR 文件则不然。 Lesson learned!!学过的知识!!

PS: Following syntax was used to open the resource file: PS:以下语法用于打开资源文件:

InputStream is = ReadConfigData.class.getClassLoader().getResourceAsStream(CONFIG_PROPERTIES_FILEPATH);

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

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