0

After a lot of property file articles and comments I am really lost.

All I want is to retrieve values and to overwrite them - and I want to use that with a jar-file.

If I compile in eclipse it works perfectly but the moment I compile I got the famous "property file not found"-exception.

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

The exception I got is the following:

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)

The german part (Die Syntax f³r den Dateinamen, Verzeichnisnamen oder d ie Datentrõgerbezeichnung ist falsch) means "The syntax for the filename, directory name or disk name is wrong".

Do you have an idea what that could be?

4

1 回答 1

0

Alright, I tried it myself and realized that it is a little more trickier than I thought initially. However, I think that in your scenario you can just use something like bellow. I tested in windows and linux and worked fine for me:

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());
        }
    }
}
于 2013-04-07T16:07:32.847 回答