2

When I read the PATH environment variable using Java, via:

System.getenv("PATH");

The result is less comprehensive then when using the shell, like:

> echo $PATH

Result using Java:

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

Result using the shell:

/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin

Why is this? Is this some Java or some shell issue? How can I retrieve the full PATH information in Java? Do I need to take care about something more?

  • How do you run the program? Do you run it with the same user as the echo on the shell? Or ist it a cron job? – Jens May 08 '15 at 08:12
  • Did you run the java code and the command in shell, as the same user? – Anju May 08 '15 at 08:18
  • Yes, both are run using the same user. I even confirmed this in Java by running `whoami` using the `ProcessBuilder`. No cron job. The Java code runs as JUnit test case in Eclipse. – user4137911 May 08 '15 at 08:24
  • check using `System.getProperty("user.name")` – Anju May 08 '15 at 08:32
  • It sounds like it's an Eclipse issue rather than a shell issue. Might be worth tagging this question with "eclipse". Also you could try running the junit test from the command line. – GregHNZ May 08 '15 at 08:33
  • @Anju: `System.getProperty("user.name")` returns the same user as `whoami` in shell or in Java – user4137911 May 08 '15 at 08:45
  • @GregHNZ: I added the tag. It really seems to be some Eclipse issue. When running the code from the console, it returns the full path. So still the question, how do I reproduce the full path from within Eclipse? – user4137911 May 08 '15 at 08:48
  • In general a process inherits its environment (including PATH) from its _parent process_. – davmac May 08 '15 at 08:48
  • "how do I reproduce the full path from within Eclipse?" - Eclipse run configurations allow you to specify environment variables. Run -> Run Configurations... -> select your run configuration -> "Environment" tab. – davmac May 08 '15 at 08:50
  • @davmac: But I don't want to explicitly overwrite the PATH variable within Eclipse, just to be able to read this explicitly set variable. – user4137911 May 08 '15 at 09:00
  • @user4137911 Your statement is confusing, you can't read an "explictly set variable" if it's not explicitly set. If you don't want to set the PATH explicitly in the run configuration, then it seems like you are saying that you want the Eclipse environment to contain the same path as the shell. One way to achieve this would be to start eclipse _from_ the shell; otherwise you need to look at (for eg) http://stackoverflow.com/questions/135688/setting-environment-variables-in-os-x or http://stackoverflow.com/questions/603785/environment-variables-in-mac-os-x – davmac May 08 '15 at 09:06
  • @user4137911 from the second link I gave above: `launchctl setenv PATH $PATH` in the terminal will "copy" the path to launchd, which will subsequently be inherited by any process you start via the GUI. – davmac May 08 '15 at 09:17

3 Answers3

1

They should be absolutely the same especially if you say the user has been confirmed the same in both situations ( Eclipse/Java JUnit function + the shell ). However, it could still happen if you load the PATH only in the shell and was not saved yet in the general System PATH. What OS are you using ?

  • I'm using MacOS X and I'm just trying to read the PATH variable, without any modifications applied on the shell. – user4137911 May 08 '15 at 08:40
  • Yes, Mac OS X works a bit different. You need to set up the PATH in ~/.MacOSX/environment.plist so that Eclipse can read it too –  May 08 '15 at 08:55
0

Check your .bashrc/.bashprofile file. It must have defined additional values you see in shell.

shanmuga
  • 4,329
  • 2
  • 21
  • 35
0

Why is this? Is this some Java or some shell issue?

No. Both are functioning correctly. It's because the two processes (the Java process, and the shell) have a different environment and in particular a different setting for the PATH variable.

You may ask why it is different. Most likely, the shell initialisation has modified its PATH. This is usually controlled by files such as /etc/profile and ~/.profile.

The easiest way to get the same environment in your Java program (which you run from Eclipse) as in your shell is to run Eclipse from the shell instead of launching it via the Finder GUI. You also have the option of copying the shell PATH setting to the launchd settings, via the command:

launchctl setenv PATH $PATH

How can I retrieve the full PATH information in Java?

System.getenv("PATH") retrieves the full PATH for the Java process, which is inherited from (or otherwise set by) the parent process.

davmac
  • 20,150
  • 1
  • 40
  • 68