2013-10-10 46 views
5

我问了这个问题后做了大量的研究,也在研究后在我的代码中实现它,但我最终与FileNotFoundException.What正是我在这里做的是我想避免硬编码在我的Java代码,以便创建一个名为Constants.properties的属性文件,并在我的Java代码中调用它。但它表示它没有找到该文件。我的属性文件位于项目的src文件夹中。以下是代码片段。有什么建议么?FileNotFoundException当使用java属性文件

属性文件:

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 
+0

哪条线发生异常? –

+0

@JavaDevil:进程发生异常pr = rt.exec(connString); – user2821894

+0

请发布异常堆栈跟踪。 –

回答

12

是,不要把你的属性文件到src文件夹。把它放在你启动jvm的地方(或者提供一个绝对路径)。另外,我真的建议摆脱路径名称中的正斜杠。

更新:加入此找出把你的文件:

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

我试过把它放在主类存在的包中,但仍然是相同的异常。 – user2821894

+0

主类(java文件)还是主类.class文件? – Axel

+0

它在我的java文件是相同的包。 – user2821894

3

加载Constants.properties它应该是正确的你的src包包下,在其中您开始包装水平的方式。

例如,

如果你HAVA的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"); 
....... 
1

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

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

请确保您的属性文件在根路径t他的项目。右键单击项目并粘贴属性文件。你的错误将会消失。

另请参考@Axel上面的答案。它会解决你的问题。