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

查看:59
本文介绍了在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具有 Preferences 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天全站免登陆