21

When I print out the path in bash, it prints this:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

When I run System.out.println(System.getenv("PATH")); in Java running under Eclipse, it prints

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

How can I figure out why there is this discrepancy? I need to add /usr/local/bin to the PATH and make it available to Java apps under Eclipse. (note: I have made no modifications system paths, so these are the defaults set by the OS or perhaps by one or more of the applications i've installed.)

Chealion
  • 26,327
Jason S
  • 7,944

12 Answers12

15

For those of you looking for an answer years later (Neon, Oxygen):

Some of my node and angular/angular2 tooling in eclipse failed due to missing $PATH entries in the MacOS terminal. Your tooling propably utilizes the embedded eclipse terminal which does not start providing your login/user shell. So you need to set the eclipse terminal in your eclipse preferences to start as --login shell in order to comprise your users PATH settings:

Go to:

Preferences -> Terminal -> Local Terminal

and set

Arguments to: --login

open a new Terminal inside Eclipse and your user's $PATH should be used from now on. Also everything you have set up in ~/.bash_profile will run when opening a new Terminal in Eclipse.

pXel
  • 103
10

If you want to set an variable system wide on OSX you need to put it in your ~/.MacOSX/environment.plist file.

See this page on the apple website for more detail.

Ted Naleid
  • 1,099
10

Had the same problem. Here is my solution:

  1. Edit the /etc/paths file to include the desired additional path.

  2. For a OSX installation, run /Applications/eclipse/eclipse not /Applications/eclipse/Eclipse.app

Oliver Salzburg
  • 89,072
  • 65
  • 269
  • 311
7

Aha, I found this webpage which says

Note: Mac OS X does not include /usr/local/bin in its default PATH. Therefore, assuming you're using the default bash shell, please type (or include in /etc/profile) export PATH=/usr/local/bin:$PATH before using any of these tools.

Is this an appropriate/inappropriate thing to do? I get leery of editing system files unless I really know what I'm doing. /usr/local/bin is only writeable by root so I wouldn't be opening a security hole, would I?


Odd, I added the export path, and it has no effect on eclipse run from the Dock. Also I found this page about paths and my /etc/paths file has

/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin

and yet those paths don't show up in eclipse... is there an easy way to create/find a binary file that prints out $PATH, that I could run from the Dock, so I could debug this more easily? I don't know if Eclipse mucks around with the path.

Jason S
  • 7,944
4

If you are on Eclipse (tested with "Mars"), you could change your "Run Configuration":

  1. Click on "Run" → "Run Configurations..."
  2. Choose the appropriate run configuration on the left
  3. Now choose tab "Environment"
  4. Click on "New..."
  5. In the "Name" field, enter "PATH"
  6. Now go to your Terminal, enter "echo $PATH" and copy the result
  7. Past the result into the "Value" field
  8. Klick "OK"
  9. Back in the "Environment" tab, make sure you select "Append environment to native environment"

Now Eclipse uses the same values for the PATH environment variable as your system does. Remember: This is only true for the run configuration you chose.

As already noted in your question, check in Java with:

System.getenv();
Mike
  • 141
3

This tip above works for me: 1) add path to /etc/paths as root 2) start eclipse with $ECL_HOME/Eclipse.app/Contents/MacOS/eclipse instead of $ECL_HOME/Eclipse.app

Kathy
  • 31
2

Most likely, one of your bash login scripts (.bashrc or .bash_profile) is changing $PATH. If you start Eclipse from the terminal, does it get /usr/local/bin in its path?

derobert
  • 4,572
2

Alternatively you might just add a symbolic link to your /usr/local directory pointing to your application.

E.g. do as root:

ln -s /Applications/PhantomJS/phantomjs-1.9.2-macosx/bin/phantomjs /usr/local/phantomjs

This helped me to run (mvn test) the external PhantomJS application from within a Maven project in Eclipse.

Dirk
  • 21
1

Eclipse NEON, mac OS X 10.11.6
The terminal in Eclipse can not use mvn command.(I make one soft link of mvn in /usr/local/bin)

Temp solution:

Right click /Applications/eclipse -> show package contents -> contents -> MacOS -> double click eclipse to start.

Start Eclipse this way , Eclipse can detect all the path settings and then I can use mvn command in Eclipse terminal now.

You can right click eclipse in this folder and make alias to start it conveniently.

I don't know why right now. I think this should be one eclipse BUG.

bluearrow
  • 111
1

You can modify the global osx path by adding a line to /etc/launchd.conf and rebooting your computer.

setenv PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Works in OSX 10.8 and 10.9

https://serverfault.com/questions/16355/how-to-set-global-path-on-os-x/277034#277034

kristi
  • 203
0

edit the ~/.bash_profile to include the path

instead of starting in the launchpad,

open the terminal and starting it as

/Applications/Eclipse.app/Contents/MacOS/eclipse

That did the trick for me.

0

Pulling together a few answers and updating to something easy:

  1. Right-click Eclipse.app and choose "Show Package Contents"
  2. Navigate to Contents/MacOS to find "eclipse".
  3. Right-click and choose "Make Alias", or drag and drop it while holding command and option.
  4. Put the alias somewhere convenient, like back in Applications.
  5. Double-click to launch.
  6. There is no Step 6!

Not only will this open a handy Terminal window showing the raw Eclipse output, it will automatically adopt whatever path you have set in your login shell.

So you can use any of the normal methods to set your path (editing /etc/paths or ~/.bash_profile or just installing well behaved command line tool like mbed), and worry no further. If you can call it from the Terminal then Eclipse can call it too.