作为jar运行时FileNotFoundException [英] FileNotFoundException while running as a jar
问题描述
FileInputStream fstream = new FileInputStream(abc.txt)
在运行时抛出FileNotFoundExceptionn一个罐子。为什么?通常它可以在从main方法运行时找到。
is throwing a FileNotFoundExceptionn while running as a jar. Why ? Normally it is able to find while running from main method.
推荐答案
class MyClass{
InputStream fstream = this.getClass().getResourceAsStream("abc.txt");
}
应使用此代码。
并且应该在Object references类的位置保存文件(在本例中为abc.txt)。这意味着,this.getClass指的是某个文件夹的位置,即com / myfolder / MyClass.java文件夹。
This code should be used. And the files(in this case abc.txt) should be kept , in the Object references class location. That means , this.getClass refers to the location of some folder i.e, com/myfolder/MyClass.java folder .
所以我们应该将abc.txt保存在com / myfolder这个位置。
So we should keep the abc.txt in com/myfolder this location.
这篇关于作为jar运行时FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!