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.)

12 Answers 12

14

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.

4
  • Best solution, solves the problem just by changing eclipse configuration in GUI.
    – pXel
    Sep 12, 2017 at 12:22
  • This answer made me upgrade to Oxygen, and that was all I needed to make it work.
    – John
    Apr 12, 2018 at 13:30
  • I do not have the "Terminal" submenu in Eclipse with version string "2019-06 (4.12.0)".
    – tsj
    Sep 6, 2019 at 13:14
  • @tsj You need to install "TM Terminal" plugin from Eclipse marketplace.
    – Ibrahim.H
    Feb 4, 2021 at 8:47
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.

1
  • 1
    Unfortunately, this doesn't seem to work for me.
    – thSoft
    Nov 19, 2012 at 22:44
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

4
  • 1
    omg number 2 on here was the solution to SO MUCH FRUSTRATION. Thanks!!
    – Tommy
    Oct 2, 2013 at 16:39
  • 1
    I registered to this website just so I could upvote this answer - item #2 fixed it!
    – Iftah
    Jul 6, 2016 at 8:28
  • Where can I find the etc/paths?
    – John
    Apr 12, 2018 at 12:37
  • 1
    @John /etc/paths is the full path of the file. You can do Cmd+Shift+G and then put "/etc" into it and press enter/return. You must find the file there named "paths".
    – Sufian
    May 30, 2018 at 10:56
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.

2
  • 3
    +1 for your final question. I'm wondering how to diagnose path issues in OS X not in bash or the Terminal.
    – fideli
    Jan 21, 2010 at 0:19
  • Note for future Googlers: Apple now includes /usr/local/bin first in users' $PATH. (See here.)
    – Telemachus
    Dec 1, 2018 at 15:47
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();
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

1
  • This one works for me, thank you very much! In my case, the path to run Eclipse is /Applications/Eclipse.app/Contents/MacOS/eclipse Feb 9, 2017 at 23:24
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?

2
  • 1
    good call! If I run Eclipse from the terminal, it has /usr/local/bin in the path. Now what should I do? Isn't /usr/local/bin something that should be in the path to all applications? which script can I put this in, so that if I run Eclipse from Finder or from the Dock, it will be in the path?
    – Jason S
    Aug 23, 2009 at 19:32
  • 1
    Also I don't have a .bashrc or .bash_profile in my user directory.
    – Jason S
    Aug 23, 2009 at 19:33
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.

2
  • This does not seem to have anything to do with $PATH or directories in $PATH like /usr/local/bin. Could you please explain in more details? Oct 18, 2013 at 10:00
  • you put a symbolic link somewhere in the path, that points to the actual location which is not in the path. I've done it before, it works for single executables, doesn't work for a directory full of files
    – Jason S
    Oct 18, 2013 at 19:37
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

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.

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .