-1

I have a problem with writing to properties file via .jar file. My 'output' works only in Eclipse, but don't know how to change it and make it works in executable .jar.

        FileOutputStream output = new FileOutputStream("src/application/data.properties",true);
        InputStream in = Login.class.getResourceAsStream("data.properties");
        properties.load(in);

        properties.setProperty(username+"Username", username);
        properties.setProperty(username+"Password", password);
        properties.store(output, formattedDate);

EDIT:

I'm getting no errors. I want to store this 'username' and 'password' in data.properties file which is included in a project. When I run it in Eclipse, it works. When I run it by .jar file, it doesn't write those data to properties file. I suppose, it's because when it creates 'output' while running jar, it is created only locally, and it doesn't provide me the access to appropriate file, but I might be wrong. I get this access and I can work on this existing file when I create 'in' by .getResourceAsStream(), but don't know how create 'output' in a similiar way, and be able to store data in already existing file in project. I create .jar in eclipse by "Export -> runnable JAR file " and run it by double-click.

Tomek
  • 3
  • 3
  • 2
    You don't provide sufficient informations. What are you getting? What do you want? How do you create this jar? How do you run it? We can't guess. – Turtle May 24 '17 at 12:40
  • 1
    When it's running as a jar in a different locaion, the folders "src/application" probably won't exist. Are you getting an error? – Steve Smith May 24 '17 at 13:16
  • I'm getting no errors. – Tomek May 24 '17 at 13:32
  • When you run from a .jar file, your development project is not visible and effectively does not exist. Use the data.properties in your .jar file as a *prototype* for a *brand new* data.properties stored somewhere under the user’s home directory. See https://stackoverflow.com/documentation/java/2433/resources-on-classpath#t=201701111734535687024&a=remarks and https://stackoverflow.com/questions/35388882/find-place-for-dedicated-application-folder. – VGR May 24 '17 at 15:13

1 Answers1

1
  1. I'll risk a guess: getResourceAsStream is returning a Stream reading the properties file inside the JAR, while FileOutputStream is addressing a file outside the JAR - the properties inside the JAR is never changed!

    It is not trivial to overwrite a file inside a (running) JAR! The Jar must be recreated for that. Probably better only write/read from an external file, that is, use FileInputStream instead of getResourceAsStream. It's also a bit strange to access a file in the "src" directory since this normally is not included in the deployed system/code.

  2. Using FileOutputStream(..., true) is appending the data to the end of file - it is not overwriting the data for an existing user. Not sure if that is a problem or if this code is only called for new users?!

  3. saving clear passwords (file or database) is a potential security threat...

Community
  • 1
  • 1
user85421
  • 28,957
  • 10
  • 64
  • 87