0

I've tried to use the CMD console instead of using Eclipse's console because I wanted to try to do a simple animation ('|', '/', '-', '\') and couldn't find a way to clear the screen on Eclipse's console.

After exporting my code into a Runnable Jar file, I opened it on the CMD console using java -jar test.jar but I get a FileNotFoundException.

To help debug my code, I've tried compiling it on Eclipse. This is the following output:

|/-\|/-\|/-\

Note: between each string output has a 500 ms delay.

public void doAnimation(String animation) {
        try {
            BufferedReader br = new BufferedReader(new FileReader( new File("res/animations.txt")));
            String current = null;
            boolean exit = false;
            while((current = br.readLine()) != null && !exit) {
                if(current.toLowerCase().equals(animation.toLowerCase())) {
                    String[] frames = br.readLine().split(" ");
                    int repeat = 3;
                    for(int r = 0; r < repeat; r++) {
                        for(int i = 0; i < frames.length; i++) {
                            clearConsole();
                            print(frames[i]);
                            Thread.sleep(500);
                        }
                    }
                    exit = true;
                }
            }
            br.close();
        } catch (FileNotFoundException e) {
            print("Could not find animation file.");
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

I expected a simple rotating stick animation but instead it wouldn't find the text document.

Dhananjaya
  • 372
  • 1
  • 6
  • 22
Alex
  • 31
  • 4

1 Answers1

0

Ensure the jar file has res/animations.txt as top directory file.

Use this.getClass().getClassLoader().getResoirceAsStream() to get input stream opened for file.

In case of eclipse, whatever you put in project root can be accessed as relative path. But when you use jar file, your all resources must be embedded inside the jar file so as to be used as relative path.

Shailesh Pratapwar
  • 4,054
  • 3
  • 35
  • 46