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

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

提交回复
热议问题