[Java]Storing Properties within a jar?

Junior Spellweaver
Joined
Jul 11, 2008
Messages
118
Reaction score
16
Ok, my goal is to store a properties file within a jar, but its proving to be quite difficult.

PHP:
    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
launchprefs.setProperty("browser", jTextField2.getText());
launchprefs.setProperty("client", jTextField3.getText());
jDialog2.setVisible(false);
        try {
            launchprefs.store(new FileOutputStream("launchprefs.properties"), "Properties for this program.");
        } catch (IOException ex) {
           JOptionPane.showMessageDialog(jDialog2, "Error Saving Properties.");
        }
    }

Currently I'm using this snippet of code to save the properties file, it is already pre-existing within the package and I want to overwrite it with a new one which originally was the goal of the above code. However, what my current code does is save outside the jar, I executed it on my desktop and upon invoking this method a new properties file was created on my desktop. Is it possible to save a properties file IN a jar, and if so, how?
 
Even if it saves it outside the JAR if you access it correctly it should work whether it's inside or out.
I am not aware of a simple way to do it, but because a JAR file is merely a zip file renamed, all you should need to do is open it up using one of the ZIP routine/classes provided by Java's classes, and you should then be able to save it inside it. There are even routines provided for working with JARs so you can circumvent using the ZIP, but they are virtually the same. (Subclassed and creating a JarEntry to replace the ZipEntry).

Dig through the hierarchy here, and you should find what you need:
java.util.jar
 
Back