0

I have this code below and it's working perfectly fine when I use it in eclipse. However if I build a executable jar file this code fails to complete. The problem is that the text file is never created.

This is the absolut way to the class folder "game/src/res" where the map "worlds" is where I try to create the text file.

So my question:

Is there an easy way to create a text file when your running a executable jar file?

public GenerateWorld(int m, int n,int y, int x){//m is rows n is columns

    Random random = new Random();


    try{
        out = new PrintStream(new FileOutputStream("src/res/worlds/temple.txt")); 
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    out.println(n+" "+m);
    out.println(x+" "+y);
    numbers = new String[m][n]; 
    String row = "";
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            if(i+1==x&&j+1==y){
                numbers[i][j] = 29+" ";
            } else {

                if(i==0){//TOP WALL
                    if(j==0){
                        numbers[i][j] = 25+" ";
                    } else if(j+1==n) {
                        numbers[i][j] = 26+" ";
                    } else {
                        numbers[i][j] = 21+" ";
                    }
                } else if(j==0){//LEFT WALL
                    if(i==0 || i+1 == m){
                        numbers[i][j] = 25+" ";
                    } else {
                        numbers[i][j] = 23+" ";
                    }
                } else if(i+1 == m){//BOTTOM;
                    if(j == 0 || j+1 == n){
                        numbers[i][j] = 25+" ";
                    } else {
                        numbers[i][j] = 22+" ";
                    }
                } else if(j+1 == n){//RIGTH WALL
                    if(i==0){
                        numbers[i][j] = 26+" ";
                    } else if( i+1==m){
                        numbers[i][j] = 25+" ";
                    } else {
                        numbers[i][j] = 24+" ";
                    }
                } else {

                    if(random.nextInt(20)==5){
                        numbers[i][j] = 28+" ";
                    } else {
                        numbers[i][j] = 20+" ";
                    }
                }
            }
            row += numbers[i][j];

        }
        out.println(row);
        row = "";
    }
    out.close();
}
Viktor Lindblad
  • 100
  • 1
  • 9

2 Answers2

2

Since you use new FileOutputStream("src/res/worlds/temple.txt"), make sure, this path relative to current working dir does actually exist. If it doesn't (ie. "src/res/worlds/"), then Java can't create "temple.txt" file. As you are running it from Eclipse, your CWD is set to project directory, where presumably "src/res/worlds" dir already exist. But if you run it from JAR, and CWD is not you project's home dir, then this path is not available.

So make use of File's class isDirectory method to test if your path exists and if not, make use of mkdirs method to create whole path at once.

But it would be better to consider placing generated files in some configurable directory (not hard coded in the source file).

Cromax
  • 1,822
  • 1
  • 23
  • 35
1

What you can do is that you can get the absolute path of jar in the code and after that using a properties file you can get the path of where you want to create the directory using either MKDIRS or just checking whether the file is created or not and then creating the FILE.

Getting absolute path String absolutePath = getClass().getProtectionDomain().getCodeSource()

After that justload the properties file internally or externally and then use the path.

Reading Properties file in Java

Community
  • 1
  • 1
Rahul Singh
  • 19,030
  • 11
  • 64
  • 86