简体   繁体   English

在JAR文件中时,URL getResource无法正常工作

[英]URL getResource not working when in JAR file

My application loads a txt file which is in PROJECTNAME/resource folder. 我的应用程序加载了PROJECTNAME/resource文件夹中的txt文件。

在此处输入图片说明

Here is how I'm loading the file: 这是我加载文件的方式:

URL url = getClass().getClassLoader().getResource("batTemplate.txt");
java.nio.file.Path resPath;
String bat = "";

try {
    resPath = java.nio.file.Paths.get(url.toURI());
    bat = new String(java.nio.file.Files.readAllBytes(resPath), "UTF8");
} catch (URISyntaxException | IOException e1) {
        e1.printStackTrace();
}

NOTE: This works when I run from Eclipse, and doesn't when I Export to a Jar file (Extract required lib. into generated JAR). 注意:这在我从Eclipse运行时有效,而在我导出到Jar文件(将所需的库提取到生成的JAR中)时不起作用。 I know that the file is being extracted because its in the JAR file. 我知道该文件正在提取中,因为它在JAR文件中。 Also the images work. 图像也起作用。

在此处输入图片说明

Error MSG: 错误MSG:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:
    Illegal character in path at index 38: file:/C:/DataTransformation/Reports Program/ReportsSetup.jar
        at com.sun.nio.zipfs.ZipFileSystemProvider.uriToPath(ZipFileSystemProvider.java:87)
        at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:166)
        at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
        at java.nio.file.Paths.get(Unknown Source)
        at ReportSetup$13.mouseReleased(ReportSetup.java:794)

I also looked at the similar problem here, however they refer to file/URL outside JAR file. 我在这里也看到了类似的问题,但是它们引用的是JAR文件之外的文件/ URL。

A .jar entry is not a file; .jar条目不是文件; it is part of the .jar archive. 它是.jar存档的一部分。 It is not possible to convert a resource to Path. 无法将资源转换为Path。 You'll want to read it using getResourceAsStream instead. 您将要使用getResourceAsStream来阅读它。

To read all of it, you have a few options. 要阅读所有内容,您有一些选择。 You can use a Scanner: 您可以使用扫描仪:

try (Scanner s = new Scanner(getClass().getClassLoader().getResourceAsStream("batTemplate.txt"), "UTF-8")) {
    bat = s.useDelimiter("\\Z").next();
    if (s.ioException() != null) {
        throw s.ioException();
    }
}

You can copy the resource to a temporary file: 您可以将资源复制到一个临时文件中:

Path batFile = Files.createTempFile("template", ".bat");
try (InputStream stream = getClass().getClassLoader().getResourceAsStream("batTemplate.txt")) {
    Files.copy(stream, batFile);
}

You can simply read the text from an InputStreamReader: 您可以简单地从InputStreamReader中读取文本:

StringBuilder text = new StringBuilder();
try (Reader reader = new BufferedReader(
    new InputStreamReader(
        getClass().getClassLoader().getResourceAsStream("batTemplate.txt"),
        StandardCharsets.UTF_8))) {

    int c;
    while ((c = reader.read()) >= 0) {
        text.append(c);
    }
}

String bat = text.toString();

I think you should try getClass().getResourceAsStream("/batTemplate.txt"). 我认为您应该尝试getClass()。getResourceAsStream(“ / batTemplate.txt”)。

Also check this . 还要检查一下 Might help. 可能有帮助。

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

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