0

I built a game a while ago in eclipse. I wanted to export it like usual to a .jar. I've done this before and with my other projects it works. This one doesn't work because it gives a blank white screen. I put in a System.exit(1) on a different spots to see where the program stops working. Eventually I found the spot. It can't seem to locate my .txt file but in eclipse if does work. I've checked the .jar with 7zip(like winzip) and the .txt file is in there. I also used the command line to open it and it gives the same error of not being able to locate the .txt file. I've looked a lot on the internet and stackflow for people that have same questions of related problems but they didn't offer a fix unfortunately.

The code that make a new world is this.

 world = new World(handler,"res/worlds/world1.txt");

In the world it calls this funtion:

 loadWorld(path);

This funtion should return back a string. First it calls another function to get that string:

 String file = Utils.loadFileAsString(path);

Finally this is the code that doesn't work because the file can't be located.

    public static String loadFileAsString(String path){
    StringBuilder builder = new StringBuilder();

    try{

        BufferedReader br = new BufferedReader(new FileReader(path));
        String line;
        while((line = br.readLine()) != null)
            builder.append(line + "\n");
        br.close();
    }catch(IOException e){
        e.printStackTrace();
    }
    return builder.toString();
}

Is there something that I missed? Eclipse runs it fine but after exporting its like the .txt isn't exported but it is still there. Thanks in advance.

3 Answers3

0

The java class File (which is used by FileReader) operates on OS the file system path, either absolute, or relative to the current working directory. Your example denotes a relative path. I beg "res/world/..." is just beyond your project folder in eclipse. When running an app in eclipse the current directory is the project path itself. Therefore your code worked in eclipse.

Running standalone (by starting a jar) the file cannot be found in the same OS path. It is located within the jar. Therefore you must access it by using the resource loading features of your classloader.

Heri
  • 4,368
  • 1
  • 31
  • 51
0

That is a relative path. Relative paths are resolved relative to where you are when you execute the application. In eclipse this is, by default, the project directory. However, if you are running the jar externally, it will depend entirely on how you run the jar.

One common solution is to always have the jar placed in the same location relative to the resource (world1.txt). For example, the following directory structure...

-lib
--main.jar
-res
--worlds
---world1.txt
main.sh

Then, you need to get some base directory into your application. One way is to pass in the absolute path to main.jar as a command line argument, system property, or environment variable.

Another common approach is to get the path of the running jar file using some technique like this.

Finally, in your application, instead of using a relative path, you can use an absolute path:

String world1Path = baseDirectory + "res/worlds/world1.txt"
Community
  • 1
  • 1
Pace
  • 41,875
  • 13
  • 113
  • 156
0

If you have an archive reader (such as 7zip) open up the .jar and see if your .txt file is inside the .jar. If not, simply copy the .txt file into the .jar file. This happened to me when trying to use a .png for a Java application. Hope it works out!

Ethan Moore
  • 383
  • 1
  • 5
  • 18