Reputation: 1412
I run a java application like this:
java --cp libs.jar MyClass myconfigfile
After a normal shutdown, the app will remember the values I had in numerous text fields and checkboxes. I'm told that 'last state' is kept in java preferences. I want to grab my last state from one computer and bring it over to another. Is there a way to do this?
I'm using Ubuntu 12.04.
Upvotes: 0
Views: 293
Reputation: 1168
Regardless if your program is a class or a jar, one of the most portable methods is to just have a config file at the root of your project.
Example:
// Makes a file at the root of your project, and the name starts with a period so its hidden by the system.
File stateFile = new File(".lastState");
// Creates a file IF it doesn't exist already
stateFile.createNewFile();
// Do stuff
// Then save stuff to the file
Psuedo code for writing options:
PrintStream out = new PrintStream(stateFile);
for(Option option : yourList.options()) {
// Print the option name, a Unit seperator char, the status of the option, and a Group seperator char
out.print(option.getName() + '\x1F' + option.status() + '\x1D');
}
Psuedo code for reading options:
private boolean readOptionsFromStateFile(Map<String, Option> mapOfOptionNamesToOption) {
Scanner in = new Scanner(stateFile);
in.setDelimiter("\\x1D"); // Set the delimiter to the group seperator
while(in.hasNext()) {
String temp = in.next();
if(temp == "") return;
String[] keyValues = temp.split("\x1F"); // Split at the unit seperator
if(keyValues.length != 2) {
return false;
}
Option curOption = mapOfOptionNamesToOption.get(keyValues[0]);
if(curOption != null) {
curOption.setStatus(keyValues[1]);
} else {
return false;
}
}
return true;
}
Upvotes: 0
Reputation: 12332
Based on the limited information you provide, it sounds like the application is using the Java Preferences API. In this case, where the data is actually stored is OS-dependent. In Windows, it's in the Registry. Linux and OS X, I'm not sure. That is, the data is not likely stored in a single properties file somewhere from which you can just copy it.
If you have access to the code, you can export the Preferences object to a file though.
FileOutputStream output = new FileOutputStream("myPrefs.xml");
myPrefs.exportSubtree(output);
Edit: Looks like in Linux you may find the info in one of these two places:
~/.java/.userPrefs
/etc/.java/.systemPrefs
Upvotes: 1