0

So I have to run a program using the terminal. But when I do that it tells me that the file I'm trying to use can not be found. When I use NetBeans to run my program, there is not problem what so ever. This id my Code

    File file = new File("src/level/Level2.txt");
    FileReader fileReader = new FileReader(file);
    BufferedReader reader = new BufferedReader(fileReader);

When it works, the program should open a window with the level from the file. When trying out "level/Level2.txt" for the filename, it opens an empty window. Hope anybody can help me.

EDIT I found what was wrong. My Program couldn't find the Images I wanted to display.

Hxebu1
  • 9
  • 2

1 Answers1

-1

To make things less mysterious, you can try using an absolute path instead of relative.

String currentDir = Paths.get(".").toAbsolutePath().normalize().toString();
System.out.println("currentDir = " + currentDir);

For example, on my system this output this directory name:

currentDir = /Users/harry/Projects/proj3

The absolute path to the directory you see from the command line will depen on where you started Java to run from the console.(Assuming your Java app doesn't try to change the current working directory.)

Suggestion borrowed from here:

Getting the Current Working Directory in Java

I'm suggesting this to help with your particular problem, and not as general advice for using paths in Java applications. Relative paths are better... so good to figure out how your program is accessing directories when you run it without your IDE.

Community
  • 1
  • 1
akubot
  • 141
  • 1
  • 8
  • Okay I tried it and it's not the same directory. The terminal gives me: currentDir = /Users/Henrike/NetBeansProjects/DisCaptive/src and netbeans gives me: currentDir = /Users/Henrike/NetBeansProjects/DisCaptive – Hxebu1 Sep 14 '16 at 17:42
  • Thank you for showing me this. It helped me to get the Images to be found. – Hxebu1 Sep 14 '16 at 18:58
  • Glad that helped... I should have said that running from the command prompt will give you an answer that depends on where you run the Java command to start your program. – akubot Sep 14 '16 at 21:00
  • You should avoid using absolute path unless you want your program running on one and only one computer. Much better to understand and fix the problem. – Hovercraft Full Of Eels Sep 15 '16 at 01:57