7

多くの調査を行い、調査後にコードに実装した後にこの質問をしていますが、最終的に FileNotFoundException. 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
4

4 に答える 4

14

はい、プロパティ ファイルを src フォルダーに置かないでください。jvm を開始する場所に置きます (または絶対パスを指定します)。また、パス名のスラッシュを取り除くことを強くお勧めします。

更新:これを追加して、ファイルの配置場所を見つけます。

System.out.println(new File(".").getAbsolutePath());
于 2013-10-10T19:43:07.063 に答える
4

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");
.......
于 2013-10-10T19:43:53.400 に答える
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) {}
于 2014-03-28T20:40:32.953 に答える