你能从java命令行设置当前运行目录吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4442722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you set the current running directory from the java command line?
提问by user538442
I am running a Java application from the command line. Can I specify a command line argument to set the current running directory to something other than where the application is actually going to run?
我正在从命令行运行 Java 应用程序。我可以指定一个命令行参数来将当前运行目录设置为应用程序实际运行位置以外的其他目录吗?
回答by Abel Morelos
If you want to change the current directory, you'll have to use JNI and invoke a native API from your Java code. For example, for Windows you would use SetCurrentDirectory
如果要更改当前目录,则必须使用 JNI 并从 Java 代码调用本机 API。例如,对于 Windows,您将使用SetCurrentDirectory
回答by thoredge
If it all possible I would rather use a script to run the java application and set the directory in the script:
如果可能的话,我宁愿使用脚本来运行 java 应用程序并在脚本中设置目录:
#!/bin/sh
cd <your dir>
java <some arguments>
The JNI-solution may affect all kinds of relative paths in your application; for examples the classpath you put in.
JNI 解决方案可能会影响应用程序中的各种相对路径;例如你放入的类路径。
回答by Datta Rooyikar
There is a JVM argument -Duser.dir
which can be used to set working directory for JVM.
有一个 JVM 参数-Duser.dir
可用于设置 JVM 的工作目录。
回答by user2407334
I found this SO post and it helped me solve my problem. Though I wanted to share something specific about IntelliJ that might trip somebody else up.
我找到了这篇 SO 帖子,它帮助我解决了我的问题。虽然我想分享一些关于 IntelliJ 的具体内容,但可能会绊倒其他人。
I have posted a picture below where the -Duser.dir flag is used and also the Working Directory text field is filled in.
我在下面发布了一张图片,其中使用了 -Duser.dir 标志,并且还填写了工作目录文本字段。
In this situation, the Working Directory will be set to "JarLearning" rather than "2ndTry".
在这种情况下,工作目录将设置为“JarLearning”而不是“2ndTry”。