Java:如何知道 jar 位于 Linux 中的位置
在 Linux 中,JVM 将工作目录设置为用户的主文件夹。 在 Windows 中,这是 jar 所在的文件夹。 如何找到 jar (来自我的应用程序) 所在位置以更改工作目录?
马丁
In Linux, the JVM sets the working direcory equal to the homefolder from the user.
In Windows is this the folder where the jar is located.
How can I find where the jar (from my app) is located to change the working dir?
Martijn
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用以下代码获取 jar 的路径:
然后您可以使用以下方法删除“*.jar”部分:
当我需要在 jar 所在的同一目录中打开文件时,我在 java 游戏中遇到了同样的问题。
linux下双击jar打不开文件。当然,您可以打开命令行并将目录更改为 jar 目录,然后从那里运行 jar,但我需要双击该 jar。
Try obtaining the path to the jar with this code:
Then you can remove the "*.jar" part with:
I had the same problem with a java game when I needed to open a file in the same directory the jar was.
Double clicking the jar in linux didn't open the file. Of course you can open a command line and change dir to the jar dir and then run the jar from there but I needed to double click the jar.
在这两种情况下,它都是实际的目录。在 Windows 下,如果您从资源管理器执行它,实际目录会自动设置为 jar 的基本目录。在 Linux 下,主目录是 GUI 的实际目录。要设置目录,请编写一个简短的 shell 脚本,该脚本设置实际目录(使用 cd 命令),然后执行 jar(使用 javaw -jar xyz.jar)。
In both cases it is the actual directory. Under windows automatically the actual directory is set to the base-dir of the jar, if you execute it from explorer. Under linux the home-directory is the actual directory for your GUI. To set the directory write a short shell-script, which sets the actual directory (with the cd-command) and then executes the jar (with javaw -jar xyz.jar).