Serialized files don't work when project is converted to executable jar?

前端 未结 4 1187
温柔的废话
温柔的废话 2021-01-06 17:34

I made my java project into an executable jar using the export to jar option in eclipse. The jar runs as expected, except that it does not use any of the serialized files. I

4条回答
  •  甜味超标
    2021-01-06 17:39

    Answering this question:

    How do I create some kind of structure in which I pack my executable jar and its serialization folder ?

    A common approach is to have a well-defined place to store serialized files, settings, etc, that does not depend on where the program has been executed from. Usually it is user's home directory, or Application Data in case of windows. I used this code to store my application settings:

        String home = System.getenv("APPDATA");
        if (StringUtils.isEmpty(home)) {
            home = System.getProperty("user.home");
        }
        CONFIG_HOME = new File(home, ".myProgram").getAbsoluteFile();
        CONFIG_HOME.mkdirs();
    

    So on windows it will use AppData and on *nix systems it will use user's home. The dot in front of myProgram is to make it hidden on *nix platforms, which is a common practice.

    EDIT For your question in your comment:

    on my linux machine there is no APPDATA env variable so this code will create a directory /home/myUser/.myProgram. On windows it will be something like c:/Users/myUser/AppData/Local/.myProgram. On MacOSX, no idea.

提交回复
热议问题