[英]FileNotFoundException when using java properties file
我在做了大量的研究之后問了這個問題,並且在研究之后在我的代碼中實現了這個問題,但我最終得到了FileNotFoundException。我在這里做的正是我想避免在我的java代碼中硬編碼所以創建一個名字的屬性文件作為Constants.properties並在我的java代碼中調用它。 但它說它找不到文件。 我的屬性文件位於項目的src文件夾中。 以下是代碼段。 有什么建議么?
屬性文件:
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
是的,不要將屬性文件放入src文件夾中。 把它放在從你開始jvm的地方(或提供絕對路徑)。 另外,我真的建議擺脫路徑名中的正斜杠。
更新:添加此項以找出放置文件的位置:
System.out.println(new File(".").getAbsolutePath());
加載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");
.......
我有同樣的問題,它解決了這樣:
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) {}
確保您的屬性文件位於項目的根路徑中。 右鍵單擊項目並粘貼屬性文件。 你的錯誤會消失。
另請參閱上面的@Axel答案。 它將解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.