简体   繁体   English

尝试从jar中读取资源文件

[英]Trying to read a resource file from within a jar

I have read every single question and answer regarding this topic and I have tried a multitude of the suggested solutions, none of which are working, currently I am trying to use the URL resource method but url always returns null. 我已经阅读了有关此主题的每个问题和答案,并且尝试了许多建议的解决方案,但都没有用,目前我正在尝试使用URL资源方法,但url始终返回null。 Here is my current pseudo setup: 这是我当前的伪设置:

/src/java/pkg/name/is/long/meatpackage/MyClassFile.java /src/java/pkg/name/is/long/meatpackage/MyClassFile.java

/src/java/pkg/name/is/long/meatpackage/myresources/myresourcefile.txt /src/java/pkg/name/is/long/meatpackage/myresources/myresourcefile.txt

@SuppressWarnings("rawtypes")
Class thisClass = Class.forName("pkg.name.is.long.meatpackage.MyClassFile");
ClassLoader cLoader = thisClass.getClassLoader();
//note, I have tried a plethora of variations to the path for the file, this is just one example
URL url = cLoader.getResource("pkg/name/is/long/meatpackage/myresources/myresourcefile.txt");
System.out.println("Found the file!!: " + url);

Seriously, it can't be THIS hard to include a text file in your jar and access it at runtime can it? 认真地说,在您的jar中包含一个文本文件并在运行时访问它不是很难吗?

When I list the jar it shows that the file is in pkg/name/is/log/meatpackage/myresources/myresourcefile.txt. 当我列出jar时,它表明文件位于pkg / name / is / log / meatpackage / myresources / myresourcefile.txt中。

Another approach I have tried is: 我尝试过的另一种方法是:

//along with multiple path combinations for the file the InputStream is always null.    
//multiple paths include but are not limited to:
// "pkg/name/is/long/meatpackage/myresources/myresourcefile.txt"
// "/pkg/name/is/long/meatpackage/myresources/myresourcefile.txt"
// "/myresources/myresourcefile.txt"
// "/myresourcefile.txt"
// "myresourcefile.txt"
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("myresourcefile.txt"); 
BufferedReader br = new BufferedReader(new InputStreamReader(is)); 

You can simplify this using getClass().getResourceAsStream() or MyClassFile.getResourceAsStream() - you don't need to talk to the classloader directly. 您可以使用getClass()。getResourceAsStream()或MyClassFile.getResourceAsStream()简化此过程-无需直接与类加载器对话。

Note that the resource name is slash separated, and if you specify / then it's from the root of the JAR or from the base of the class path, otherwise it's implicitly from the same package directory as the class is. 请注意,资源名称以斜杠分隔,如果您指定/,则它是从JAR的根目录或类路径的基部开始的,否则它与类是相同的包目录中的隐含目录。

For example: 例如:

package com.example; 包com.example; public class Foo { static { Foo.class.getResourceAsStream("bar.txt"); 公共类Foo {静态{Foo.class.getResourceAsStream(“ bar.txt”); // looks for /com/example/bar.txt Foo.class.getResourceAsStream("/bar.txt"); //寻找/com/example/bar.txt Foo.class.getResourceAsStream(“ / bar.txt”); // looks for /bar.txt } } //寻找/bar.txt}}

If you're developing inside Eclipse, note that the getResourceAsStream only looks inside the compiled output directory (ie whatever is in bin ). 如果您在Eclipse中进行开发,请注意, getResourceAsStream仅在编译后的输出目录中查找(即bin任何内容)。 So if you have your resource outside of there, copy it into the src directory (so it gets copied over to the bin directory at build time) and it should find it properly. 因此,如果您的资源不在此处,请将其复制到src目录中(以便在构建时将其复制到bin目录中),并且应该可以正确找到它。

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

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