I have a swing Java application that preserves a lot of data (you can think of a game and its saves for example). Those data are stored in files rather than in database.
I would like to keep this files near installation files(.jar file) of my application.Some users(like me) are used to delete default application folder of OS when it gets large and I don't want them to loose their data this way.
Any ideas how to do this easily ? How do I get the folder of the .jar file from program that is executing form that .jar file? Or how do I output files directly into some package? How do I create packages(folders inside jar) dynamically ? Or is there a simple way to distribute Java application开发者_Go百科 in other formats then .jar and then store generated data in installation (sub)folder ?
Thanks for reading
I would like to keep this files near installation files(.jar file) of my application.Some users(like me) are used to delete default application folder of OS when it comes to large and I don't want them to loose their data this way.
This (and the concept of multi-user systems with no write access to application directories for regular user accounts) is the reason why user data should be kept in the user's home directory, and not anywhere near the app installation folder.
The code
URL folderURL = ClassLoader.getSystemResource(".");
will provide a URL to the first location searched for resources like the class file you're currently executing.
Here is an example:
package jartest;
import java.io.File;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
private static final Pattern JARURL = Pattern.compile("jar:file:(.*)!.*");
public static void main(String[] args) {
try {
URL url = Main.class.getResource("Main.class");
Matcher matcher = JARURL.matcher(url.toString());
if (matcher.matches()) {
File file = new File(matcher.group(1));
File dir = file.getParentFile();
System.out.println(dir);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Get the location of your jar file
Firstly create a folder(say myfolder) and put your files inside it
Consider the following function
public String path()
{
URL url1 = getClass().getResource("");
String ur=url1.toString();
ur=ur.substring(9);
String truepath[]=ur.split("myjar.jar!");
truepath[0]=truepath[0]+"myfolder/";
truepath[0]=truepath[0].replaceAll("%20"," ");
return truepath[0];
}//This method will work on Windows and Linux as well.
//You can alternatively use the following line to get the path of your jar file
//classname.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Suppose your user put the jar file in D:\Test\dist
Then path() will return /D:/Test/dist/myfolder/
Now you can place 'myfolder' inside the folder where your jar file is residing
This way you can access your files inside myfolder
Steps for distribution
Download Inno Setup Compiler from http://www.jrsoftware.org/isdl.php and run it
Select 'Create a new script file using script Wizard' option Fill in your application details
Next browse your jar file(Select all files option of Open dialogue box to see your jar file)
And of cource add your 'myfolder' to Other application files section(Add folder)
Follow the remaining instructions
You are a go.
精彩评论