2013-04-04 76 views
0

大量的财产文件文章和评论后,我真的失去了。在JAR中使用属性文件

我想要的是检索值并覆盖它们 - 我想将它用于jar文件。

如果我在eclipse中编译,它完美的工作,但我编译的时候,我得到了着名的“属性文件未找到” - 异常。

FileInputStream in = new FileInputStream(ClassLoader.getSystemResource("client.properties").getPath()); 
Properties props = new Properties(); 
     props.load(in); 
     in.close(); 

我得到的例外是:

C:\Users\thomas\Desktop>java -jar erp_auer_client_v0_1.jar 
java.io.FileNotFoundException: file:\C:\Users\thomas\Desktop\erp_auer_client_v0_ 
1.jar!\client.properties (Die Syntax f³r den Dateinamen, Verzeichnisnamen oder d 
ie Datentrõgerbezeichnung ist falsch) 
     at java.io.FileInputStream.open(Native Method) 
     at java.io.FileInputStream.<init>(Unknown Source) 
     at java.io.FileInputStream.<init>(Unknown Source) 
     at global_functions.helperFunctions.getPathBARTENDEREXE(helperFunctions. 
java:361) 
     at client.programmeinstellungen.ProgrammEinstellungenManagement.<init>(P 
rogrammEinstellungenManagement.java:59) 
     at client.main.MainOverview$11.mousePressed(MainOverview.java:275) 

德国部分(模具语法f³r书房Dateinamen,Verzeichnisnamen奥德d 即DatentrõgerbezeichnungIST falsch)是指“为文件名,目录的语法名称或磁盘名称错误“。

你知道这可能是什么吗?

+0

你想有你的属性文件里面的jar(好吧不是一个问题)。接下来你想要保存这个属性文件中的值,对吧?我不确定这是否可行,因为jar文件是一个自包含文件,您的应用程序应该在该自包含文件中运行。我认为你只能从Eclipse运行,因为Eclipse是从存储项目的文件夹中进行的,而不是从真正的jar文件中进行的......这有意义吗? – Rafa 2013-04-04 03:46:46

+0

谢谢你的快速回答。但是,如果是这样的话,我可以在哪里/哪里永久储存价值并可以再次更改它? – tomier 2013-04-04 03:47:49

+0

jar文件外的任何地方:(1)你可以硬编码给定的位置; (2)你可以假设你的属性文件和你的jar文件在同一个文件夹中; (3)您可以添加一个带有文件位置的参数并使用它,如下所示:java -jar erp_auer_client_v0_1.jar -filePath c:\ temp \ client.properties – Rafa 2013-04-04 03:54:29

回答

0

好吧,我自己试了一下,发现它比我想的要复杂一点。但是,我认为在你的情况下,你可以使用类似波纹管的东西。我测试了在Windows和Linux,并为我工作得很好:

package mypackage; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.OutputStream; 
import java.net.URISyntaxException; 
import java.util.Date; 
import java.util.Properties; 

public class MyMainClass { 

    public static void main(String[] args) throws URISyntaxException { 
     /* 
     * 
     * MyMainClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); 
     * In Linux it returns null 
     * 
     * ClassLoader.getSystemClassLoader().getResource(".").getPath() + "MyProperty.properties"; 
     * In Linux it returns {JRE_PATH}/lib/ext/pulse-java.jar 
     * In Windows it returns {JAR_FILE_PATH} 
     */ 

     String propertyFilePath = "MyProperty.properties"; 
     Properties props = new Properties(); 
     try { 
      FileInputStream in = new FileInputStream(propertyFilePath); 
      props.load(in); 
      in.close(); 
     } catch (Exception e) { 
      File f = new File(propertyFilePath); 
      System.err.println("Could not read file: " + f.getAbsolutePath()); 
     } 

     System.out.println("Fetching property value of [now]: " + props.get("now")); 
     String now = new Date().toString(); 
     System.out.println("Storing property [now]: " + now); 
     props.setProperty("now", now); 

     try { 
      OutputStream out = new FileOutputStream(propertyFilePath); 
      props.store(out, "Saving value of [now]"); 
     } catch (Exception e) { 
      File f = new File(propertyFilePath); 
      System.err.println("Could not write file: " + f.getAbsolutePath()); 
     } 
    } 
}