Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java file path in Linux

Tags:

java

I have a rather silly question, but I haven't been able to find a solution for this:

When I try and read a file I get a "file not found error" is runtime. It compiled the file though.

I am on Linux, so I use the statement something like:

Scanner s = new Scanner(new File("home/me/java/ex.txt"));

and it gives me a runtime rror:

/home/me/javaException in thread "main" java.io.FileNotFoundException: home/me/java/ex.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:137)
at java.util.Scanner.<init>(Scanner.java:653)
at test.main(test.java:14)

I tried changing every possible thing along the lines of filenames, but nothing seems to work.

Any clues as to why this is happening? where does java look for files by default?

like image 536
AJW Avatar asked Dec 25 '11 02:12

AJW


1 Answers

Looks like you are missing a leading slash. Perhaps try:

Scanner s = new Scanner(new File("/home/me/java/ex.txt"));

(as to where it looks for files by default, it is where the JVM is run from for relative paths like the one you have in your question)

like image 198
Todd Gardner Avatar answered Sep 24 '22 13:09

Todd Gardner