4

I have a java application project in Netbeans. I have just one class. I try to do this

FileReader fr = new FileReader("sal.html");

I have the file sal.html under the same package. But I get this error when I run:

Errorjava.io.FileNotFoundException: sal.html (The system cannot find the file specified)
John Eipe
  • 10,922
  • 24
  • 72
  • 114

7 Answers7

4

My guess is that Netbeans is invoking the JVM from your project's root folder. Quoting a portion of the File Javadoc:

By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.

To verify relative path resolution you could try:

System.out.println(System.getProperty("user.dir"));
System.out.println(new File("sal.html").getAbsolutePath());

You could then move your file to wherever java is looking for it. Most probably your project's root folder.

You could also consider using the class loader to read files as resources inside packages using getClass().getResourceAsStream("sal.html");. This is the preferred way of accessing resources since you no longer have to worry about absolute vs. relative paths. If a resource is in your classpath, you can access it. See this answer for more.

Community
  • 1
  • 1
Sahil Muthoo
  • 12,033
  • 2
  • 29
  • 38
  • one more doubt! You said the directory from where JVM is invoked is stored in user.dir system property. I ran the above 2 lines of code and I get the same output. `C:\Users\Onie\Documents\NetBeansProjects\Testapp\sal.html` `C:\Users\Onie\Documents\NetBeansProjects\Testapp` So it should be working right? Also I tried `new FileReader("C:\\Users\\Onie\\Documents\\NetBeansProjects\\Testapp\\sal.html");` and it fails! – John Eipe Sep 03 '11 at 17:14
  • the same error. `Errorjava.io.FileNotFoundException: C:\Users\Onie\Documents\NetBeansProjects\Testapp\sal.html (The system cannot find the file specified)` – John Eipe Sep 03 '11 at 17:21
  • If the file's actual location = output of `new File("file").getAbsolutePath()`, it shouldn't throw a `FileNotFoundException`. You say your code works when you invoke it from the command line? – Sahil Muthoo Sep 03 '11 at 18:02
  • yes.. and there is no FileNotFoundException when i run it from cmd line – John Eipe Sep 04 '11 at 17:19
1
System.out.println(System.getProperty("user.dir"));
System.out.println(new File("sal.html").getAbsolutePath());

Then it will show where the JVM is retrieving the files from. Usually for linux in the /home/username/NetbeansProjects/ApplicationName/.

Put your resources or files to this path

Pokechu22
  • 4,984
  • 9
  • 37
  • 62
1

Put your file to main project folder. Not to any sub folders like src, or bin etc. Then it will detect your file.

Jayanga Kaushalya
  • 2,674
  • 5
  • 38
  • 58
1

Click on file view in Netbeans. Move sal.html to the project folder. Such that you will see it like this

- JavaProject
  + build
  + lib
  + nbproject
  + src
  + build.xml
  manifest.mf
  sal.html

Now

FileReader fr = new FileReader("sal.html");

will work.

0

I think your problem is in the relative path to the file. Try to declare FileReader with full path to file.

4e6
  • 10,696
  • 4
  • 52
  • 62
0

FileNotFoundException means file not found.

The build folder for the netbeans is different where there is no file sal.html.

Try using absolute path in place of using relative path.

Vivek Goel
  • 22,942
  • 29
  • 114
  • 186
0

This is not a "File not found" problem. This is because each class hold its own resources (let it be file, image etc.) which can be accessed only through a resource loader statement which is as below:

InputStream in = this.getClass().getResourceAsStream("sal.html");

The only fix is that you will get an InputStream instead of a file. Hope this helps.

jagbandhuster
  • 677
  • 2
  • 7
  • 20