0

I'm trying to read a file in java, when I run the program through the IDE it works fine but when it tries to open it when I execute the jar it says the file does not exist. Here is the code where it fails.

    public class Main {

    public static void main(String[] args) {

            try {
                App app = new App("files/" + "jsonFile.json", printWriter);
                app.runApp();

            } catch (Exception e) {
                logger.error("error", e);
            }
    }   
}




public class App {

public void runApp(){
    File fileDescription = new File("./" + pathDescription);
    StringBuilder allDescription = new StringBuilder();
    try {
            FileReader fr = new FileReader(fileDescription);
            BufferedReader br = new BufferedReader(fr);
            String line = "";
            while ((line = br.readLine()) != null) {
                allDescription.append(line);
            }
            JSONDescription = allDescription.toString();
            fr.close();
            br.close();
    } catch (IOException e) {
            logger.error("Error reading file",e);
    }
}

}

I know the file exists inside the jar because I looked manually into the jar using jarzilla. Any idea of what it could be happening.

elcharrua
  • 1,582
  • 6
  • 30
  • 50
  • your code blocks are not you wrote business logic directly within App , without any method in it, what value does variable pathDescription take. – arjunsv3691 Nov 28 '17 at 17:07
  • This can't be the code. new app("files/" + "jsonFile.json", printWriter) won't compile since app starts with lower case + your class doea not have a constructor that takes String and printWriter. Also main won't compile since you didn't define any printWriter. – Yoav Gur Nov 28 '17 at 17:07
  • This isn't C, your main method has to be in a class. – Impulse The Fox Nov 30 '17 at 12:21

3 Answers3

2

You can lookup a file inside a jar using something like this:

    InputStream stream = ClassInsideTheJar.class.getResourceAsStream("/files/jsonFile.json");
    BufferedReader br  = new BufferedReader(new InputStreamReader(stream));

Where "ClassInsideTheJar" is any class in the jar.

Merch0
  • 594
  • 5
  • 17
1

To access files within the JAR, you could use something like

BufferedReader reader = new BufferedReader(new InputStreamReader(
this.getClass().getResourceAsStream("files/jsonFile.json")));

This assumes that there is a folder called files in the root of your jar, and inside that you have the jsonFile.json file.

Chetan Jadhav CD
  • 1,116
  • 8
  • 14
1

When you run the program inside an IDE, the compiled code exists outside the jar, in the file system. The IDE compile the code, and then run the program, without building the JAR file. So, your program found the file, because that file still exists in the file system.

To open a file inside a JAR, you need to use another API: loading the file as a resource.

load file within a jar

Alejandro Goñi
  • 532
  • 1
  • 3
  • 14