2013-10-10 4 views
5

stelle diese Frage nach viel Forschung und auch implementieren es in meinem Code nach der Forschung, aber ich endete mit FileNotFoundException.Was genau hier zu tun ist, ich möchte hardcoding in meinem Java vermeiden Code erstellt also eine Eigenschaftendatei mit dem Namen Constants.properties und rufe sie in meinem Java-Code auf. aber es sagt, dass es die Datei nicht findet. Meine Eigenschaftendatei befindet sich im Ordner src des Projekts. Unten ist das Code-Snippet. Irgendwelche Vorschläge?FileNotFoundException bei der Verwendung von Java-Eigenschaften Datei

Properties-Datei:

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-Code: Dies ist die Klasse-Datei, die die Eigenschaften Details Datei hat.

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

Hauptklasse:

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

gibt diese Ausnahme:

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

In welcher Zeile tritt die Ausnahme auf? –

+0

@JavaDevil: Bei Prozess tritt eine Ausnahme auf pr = rt.exec (connString); – user2821894

+0

Bitte buchen Sie die Exception-Stack-Trace. –

Antwort

12

Ja, nicht Ihre Eigenschaften-Datei in den Ordner src. Setze es dort ab, wo du die jvm startest (oder einen absoluten Pfad angeben). Ich schlage auch vor, Vorwärtsstriche in Pfadnamen zu entfernen.

UPDATE: Fügen Sie diese, um herauszufinden, wo die Dateien setzen:

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

Ich habe versucht, es in das Paket, wo die Hauptklasse existiert, aber immer noch die gleiche Ausnahme. – user2821894

+0

Die Hauptklasse (Java-Datei) oder die Hauptklassen .class-Datei? – Axel

+0

Es ist in dem gleichen Paket, wo meine Java-Datei ist. – user2821894

3

Die Art und Weisen Sie Constants.properties es auf der Ebene, wo Ihre Verpackung beginnt seine direkt unter src Paket Paket laden soll.

zum Beispiel

wenn Sie hava src/java/propinfopackage/PropInfo

es Ordner in Java setzen und es nennen wie folgt

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

Ich hatte das gleiche Problem und es war gelöst wie folgt:

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

Stellen Sie sicher, dass sich Ihre Eigenschaftendatei im Stammverzeichnis von t befindet er Projekt. Klicken Sie mit der rechten Maustaste auf das Projekt und fügen Sie die Eigenschaftendatei ein. Dein Fehler wird verschwinden.

Siehe auch @Axel Antwort oben. Es wird Ihr Problem lösen.

Verwandte Themen