FileNotFoundException when using java properties file
Asked Answered
S

4

7

am asking this question after doing a lot of research and also implementing it in my code after the research but I ended up with FileNotFoundException.What exactly am doing here is I want to avoid hardcoding in my java code so am creating a properties file with name as Constants.properties and am calling it in my java code. but it says that it is not finding the file. My properties file is in the src folder of the project. Below is the code snippet. Any suggestions?

Properties file:

executable.run = C:\\server\\lrd.exe
incoming.file = C:\\file\\test.ic
executable.params1 = -z
executable.params2 = -a[+]
log.file = C:\\TESTFile\\test.txt

Java Code: This is the class file which has the properties file details.

public class PropInfo {
    static private PropInfo _instance =  null;
    public String executable =  null;
    public String filein = null;
    public String params1 = null; 
    public String params2 = null; 
    public String log = null; 

    protected PropInfo(){
        try{
            InputStream file = new FileInputStream(new File("Constants.properties"));
            Properties props = new Properties();
            props.load(file);
            executable = props.getProperty("executable.run");
            filein = props.getProperty("incomin.file");
            params1 = props.getProperty("executable.params1");
            params2 = props.getProperty("executable.params2");
            log = props.getProperty("log.file");
        } 
        catch(Exception e){
            System.out.println("error" + e);
        }    
    }

    static public PropInfo instance(){
        if(_instance == null){
            _instance = new PropInfo();
        }
        return _instance;
    }
}

Main Class:

try{
    PropInfo propinfo = PropInfo.instance();
    String connString = propinfo.executable + " " + propinfo.params1 + " " + 
            propinfo.filein + " " + propinfo.params2 + " " + " " + propinfo.log ;

    Runtime rt = Runtime.getRuntime();
    // Process pr = rt.exec 
    // (PropInfo.executable+" "+PropInfo.params1+" "+PropInfo.filein+" "
    //+PropInfo.params2+" "+PropInfo.log);
    Process pr = rt.exec(connString);

    BufferedReader input = new BufferedReader(new InputStreamReader (pr.getInputStream()));

    String line=null;
    StringBuffer start= new StringBuffer();
    while((line=input.readLine()) != null) {
        start.append("Started" + line + "\n");
        System.out.println(line);
    }

    // System.out.println("browse");

}
catch (Throwable t)  
{  
    t.printStackTrace();  
}  
finally 
{  
}

Gives this Exception:

errorjava.io.FileNotFoundException: Constants.properties (The system cannot find the  
file specified)
java.io.IOException: Cannot run program "null": CreateProcess error=2, The system  
cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at com.emc.clp.license.StartTest.main(StartTest.java:44)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the 
 file     specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:288)
at java.lang.ProcessImpl.start(ProcessImpl.java:133)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
... 4 more
Slouch answered 10/10, 2013 at 19:32 Comment(4)
In which line is the exception occurring?Agna
@JavaDevil: Exception is occuring at Process pr = rt.exec(connString);Slouch
Please post the exception stack trace.Bed
@Sotirios Delimanolis: Edited my question above with the exception stack trace.Slouch
C
14

Yes, don't put your properties file into the src folder. Put it where you start the jvm from (or provide an absolute path). Also I really suggest getting rid of forward slashes in path names.

UPDATE: Add this to find out where to put your file:

System.out.println(new File(".").getAbsolutePath());
Corettacorette answered 10/10, 2013 at 19:43 Comment(5)
I tried putting it in the package where the main class exists but still the same exception.Slouch
The main class (java file) or the main classes .class file?Corettacorette
Its in the same package where my java file is.Slouch
Wow this worked like a charm.I used the above line and it showed me the path. I changed the path according to it in my code and it worked like a charm. Thanks so much you saved my day.Slouch
Because that is where you started the JVM from (or it is set as the working directory in the shortcut you used).Corettacorette
F
4

The way you load Constants.properties it should be right under your src package package at the level where your packaging starts.

for example,

if you hava src/java/propinfopackage/PropInfo

put it inside java folder and call it as follows

    InputStream propertiesInputStream = null;
                Properties properties = new Properties();
                propertiesInputStream = PropInfo.class.getClassLoader().getResourceAsStream("/Constants.properties");
                properties.load(propertiesInputStream);
  String value = properties.getProperty("executable.run");
.......
Fitter answered 10/10, 2013 at 19:43 Comment(0)
G
1

I had the same problem and it was solved like this:

Properties prop = new Properties();
try {
    prop.load(getClass().getResourceAsStream("/com/my/package/Constants.properties"));//here your src folder
    System.out.println(prop.getProperty("executable.run"));

} catch(IOException e) {}
Guideboard answered 28/3, 2014 at 20:40 Comment(0)
B
1

Make sure your properties file is in root path of the project. Right click on project and paste the properties file. Your error will go.

Also refer @Axel Answer above. It will resolve your issue.

Bitch answered 10/3, 2017 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.