简体   繁体   English

使用java属性文件时的FileNotFoundException

[英]FileNotFoundException when using java properties file

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. 我在做了大量的研究之后问了这个问题,并且在研究之后在我的代码中实现了这个问题,但我最终得到了FileNotFoundException。我在这里做的正是我想避免在我的java代码中硬编码所以创建一个名字的属性文件作为Constants.properties并在我的java代码中调用它。 but it says that it is not finding the file. 但它说它找不到文件。 My properties file is in the src folder of the project. 我的属性文件位于项目的src文件夹中。 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. Java代码:这是具有属性文件详细信息的类文件。

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

Yes, don't put your properties file into the src folder. 是的,不要将属性文件放入src文件夹中。 Put it where you start the jvm from (or provide an absolute path). 把它放在从你开始jvm的地方(或提供绝对路径)。 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());

The way you load Constants.properties it should be right under your src package package at the level where your packaging starts. 加载Constants.properties的方式应该在您的包装开始的级别下的src包包下面。

for example, 例如,

if you hava src/java/propinfopackage/PropInfo 如果你hava src / java / propinfopackage / PropInfo

put it inside java folder and call it as follows 把它放在java文件夹中,并按如下方式调用它

    InputStream propertiesInputStream = null;
                Properties properties = new Properties();
                propertiesInputStream = PropInfo.class.getClassLoader().getResourceAsStream("/Constants.properties");
                properties.load(propertiesInputStream);
  String value = properties.getProperty("executable.run");
.......

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) {}

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. 另请参阅上面的@Axel答案。 It will resolve your issue. 它将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM