使用java属性文件时FileNotFoundException
在做了大量的研究之后,我提出了这个问题,并且在研究之后在我的代码中实现了这个问题,但是我最终得到了FileNotFoundException。我在这里做的是避免在java代码中硬编码,所以我创建了一个名为Constants.properties的属性文件,并在java代码中调用它。但是它说它找不到文件。我的属性文件位于项目的src文件夹中。下面是代码片段。有什么建议吗 属性文件:使用java属性文件时FileNotFoundException,java,file-io,runtime.exec,Java,File Io,Runtime.exec,在做了大量的研究之后,我提出了这个问题,并且在研究之后在我的代码中实现了这个问题,但是我最终得到了FileNotFoundException。我在这里做的是避免在java代码中硬编码,所以我创建了一个名为Constants.properties的属性文件,并在java代码中调用它。但是它说它找不到文件。我的属性文件位于项目的src文件夹中。下面是代码片段。有什么建议吗 属性文件: executable.run = C:\\server\\lrd.exe incoming.file = C:\\f
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
errorjava.io.FileNotFoundException:Constants.properties(系统找不到
文件(已指定)
java.io.IOException:无法运行程序“null”:CreateProcess错误=2,系统
找不到指定的文件
位于java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
位于java.lang.Runtime.exec(Runtime.java:615)
位于java.lang.Runtime.exec(Runtime.java:448)
位于java.lang.Runtime.exec(Runtime.java:345)
位于com.emc.clp.license.StartTest.main(StartTest.java:44)
原因:java.io.IOException:CreateProcess error=2,系统找不到
指定的文件
在java.lang.ProcessImpl.create(本机方法)
位于java.lang.ProcessImpl。(ProcessImpl.java:288)
在java.lang.ProcessImpl.start(ProcessImpl.java:133)
位于java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
... 4更多
是的,不要将属性文件放入src文件夹。将它放在启动jvm的位置(或提供绝对路径)。我还真的建议去掉路径名中的前斜杠
更新:添加此项以查找文件的存放位置:
System.out.println(new File(".").getAbsolutePath());
加载Constants.properties的方式应该位于src包的正下方,位于包开始的级别 比如说, 如果您有src/java/PropInfo包/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) {}
确保属性文件位于项目的根路径中。右键单击project并粘贴属性文件。你的错误会消失的
另请参考上述@Axel答案。它会解决你的问题 异常发生在哪一行?@JavaDevil:异常发生在进程pr=rt.exec(connString);请发布异常堆栈跟踪。@Sotirios Delimanolis:用异常堆栈跟踪编辑了我上面的问题。我尝试将其放入存在主类但仍然是相同异常的包中。主类(java文件)或者主class.class文件?它和我的java文件在同一个包中。哇,这就像一个符咒。我用上面的一行,它向我展示了路径。我在代码中根据它更改了路径,它就像一个符咒一样工作。非常感谢你救了我一天。因为那是你启动JVM的地方(或者在你使用的快捷方式中它被设置为工作目录)。