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?

tomier
  • 1
  • 4
  • You want to have your property file inside a jar (ok not a problem). Next you want to save values in this property file, right? I am not sure if that is possible, because a jar file is a self contained file and your application should run within that self contained file. I think you can run from Eclipse only because Eclipse does it from a folder where your project is stored and not from a real jar file... Does it make sense? – Rafa Apr 04 '13 at 03:46
  • Thank you for your fast answer. But if so how / where can I store values permanently and can change it again? – tomier Apr 04 '13 at 03:47
  • Anywhere outside the jar file: (1) You can hard code a given location; (2) You can just assume your property file will be on the same folder as your jar file; (3) You can add a parameter with the file location and work with it, something like: java -jar erp_auer_client_v0_1.jar -filePath c:\temp\client.properties – Rafa Apr 04 '13 at 03:54
  • Since the file is started with double-Click on the jar-file the (2) would be the best. What is usually the correct way if you want to export a jar-program for user usage? – tomier Apr 04 '13 at 03:58
  • I use maven, but if you are using Eclipse you can just export as a jar file and follow the Export Wizard. It should be alright. – Rafa Apr 04 '13 at 04:01
  • What would be the right path for "FileInputStream in = new FileInputStream(ClassLoader.getSystemResource("client.properties").getPath());" when I want to have option (2) same folder? (Off topic) How do you start an application over maven? What is the difference? Ah, I mean export – tomier Apr 04 '13 at 04:14
  • If you use (2), then it is not a SystemResource anymore see: http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file – Rafa Apr 04 '13 at 04:21
  • Maven is a dependency tool, you can build a project and create a jar (or war, ...). However, you need to learn how to create a descriptor (pom.xml) and the command line. It might take some time for you to learn... – Rafa Apr 04 '13 at 04:25
  • Ok. Thank you very much for your help. I can't +1 you because it's just a comment. – tomier Apr 04 '13 at 16:14
  • Hey, the problem is now that the same jar file is NOT working on another PC. I don't see why but it doesn't create the folder on the other PC. – tomier Apr 05 '13 at 20:22

1 Answers1

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());
        }
    }
}
Rafa
  • 1,997
  • 3
  • 21
  • 33