为什么getResourceAsStream()在IDE中工作,而不是JAR? [英] Why does getResourceAsStream() work in the IDE but not the JAR?

查看:202
本文介绍了为什么getResourceAsStream()在IDE中工作,而不是JAR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想读一个文件到我的程序中。该文件位于../f.fsh工作目录上的一个目录中。所以当我在IDE中运行它时,下面的代码正确运行。
InputStream is = getClass()。getResourceAsStream(name);
InputStreamReader isreader = new InputStreamReader(is); //在这里触发NULL POINTER EXCEPTION
BufferedReader br = new BufferedReader(isreader);

但是当我创建一个包含f.fsh的JAR文件并将其运行时,创建InputStreamReader时崩溃,因为InputStream为空。



我已经阅读了一堆有关输入流和JAR文件的问题的答案,以及我从这是我应该使用相对路径,但我已经这样做了。从我所了解的getResourceAsStream()可以找到相对于项目根目录的文件,这就是我想要的。为什么在JAR中不起作用?出了什么问题,我该如何解决?

这是否与classpath有关?我认为这只是包含文件的外部正在运行。



我也试过,但仍然失败,当在斜线:

  InputStream is = getClass()。getResourceAsStream(\\+ name); 

我看了一下: .. 在Eclipse中运行时在 Class.getResourceAsStream()中工作,这是Eclipse中的一个错误。 Eclipse和其他IDE实现自定义类加载器,以便在运行时从项目中获取资源。看起来Eclipse中的类加载器实现没有对 getResourceAsStream()方法的输入执行所有必要的验证。在这种情况下,这个错误对你有利,但是你仍然需要重新考虑如何为你的代码构建你的资源,以便在所有情况下都能正常工作。


I just want to read a file into my program. The file is located one directory above the working directory at "../f.fsh". So the following code runs correctly when I run it in the IDE

String name="../f.fsh";
InputStream is = getClass().getResourceAsStream(name);
InputStreamReader isreader=new InputStreamReader(is);//CRASHES HERE WITH NULL POINTER EXCEPTION
BufferedReader br = new BufferedReader(isreader);

but when I create a JAR file that has f.fsh zipped inside of it and run it, it crashes when creating the InputStreamReader, because the InputStream is null.

I've read a bunch of answers to questions about input streams and JAR files, and what I got out of it is that I should be using relative paths, but I am already doing that. From what I understand getResourceAsStream() can find files relative to the root of the project, that is what I want. Why does it not work in the JAR? What is going wrong, how can I fix it?

Does it have to do with the classpath? I thought that was only for including files external to the jar being run.

I have also tried, but still fail, when putting a slash in:

InputStream is = getClass().getResourceAsStream("\\"+name);

I looked at: How to get a path to a resource in a Java JAR file andfound that contents of a JAR may not necesarily be accesible as a file. So I tried it with copying the file relative to the jar (one directory up from the jar), and that still fails. In any case I'd like to leave my files in the jar and be able to read them there. I don't know what's going wrong.

解决方案

If .. works in Class.getResourceAsStream() while running from Eclipse, it's a bug in Eclipse. Eclipse and other IDEs implement custom class loaders to fetch resources from the project at runtime. It looks like the class loader implementation in Eclipse isn't performing all the necessary validations on input to getResourceAsStream() method. In this case the bug is in your favor, but you will still need to rethink how you structure your resources for your code to work in all cases.

这篇关于为什么getResourceAsStream()在IDE中工作,而不是JAR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆