当项目转换为可执行jar时,序列化文件不起作用? [英] Serialized files don't work when project is converted to executable jar?

查看:137
本文介绍了当项目转换为可执行jar时,序列化文件不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用eclipse中的export to jar选项将我的java项目导入到可执行的jar中。该jar按预期运行,除了它不使用任何序列化的文件。我可以从GUI中清楚地看到。可能是这个问题的原因,我该如何解决?



我看到这个相关问题 - 为什么我的JAR文件不能创建序列化?





如何创建某种结构,我的可执行jar及其序列化文件夹?

解决方案

回答这个问题:



< blockquote>

如何创建某种结构,我可以在其中包装可执行的jar及其序列化文件夹?


一个常见的方法是有一个明确定义的地方来存储序列化的文件,设置等,这些程序不依赖于程序执行的位置。通常它是用户的主目录,或者Windows应用程序数据。我使用这段代码存储我的应用程序设置:

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

所以在Windows上,它将使用AppData和* nix系统,它将使用用户的家。 myProgram前面的点是使它隐藏在* nix平台上,这是一个常见的做法。



编辑评论:



在我的linux机器上没有APPDATA env变量,所以这个代码将创建一个目录 /home/myUser/.myProgram 。在Windows上,它将像 c:/Users/myUser/AppData/Local/.myProgram 。在MacOSX上,不知道。


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 can see that clearly from the GUI. What could be the reason for this problem and how do I fix it ?

I saw this related question - Why does my JAR file not create a serialization?

But it does not tell me exactly how to get around this problem. It looks like you cannot pack a folder into a jar. Why ? Because code could accidentally/intentionally continue to add data into that folder and make the whole jar occupy the hard disk ?

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

解决方案

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.

这篇关于当项目转换为可执行jar时,序列化文件不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆