Confused about "files inside a JAR"

I have a program that has a Properties file which I would like to "save" into the JAR file, so it is not outside the JAR.

I know this can be done, because I've seen some Java programs that save their configuration between sessions, but not via a file inside its directory. This leads me to believe the file is maintained within the JAR itself, which would be REALLY handy and secure.

Is this a correct assumption? If so, how would I do it? Here's a "test program" that we can play with, how would I save MyProperties' properties field into the MyProgram JAR?

package SBX;

import java.util.*;

import java.io.*;

publicclass MyProgram{

// Instance Variables

public MyProperties myProp =new MyProperties();

public String stringOne ="Will this be loaded?";

public String stringTwo ="How about this one?";

// Main Method

publicstaticvoid main(String[] args){

new MyProgram();

}

// Constructors

public MyProgram(){

if ((new File("properties.txt")).exists()){

myProp.updateProgram(this);

}

System.out.println("1) " + stringOne);

System.out.println("2) " + stringTwo);

myProp.updateProgram(this);

System.out.println("3) " + stringOne);

System.out.println("4) " + stringTwo);

myProp.updateProperties(this);

// -- //

// Now that we've "updated" stringOne & stringTwo and "updated" myProp and saved it,

// we should be able to get the values "ONE" and "TWO" for print outs 1 & 2 when we next

// run the program, instead of "Will this be loaded?" and "How about this one?", as we got during the first run.

// However, the thing I need help with is saving the Properties to the JAR file so it's not accessable

// outside of the JAR file. How can this be done?

}

}

class MyProperties{

// Instance Variables

private Properties properties;

// Constructors

public MyProperties(){

Properties defaults =new Properties();

defaults.setProperty("stringOne","ONE");

defaults.setProperty("stringTwo","TWO");

properties =new Properties(defaults);

try{

FileInputStream fis =new FileInputStream("properties.txt");

properties.load(fis);

fis.close();

}

catch (Exception e){

System.out.println("No properties file found, ignore this; one will be saved when the program exits.");

}

}

// Methods

publicvoid updateProgram(MyProgram prg){

prg.stringOne = properties.getProperty("stringOne");

prg.stringTwo = properties.getProperty("stringTwo");

}

publicvoid updateProperties(MyProgram prg){

properties.setProperty("stringOne", prg.stringOne);

properties.setProperty("stringTwo", prg.stringTwo);

try{

FileOutputStream fos =new FileOutputStream("properties.txt");

properties.store(fos,"A header");

fos.close();

}

catch (Exception e){

e.printStackTrace();

}

}

}

Thanks for any help!

PS...writing demo programs is kind of fun :)

[5755 byte] By [LukeFossa] at [2007-11-27 10:55:23]
# 1

You can't modify files in a JAR. Look at Preferences.

CeciNEstPasUnProgrammeura at 2007-7-29 11:56:05 > top of Java-index,Java Essentials,Java Programming...
# 2

Fossie,

Yup... you can jar up your properties file... you just need to add it to manifest file and jar (the program) takes care of the rest... google that there's loads of existing articles.

BUT, I don't think you can modify the properties file in the jar... it may be possible, but I just don't know how. Instead (I think) you ship your default properties file in the jar, but still save a local properties file to the same directory as the jar (obviously won't work for applets)... then you load your defaults, and the local settings over the top.

and PS I don't see how sticking a properties file in the jar would "secure" it in any fashion... it's just a zip file after all... would you care to elaborate?

Cheers,

Keith.

corlettka at 2007-7-29 11:56:05 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks for the replies!

I'm checking out the Preferences class right now.

And corlettk, what I think I was trying to get at with more secure was that it wouldn't be easily viewable / openable by a user. That, and it will make the program's directory look cleaner. I'll also try your suggestion, though, and read up about this manifest creature. I feel stupid not knowing fully what it is!

Cheers

LukeFossa at 2007-7-29 11:56:05 > top of Java-index,Java Essentials,Java Programming...
# 4

I wish I had known about Preferences much earlier than this thread, it would have saved me a ton of work on a ton of things.

Thanks for the help. :)

LukeFossa at 2007-7-29 11:56:05 > top of Java-index,Java Essentials,Java Programming...