【问题标题】:FileNotFoundException when using java properties file使用 java 属性文件时出现 FileNotFoundException
【发布时间】:2013-10-10 19:32:41
【问题描述】:

我在做了大量研究之后提出了这个问题,并且在研究之后也在我的代码中实现了它,但我最终得到了 FileNotFoundException。这里到底在做什么是我想避免在我的 java 代码中进行硬编码,所以我正在创建一个属性名称为 Constants.properties 的文件,并在我的 java 代码中调用它。但它说它没有找到该文件。我的属性文件位于项目的 src 文件夹中。下面是代码sn-p。有什么建议吗?

属性文件:

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 代码:这是包含属性文件详细信息的类文件。

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;
    }
}

主类:

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

给出这个例外:

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

【问题讨论】:

  • 异常发生在哪一行?
  • @JavaDevil:进程 pr = rt.exec(connString); 发生异常
  • 请发布异常堆栈跟踪。
  • @Sotirios Delimanolis:用异常堆栈跟踪编辑了我上面的问题。

标签: java file-io runtime.exec


【解决方案1】:

是的,不要将属性文件放入 src 文件夹。将它放在您启动 jvm 的位置(或提供绝对路径)。另外我真的建议去掉路径名中的正斜杠。

更新:添加此内容以找出放置文件的位置:

System.out.println(new File(".").getAbsolutePath());

【讨论】:

  • 我试着把它放在主类存在的包中,但仍然是同样的异常。
  • 主类(java文件)还是主类.class文件?
  • 它在我的 java 文件所在的同一个包中。
  • 哇,这就像一个魅力。我使用了上面的行,它向我展示了路径。我在我的代码中根据它更改了路径,它就像一个魅力。非常感谢你拯救了我的一天。
  • 因为那是你启动 JVM 的地方(或者它被设置为你使用的快捷方式中的工作目录)。
【解决方案2】:

您加载 Constants.properties 的方式应该在您的 src 包包下,在您的打包开始的级别。

例如,

如果你有 src/java/propinfopackage/PropInfo

把它放在java文件夹中,如下调用它

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

【讨论】:

    【解决方案3】:

    我有同样的问题,它是这样解决的:

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

    【讨论】:

      【解决方案4】:

      确保您的属性文件位于项目的根路径中。右键单击项目并粘贴属性文件。你的错误会消失。

      另请参阅上面的@Axel 答案。它将解决您的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-17
        • 2023-04-03
        • 1970-01-01
        • 2014-11-01
        • 1970-01-01
        • 2013-08-26
        • 2018-08-23
        • 2012-05-30
        相关资源
        最近更新 更多