简体   繁体   English

Java:无法弄清楚如何使用class.getClassLoader()。getResourceAsStream(),因此在构建工件时可以使用

[英]Java: Can't figure out how to use class.getClassLoader().getResourceAsStream() so it works when i build artifacts

I have been tasked with developing a discord bot for a friend of mine and need to load some strings from a file. 我的任务是为我的一个朋友开发一个Discord机器人,并且需要从文件中加载一些字符串。 Now, to make this work when i export the project into a jar, someone told me to use class.getClassLoader().getResourceAsStream() 现在,要在将项目导出到jar中时使其工作,有人告诉我使用class.getClassLoader()。getResourceAsStream()

After fiddling around for 2 hours, reading documentation and asking again, i got it to work in intelliJ. 摆弄了两个小时,阅读文档并再次询问后,我得到它在intelliJ中工作。 It still does not work when i build the project though. 虽然当我构建项目时它仍然不起作用。

Project Structure My Code: 项目结构我的代码:

static final ArrayList<String> QUOTES = new ArrayList<>();

public static void loadArray() {
    try{
        //File fin = new File("src/main/java/com/github/MarvelousAdain/Quotes");
        //FileInputStream fis = new FileInputStream(fin);
        System.out.println("Called loadArray Method");
        BufferedReader br = new BufferedReader(new InputStreamReader(Utilities.class.getClassLoader().getResourceAsStream("Quotes")));

        String line;
        while ((line = br.readLine()) != null) {
            QUOTES.add(line);
        }
        System.out.println("Quotes loaded, no Problem.");
        br.close();
    }catch(IOException e){e.printStackTrace();}
}

If i try running my code in the jar, this throws a NullPointerException. 如果我尝试在jar中运行代码,则会抛出NullPointerException。

Stacktrace: 堆栈跟踪:

Exception in thread "main" java.lang.NullPointerException
    at java.io.Reader.<init>(Unknown Source)
    at java.io.InputStreamReader.<init>(Unknown Source)
    at com.github.MarvelousAdain.Utilities.loadArray(Utilities.java:31)
    at com.github.MarvelousAdain.Main.main(Main.java:18)

Help would be greatly appreciated. 帮助将不胜感激。

Is the tool that creates the jar including the resource? 创建jar的工具是否包含资源?

You can check with 您可以检查

jar tf myjar.jar

Or indeed, your favourite zip tool. 或确实是您最喜欢的zip工具。

Peculiarly getResourceAsStream() returns null instead of throwing some kind of IOException like a normal API would. 奇特的getResourceAsStream()返回null而不是像普通API那样抛出某种IOException

Also note that ClassLoader.getResourceAsStream will take the name relative to the classpath, but Class.getResourceAsStream will modify the path using the name of the package of the specified class (ie in the same "directory" as the .class file). 还要注意, ClassLoader.getResourceAsStream将采用相对于类路径的名称,但是Class.getResourceAsStream将使用指定类的包的名称(即与.class文件位于同一“目录”中)来修改路径。

So, this might be more of a work around, but it was the only way i got it to work right now. 因此,这可能是更多的解决方法,但这是我现在使它工作的唯一方法。 If someone has a better idea, which i'm sure someone has, then i'd still like to hear it. 如果某人有一个更好的主意,我确定有人有,那么我还是想听听。

I built the jar, opened it with winrar and put the 3 files that i want to read in there. 我建立了罐子,用winrar打开它,然后将要读取的3个文件放在那里。 It worked. 有效。 If i put them in the same place within the IDE, it did not. 如果我将它们放在IDE中的同一位置,则不会。 I have no idea how/why this is the case, but for now this works for me. 我不知道这是为什么/为什么,但是现在这对我有用。 I'll update this if i find something better! 如果发现更好的地方,我会更新的!

暂无
暂无

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

相关问题 class.getClassLoader getResourceasStream在jar中返回null - class.getClassLoader getResourceasStream returns null in jar getClassLoader().getResourceAsStream() 如何在 Java 中工作 - How getClassLoader().getResourceAsStream() works in java 使用class.getClassLoader()。getResourceAsStream时抛出java.lang.NullPointerException - java.lang.NullPointerException throws while using class.getClassLoader().getResourceAsStream install4j6:class.getClassLoader()。getResourceAsStream(fileName)返回Null - install4j6: class.getClassLoader().getResourceAsStream(fileName) returns Null 从jar throgh class.getClassLoader()。getResource(“”);运行时,在Maven项目Java中找到一个文件夹。 方法 - Locate a folder in a maven project java when running from jar throgh class.getClassLoader().getResource(“”); method 如何指定class.getClassLoader()返回哪个类加载器? - How to specify which class loader should class.getClassLoader() return? Java getClassLoader()。getResourceAsStream(文件名) - Java getClassLoader().getResourceAsStream(filename) Java新手无法弄清楚如何使用绘画方法 - New to java can't figure out how to use paint method 我不知道如何从命令行调用Java oscar3类方法stringToString - I can't figure out how to call Java oscar3 class method stringToString from the command line 无法弄清楚如何重置一个int,因此我可以正确使用循环来制作Pig游戏 - Can't figure out how to reset an int so I can use my loop properly making the game of Pig
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM