Getting an external resource on persons computer and saving to it

My problem is, I have a file, in a folder called res. now in that folder i have my application.properties file.

my program is running from a executable jar file, and that res folder IS NOT packaged in the jar(if it was i wouldnt be able to save the application.properties file.

how do i get the location of that folder, that is outside of the jar, and then if it doesnt exist, create it?

ive tried getClass().getClassLoader().getResource(""); but my program still throws up an error when its in jarmode…

heres my code if it helps

xstream is just a xml parser
Properties.class holds my variables


private void load(){
		xstream = new XStream();//create the stream
		xstream.alias("properties", Properties.class);
		URL propURL = null;
		propURL = getClass().getClassLoader().getResource("res/application.properties");//get the location(in the jar)
		System.out.println(propURL);
		String xml = "";

		try{
			File f = new File(propURL.toURI());//create file by the properties 
			BufferedReader inProp = new BufferedReader(new FileReader(f));
			String m = "";
			System.out.println("Reading...");
			prop = (Properties)xstream.fromXML(inProp);//parse file into the class
			System.out.println("Read");
			inProp.close();
		}catch(Exception e){
			if(e.getMessage() == null){
				JOptionPane.showMessageDialog(null, "Application is corrupted, reinstall");
				System.exit(0);
                                                                                    //ive tried to create it here, cant figure it out

			}
			e.printStackTrace();
		}
		lastDir = new File(prop.getLastLoadFolder());
		urlLink.setText(prop.getLastUrl());
	}

Is this an Applet or a Java Webstart App?

If so then you CANNOT touch the user’s filesystem in any way unless you are signed and the user grants permissions.

its a executable jar, it can also be a java webstart with all permisions

either way will work.

Alright, if its not sandbox-limited then just use java.io.File

on windows File f = new File(“C:/my folder/my_file.myfilesexstension”);

File userDir = new File(System.getProperty(“user.home”);
File myAppDir = new File(userDir,“myApp”);
if (!myAppDir.exist()) myAppDir.mkdir();
// now you’ve got a clean app temp directory

Alternative (preferred) way for small files / properties: use java.util.prefs.Preferences class

And for untrusted JWS applications : javax.jnlp.PersistanceService

Lilian :slight_smile:

thanks!

i ended up using the preferences class for this, since it is jus a small string. works perfectly.

if i ever was to store something much bigger (save game file) then id probably have to use that userDir//appDir

Please don’t put stuff randomly into the user home, it’s a mess! Better put it beside the .jar file…

please put it in the user dir. configuration, save games etc belong to the user.

while me and you might have the luxury of our own pc others don’t.
esp with games and key settings.

well non user orientated can be stuck in the classpath I suppose.

At least let the user specify something which won’t clog the user dir. Imagine if all programs did that…

well see, that was my question in the very begginng

HOW DO I PUT IT NEXT TO THE JAR FILE?

i relaly never wanted to go the registry way, but everyon keeps saying too, and now you say to do what im asking , how od i do it?