How to load files using relative path in NetBeans

Raghavendra picture Raghavendra · Aug 24, 2012 · Viewed 21.8k times · Source
import java.io.*;
import java.util.Properties;

public class NewClass {
    public static void main(String args[]) throws IOException  {
        Properties p = new Properties();
        p.load(new FileInputStream("DBDriverInfo.properties"));
        String url=p.getProperty("url");
        String user=p.getProperty("username");
        String pass=p.getProperty("password");
        System.out.println(url+"\n"+user+"\n"+pass);
    }
}

Though the file DBDriverInfo.properties file is in the same directory, the following exception is raised.

Exception in thread "main" java.io.FileNotFoundException: DBDriverInfo.properties (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:97)
    at NewClass.main(NewClass.java:7)

Relative paths work fine when compiled using javac in a command line interface. But the exception raises in NetBeans.

Answer

Lo Juego picture Lo Juego · Aug 24, 2012

In Netbeans you need to put that file inside the project folder rather than in the src/package folder.