开发者

Where does Java put resource files when I JAR my program?

开发者 https://www.devze.com 2023-04-02 05:27 出处:网络
Been looking for this for the past 2 hours and can\'t find anything (I\'ve found solutions to the same problem but with images, not text files).

Been looking for this for the past 2 hours and can't find anything (I've found solutions to the same problem but with images, not text files).

Pretty much, I made a program that reads a text file. The file is a list of names and IDs. Using Eclipse, I put the file in my src folder and in the program put the path file to it. Like this:

in = new BufferedReader(new FileReader(curDir+"\\bin\\items.txt"));

Where curDir is the user's current directory (found with System.getProperty("user.dir")).

Now, problem is, the program runs fine when 开发者_Go百科I run it from Eclipse, but when I try to make it a runnable JAR and then run it, the program runs, but the info from the text file does not load. It look like Eclipse is not putting the text file with the JAR.

EDIT: Solved-ish the problem? So the JAR file needs to the in a folder with all the original files? I am so confused, what is a JAR file then?


A more robust way to get a file whether you are running from Eclipse or a JAR is to do

MyClass.getResource("items.txt")

where MyClass is a class in the same package (folder) as the resource you need.

If Eclipse is not putting the file in your JAR you can go to

Run -> Run Configurations -> -> Classpath tab -> Advanced -> Add Folders

Then add the folder containing your file to the classpath. Alternatively, export the Ant script and create a custom build script.


To the point, the FileReader can only read disk file system resources. But a JAR contains classpath resources only. You need to read it as a classpath resource. You need the ClassLoader for this.

Assuming that Foo is your class in the JAR which needs to read the resource and items.txt is put in the classpath root of the JAR, then you should read it as follows (note: leading slash needed!):

InputStream input = Foo.class.getResourceAsStream("/items.txt");
reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
// ...

Or if you want to be independent from the class or runtime context, then use the context class loader which operates relative to the classpath root (note: no leading slash needed!):

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("items.txt");
reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
// ...

(UTF-8 is of course the charset the file is encoded with, else you may see Mojibake)


Get the location of your jar file

Firstly create a folder(say myfolder) and put your files inside it

Consider the following function

public String path(String filename)
{
URL url1 = getClass().getResource("");
String ur=url1.toString();
ur=ur.substring(9);
String truepath[]=ur.split("myjar.jar!");
truepath[0]=truepath[0]+"myfolder/";
truepath[0]=truepath[0].replaceAll("%20"," ");
return truepath[0]+filename;
}//This method will work on Windows and Linux as well.
//You can alternatively use the following line to get the path of your jar file
//classname.class.getProtectionDomain().getCodeSource().getLocation().getPath();

Suppose your jar file is in D:\Test\dist

Then path() will return /D:/Test/dist/myfolder/filename

Now you can place 'myfolder' inside the folder where your jar file is residing

OR

If you want to access some read-only file inside your jar you should copy it to one

of your packages and can access it as

yourClassname.getResource("/packagename/filename.txt");

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号