Get location from where the Java code is executed

左心房为你撑大大i 提交于 2019-12-10 20:11:35

问题


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 in other formats then .jar and then store generated data in installation (sub)folder ?

Thanks for reading


回答1:


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.




回答2:


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.




回答3:


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();
        }
    }
}



回答4:


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.



来源:https://stackoverflow.com/questions/2682194/get-location-from-where-the-java-code-is-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!