在 Java Webstart 中更改工作目录 [英] Change working dir in Java Webstart

查看:31
本文介绍了在 Java Webstart 中更改工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在运行 Java Webstart 时更改 JVM 的工作目录?当我使用 system.setProperties("user.dir", newDir) 它设置它(system.getProperties() 显示它)但忽略它.

Is there a way to change working dir for JVM when running Java Webstart? When i use system.setProperties("user.dir", newDir) it sets it(system.getProperties() shows it does) but ignores it.

这是 Java Webstart 中始终使用我启动 jnlp 文件的工作目录的限制吗?我正在使用 jnlp 文件中的所有权限.

Is this a limitation in Java Webstart to always use the working dir where i started the jnlp file? I am using all permissions in the jnlp file.

请帮忙!

无论我现在做什么,我的 webstart 总是使用 user.dir 来保存文件.令人沮丧!

Whatever i do now, my webstart always uses user.dir to save files. Frustrating!

推荐答案

我自己过去也有过这个问题,但我总是发现,最后,我不需要它.我为什么要这样说?

I've had this question in the past myself, but I've always found that, in the end, I didn't need it. Why do I say this?

因为您的 java web start 应用程序不像从 Program Files 运行的可执行文件.它甚至不像大多数程序那样存在于您的计算机上(它被分解成一堆不同的文件并由 JVM 重新组装).因此,你不能说程序有自己的目录来做它需要的事情.

Because your java web start app is not like an executable run from Program Files. It doesn't even exist on your computer like most programs (it is broken up into a bunch of different files and reassembled by the JVM). Therefore, you cannot say that the program has its own directory to do what it needs.

但它不需要.原因如下:

But it doesn't need to. Here's why:

  1. Java 具有 首选项 API 在您需要存储数据时提供帮助.Preferences API 的工作原理和 JWS 一样神秘,因此它们真的很合适.无论哪种方式,如果您需要将内容写入文件,您都应该检查此 API,看看它是否能满足您的需求.
  2. 如果您需要为用户写文件,那么提示他们并允许他们选择位置显然意味着您不会使用当前工作目录来读/写文件.
  3. 如果您需要序列化对象,您应该只使用@AndrewThompson 建议的 user.home 资源创建一个程序目录.这将是您的"目录,并且与 Program Files 中的目录一样好(实际上更好)(例如,如果您在 Windows 上运行).
  1. Java has the Preferences API to help when you need to store data. The under-workings of the Preferences API is as mysterious as JWS, thus they are really a perfect fit. Either way, if you need to write things to a file, you should check this API to see if it can meet your needs.
  2. If you need to write files for the user, then prompting them and allowing them to choose the location obviously means you won't use your current working directory to read/write files.
  3. If you need to serialize objects, you should just create a program directory using the user.home resource as @AndrewThompson suggested. This will be "your" directory and is as good (in fact, better) than a directory in Program Files (if you're running on Windows, as an example).

总而言之,在所有情况下(我遇到过),都不需要更改您当前的工作目录.如果您需要自己的文件夹,请在 user.home 中创建一个(因为您不会在那里遇到文件权限问题).

In conclusion, in all cases (that I've come across), there's no need to change your current working directory. If you need your own folder, create one in user.home (because you won't run into file permissions issues there).

这篇关于在 Java Webstart 中更改工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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