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

前端 未结 4 1159
温柔的废话
温柔的废话 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.

    0 讨论(0)
  • 2021-01-06 17:43

    You can use

    AClass.class.getResource(String str);
    //or
    AClass.class.getResourceAsStream(String str);
    

    AClass: one of your classes.

    str: file location which you want to read.

    For example;

    if your class hierarchy seem like this:

    +src
        +-com
             +-test
                 |-AClass.java
                 +-util
                     +-PrintUtil.java
                 +-resources
                     |-Bouble.png
                     |-Mouse.png
                     +-Ocean.png
    

    and for reading "Mouse.png" image, you can this with a lots of ways:

    AClass.class.getResource("/resources/Mouse.png");
    //or
    PrintUtil.class.getResource("../resources/Mouse.png");
    ...
    
    0 讨论(0)
  • 2021-01-06 17:44

    You can't write inside a jar file while you are using/running the jar file. When you put the jar file in you classpath or you run the program from jar directly, the jar will be locked by your jvm, hence it won't allow you to update the same jar file which you are currently using.


    The solution given by people which says use resource as stream will work if your classes are there in a folder, not in an archive (which you are using).


    As an archive you can't directly update it, you need to do following steps (by yourself or by 3rd party api),
    Extract in temp location
    update the files
    re archive

    Now as the jar file is locked, you won't be able to do the third operation, which is not even safe. As an example when you are running a jar file, try to rename it, it won't happen, if it happens, the jar file is not yet locked by the jvm, it gets locked whenever you call a class which is inside the jar file.
    For better and secure serialization and file saving please look into this: java.util.prefs.Preferences.

    0 讨论(0)
  • 2021-01-06 17:59

    You need your JAR to use the same path for reading the Serialized Files as your code in eclipse.

    So you make a properties file containing the directory with your serialized objects. Then, this is the same for both your JAR and our project.

    See also: http://www.mkyong.com/java/java-properties-file-examples/

    0 讨论(0)
提交回复
热议问题