0

I have a project in eclipse

foo_project
 - src
  - bar_package
       bam.java
       info.txt
  - info.txt
 - resources
  - info.txt

In bam.java, say, I print the content of info.txt out like

    try {
        welcome = new BufferedReader(new FileReader("src/info.txt"));
        String currentLine = null;
        while ( (currentLine = welcome.readLine()) != null) {
            System.out.println(currentLine);
        }
        welcome.close();

    } catch (Exception fof) {
        System.err.println(fof.toString());
    } 

It is working inside eclipse as it is when I put info.txt under src folder, however, it doesn't work once I export this project to a JAR file.

In the code, I tried just "info.txt" as well as "src/info.txt", none of them is working! As you can see, I put info.txt pretty much everywhere and not successful!

How can I refer to this info.txt in the Java code, and make Java find it at both inside eclipse and JAR file?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alfred Zhong
  • 6,773
  • 11
  • 47
  • 59

5 Answers5

1

If the text in your info.txt will always be the same, instead of treating the text as a file, consider treating it as a "resource". If you do that , you can include it within your JAR, instead of having to distribute it as a separate file.

You open an InputStream to a resource using the Class.getResourceAsStream() method.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
0

I think to load files from Jar file the file reader method will not work effectively, you will have to use class.getResource() or class.getResourceAsStream() methods

some helpful links,

Load a resource contained in a jar

How to load resource from jar file packaged in a war file? Load resource from class's own JAR file

Also as others have suggested make sure the Jar file contains the txt file you looking for, you can either use "jar" command or winRAR

Community
  • 1
  • 1
Mehul Rathod
  • 1,244
  • 8
  • 7
0

To access resources inside a JAR file, you need to use

YourClass.getClassLoader().getResourceAsStream("info.txt")

This way it will look inside the JAR file (or rather, in all places on the classpath) for the file as a Resource. It will work in both Eclipse and when packaged as a JAR.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
0

I ended up using in the class

getClass().getResourceAsStream( "/info.txt") which gives an InputStream

Then I use InputStreamReader and BufferedReader to read out the file.

/ here is the src folder. Everything under this folder will be built to bin folder at the end.

If you have multiple source folders (a folder can be set to be source folder by right click and choose that option in Eclipse), all source folders built to single bin folder

For example, if you have source folders

sourceA
  foo_package/...
sourceB
  bar_package/...

then in bin, it will be

bin (this is the "/")
  foo_package/... 
  bar_package/...

Thanks for all the answers and inspiration to all!

Alfred Zhong
  • 6,773
  • 11
  • 47
  • 59
-1

you must put your txt file next to the jar file in your jar folder

it means that copy your txt file into your jar file folder not into jar file

Mostafa Jamareh
  • 1,389
  • 4
  • 22
  • 54