How to set the PATH environment variable for JVM

眉间皱痕 提交于 2020-01-04 23:02:59

问题


I am trying to run executables which are installed on my system with the Java 7 ProcessBuilder. I noticed that the environment variable PATH, which is available via

 System.getenv("PATH");

does not include my own, custom set path. It returns this:

 /usr/bin:/bin:/usr/sbin:/sbin

My path looks like this:

 /Users/saschaf/.bin:/Users/saschaf/Entwicklung/spring-roo-1.2.4.RELEASE/bin:/usr/local/opt/ruby/bin:/usr/local/bin:/Users/saschaf/Entwicklung/android-sdk-macosx/tools:/Users/saschaf/Entwicklung/android-sdk-macosx/platform-tools:/usr/local/share/npm/bin:/Users/saschaf/node_modules/.bin:/Users/saschaf/Entwicklung/git/tools:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

I don't know how to set the PATH variable so that the JVM uses the correct one. Whats the problem here?

I am running the latest OS X Mavericks, JDK 1.7.0_25-b15, Maven Apache Maven 3.1.1.


回答1:


Straight from the documentation :

You can run the JDK just fine without setting the PATH variable, or you can optionally set it as a convenience. However, you should set the path variable if you want to be able to run the executables (javac, java, javadoc, and so on) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable every time you run it, such as:

% /usr/local/jdk1.7.0/bin/javac MyClass.java

To find out if the path is properly set, execute:

% java -version

This will print the version of the java tool, if it can find it. If the version is old or you get the error java: Command not found, then the path is not properly set.

To set the path permanently, set the path in your startup file.

For C shell (csh), edit the startup file (~/.cshrc):

set path=(/usr/local/jdk1.7.0/bin )

For bash, edit the startup file (~/.bashrc):

PATH=/usr/local/jdk1.7.0/bin:
export PATH

For ksh, the startup file is named by the environment variable, ENV. To set the path:

PATH=/usr/local/jdk1.7.0/bin:
export PATH

For sh, edit the profile file (~/.profile):

PATH=/usr/local/jdk1.7.0/bin:
export PATH

Then load the startup file and verify that the path is set by repeating the java command:




回答2:


You can pass your shell's $PATH as a command line parameter:

$ java -DPATH=$PATH -cp …

You can use this example to examine your environment.



来源:https://stackoverflow.com/questions/21094498/how-to-set-the-path-environment-variable-for-jvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!