在 Java Web Start 中读取文件

发布于 2024-09-25 19:13:53 字数 289 浏览 0 评论 0原文

我有一个应用程序,其中有一些第三方库,需要一些配置文件存在。我将它们放入 jar 文件中,以便可以使用 JWS 进行部署。我知道您可以使用 getResourceAsStream() 从 jar 文件中读取文件,但 3rd 方库不会这样做。我尝试使用 getResourceAsStream 从 jar 中读取它们并将实际文件写入 user.dir 。这样应用程序就可以正常工作。然而,似乎大多数用户都是从桌面上的快捷方式启动应用程序,所以他们的 user.dir 当然是桌面。他们宁愿不要在桌面上乱扔配置文件。

我有更好的方法可以处理这个问题吗?

I have an application that has some 3rd party libraries that require some configuration files to exist. I put these in jar files so they can be deployed using JWS. I know that you can read files from the jar file using getResourceAsStream(), but the 3rd party libraries don't do that. I have tried reading them out of the jar with getResourceAsStream and writing the actual files to user.dir. The application works fine that way. However, it seems that most of the users launch the application from a shortcut on the deskop, so of course their user.dir is the desktop. They would rather not litter their desktop with configuration files.

Is there some better way I can handle this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋叶绚丽 2024-10-02 19:13:53

您实际上可以更改您的 user.dir。我认为一开始它是“不可触碰的”。

因此,首先您需要使其“可触摸”

我们使用类 MainWindow 的静态块调用以下内容

static {
  try {
    Class clazz = ClassLoader.class;
    Field field = clazz.getDeclaredField("sys_paths");
    boolean accessible = field.isAccessible();
    if (!accessible) {
      field.setAccessible(true);
    }
    field.set(clazz, null);
    try {
      String currentDir = System.getProperty("java.library.path");
      //now remove the current directory from the library path.
      String sansCurrentDir = removeCurrentDirectoryFromPath(currentDir);
      System.setProperty("java.library.path", sansCurrentDir );
    }
    finally {
      field.setAccessible(accessible);
    }
 } catch (Exception e) {
   //in our case we rethrow
 }
}


//and then later in our code in the same static block we can now create our root directory (ie, the user.dir) and execute
System.setProperty("user.dir", rootDirectory);

You can actually change your user.dir. I think it is "untouchable" at first.

So first you need to make it "touchable"

We invoke the following using a static block for our class MainWindow

static {
  try {
    Class clazz = ClassLoader.class;
    Field field = clazz.getDeclaredField("sys_paths");
    boolean accessible = field.isAccessible();
    if (!accessible) {
      field.setAccessible(true);
    }
    field.set(clazz, null);
    try {
      String currentDir = System.getProperty("java.library.path");
      //now remove the current directory from the library path.
      String sansCurrentDir = removeCurrentDirectoryFromPath(currentDir);
      System.setProperty("java.library.path", sansCurrentDir );
    }
    finally {
      field.setAccessible(accessible);
    }
 } catch (Exception e) {
   //in our case we rethrow
 }
}


//and then later in our code in the same static block we can now create our root directory (ie, the user.dir) and execute
System.setProperty("user.dir", rootDirectory);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文