简体   繁体   English

设置主目录或工作目录

[英]Set home or working directory

I have a java application and have bundled it as App.jar . 我有一个Java应用程序,并将其捆绑为App.jar There are some third party tools I have used (lets call it NumberGenerator ). 我已经使用了一些第三方工具(将其NumberGenerator )。 App.jar starts a process and calls NumberGenerator to get the output. App.jar启动一个进程,然后调用NumberGenerator以获取输出。 To refer the executable, I have used relative paths new File("lib/NumberGenerator.exe") and it works all well. 为了引用可执行文件,我使用了相对路径new File("lib/NumberGenerator.exe") ,它运行良好。

Now on Mac, i have bundled the application using this and it automatically generates an application launcher. 现在在Mac上,我使用此工具捆绑了应用程序,它会自动生成一个应用程序启动器。 When I run by clicking at launcher, it launches the application. 当我单击启动器运行时,它将启动应用程序。 But it sets the home directory as ~ ie /Users/Jatin and not where the jar file was lying. 但是它将主目录设置为~/Users/Jatin而不是jar文件所在的位置。 Hence my application is unable to detect the lib folder (Obviously because it doesn't lie in that location) 因此,我的应用程序无法检测到lib文件夹(显然是因为它不在该位置)

In my Java code, how do I set the home folder as where my jar was lying? 在我的Java代码中,如何将主文件夹设置为jar所在的位置?

I had a similar problem with linux. 我在linux上也有类似的问题。 I don't know much about osx not AppBundler but in linux I solved it by creating this script 我不太了解osx,而不是AppBundler,但是在linux中,我通过创建此脚本解决了该问题

#!/usr/bin/env sh
java -Duser.dir=$(pwd) -jar myapp.jar

It may work if you manage to run the script by double clicking on it... see How to run a shell script in OS X by double-clicking? 如果您通过双击脚本来运行脚本,则可能会起作用。请参阅如何通过双击在OS X中运行Shell脚本?

return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());

只需检查此线程如何获取正在运行的JAR文件的路径?

I see two approaches: 我看到两种方法:

  • In Info.plist , specify a java.library.path relative to $JAVAROOT , as described in the articles cite here . Info.plist ,指定java.library.path相对于$JAVAROOT ,如在文章中描述引用在这里

     <key>Java</key> <dict> ... <key>Properties</key> <dict> <key>java.library.path</key> <string>$JAVAROOT/</string> </dict> ... </dict> 
  • Use Java Web Start , which lets you manage native resources by OS and architecture. 使用Java Web Start ,它使您可以按操作系统和体系结构管理本机资源

您可以尝试以下方法:

System.setProperty("user.home", "your-dir");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM