31

My company provides an eclipse based development environment which needs some environment variables setting up for the underlying toolchain so multiple versions can be installed concurrently and not take over the system.

I want to provide an icon in finder or the dock which sets these then launches eclipse so customers cannot accidentally launch eclipse without the environment being set. This is what I have tried so far:

  1. Setting environment in Info.plist for eclipse:

    This should be a nice way to do it but I cannot make it add to the existing path (like export PATH=/myapp/bin:$PATH).

  2. bash script wrapping eclipse:

    I created a bash script called eclipse.command to set the environment then launch eclipse. This opens a terminal window as well as the eclipse icon and allows people to "Keep on dock" for the bare eclipse. I cannot put eclipse.command on the dock as it is not an application.

  3. Applescript wrapping eclipse.command:

    An Applescript wrapper around eclipse.command makes it look like an app and prevents the terminal window appearing. Unfortunately I now get a dock icon for the applescript and one for eclipse so can still keep the bare eclipse on the dock.

Any suggestions? Am I going about this in completely the wrong way?

9 Answers9

47

There is an alternate solution which involves replacing the executable that is run by MacOS X when the user launches the Eclipse application with a shell wrapper that sets up the environment.

Create an empty text file called "eclipse.sh" in the Eclipse application bundle directory /Applications/eclipse/Eclipse.app/Contents/MacOS.

Open the eclipse.sh in a text editor an enter the following contents:

#!/bin/sh

export ENV_VAR1=value
export ENV_VAR2=value

logger "`dirname \"$0\"`/eclipse"

exec "`dirname \"$0\"`/eclipse" $@

In the example ENV_VAR1 and ENV_VAR2 are the environment variables being set up. These variables will be visible to processes launched from within Eclipse. The logger command will just log the path of the eclipse executable to the system.log as a debugging aid.

In the Terminal set the executable flag of the shell script eclipse.sh, i.e.:

chmod +x /Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse.sh

Open the Eclipse.app Info.plist and change the value for the key CFBundleExecutable from eclipse to eclipse.sh.

MacOS X does not automatically detect that the Eclipse.app's Info.plist has changed. Therefore you need to force update the LaunchService database in the Terminal by using the lsregister command:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -v -f /Applications/eclipse/Eclipse.app

The next time you launch Eclipse.app from the Dock or from the Finder the environment variables should be set.

sakra
  • 62,199
  • 16
  • 168
  • 151
  • 2
    Simply the best option by a wide margin! Thank you very much, works flawlessly!! – Alexandre L Telles Sep 08 '09 at 18:22
  • 1
    This does not appear to work any longer, at least under OSX Lion with Eclipse Indigo (3.7). – John Gardner Feb 25 '12 at 03:51
  • 1
    Thx for this answer! Since Eclipse Mars one needs to use `/Applications/Eclipse.app`: `/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -v -f /Applications/Eclipse.app` – NextThursday Jun 26 '15 at 12:31
  • 3
    I do not like this solution because it does not make my suite easily usable by everyone involved in the project unless they do this on their own machine. It is absolutely ridiculous that OSX has no reliable method of passing environmental variables to the launched application. I do like this solution, because it is the ONLY thing that has worked for me. – Matt Clark Jul 22 '15 at 14:34
  • @MattClark if it helps, you can easily script a solution to make the wrapper. [Here is mine](https://gist.github.com/e40/21d6266385958165e86f01f554a5e62b). – e40 Dec 13 '17 at 10:09
  • I'm baffled I'm still doing this in >[Current Year]. Still works, thanks. – redeveloper Jan 21 '20 at 03:10
8

I created the following:

alias start-eclipse='open /Applications/eclipse/Eclipse.app'

If you run start-eclipse from the command line, all env vars will be picked up. This way, you only need to maintain a single set of env vars across both command-line and eclipse environments.

Chris Fregly
  • 1,490
  • 1
  • 12
  • 8
  • This is not ideal, but it did the trick for me with OSX Lion and Eclipse 3.7.1. – John Gardner Feb 25 '12 at 03:52
  • An ideal solution wouldn't have any tradeoffs. – pohl Mar 05 '13 at 17:29
  • Well, the chosen answer certainly has trade offs: you have to maintain different sets of environment variables across command-line and Eclipse environments. This answerer mentions this. Plus, this is a very simple/easy solution. – mannyglover Jul 12 '18 at 20:14
  • @Chris Fregly Do we have to restart eclipse if we change an environment variable and want it to take effect? – Aryan Venkat Apr 08 '19 at 11:21
6

Take a look at a related question: Environment variables in Mac OS X.

Basically, this involves the creation of a ~/.MacOSX/environment.plist file.

Log out and Log in for the environment.plist to get picked up by .App's

Community
  • 1
  • 1
mouviciel
  • 66,855
  • 13
  • 106
  • 140
4

This worked perfectly in OS X Yosemite:

  1. Open /Applications/Automator.
  2. When the drop-down appears asking you what kind of document you want to create, choose "Application."
  3. In the second-from-the-left list, double-click "Run Shell Script."
  4. In the right side delete the "cat" that gets put there automatically, and replace it with this:

    source ~/.bash_profile && /Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse
    

Now go to File->Save, and save the application to your Applications directory. I named it "Eclipse" with a capital 'E' so as not to conflict with the "eclipse" directory I already had. For good measure, you can even give it the Eclipse icon by selecting the real eclipse app, pressing command-i, selecting the icon, pressing command-c, then selecting the automator "Eclipse" app, pressing command-i, selecting the icon, and pressing command-v.

Now you can open the app, or even drag it to your dock. Note that if you start it, the "real" eclipse will still show up in your dock as a separate icon, but you can't have everything. :)

automatom
  • 275
  • 2
  • 10
3

sakra's answer above is awesome, except is doesn't automatically inherit your existing bash environment. To ensure eclipse.sh picks up your existing bash environment, modify eclipse.sh to use bash instead of sh and add a line to source your existing ~/.bash_profile thus:

#!/bin/bash
source ~/.bash_profile
logger "`dirname \"$0\"`/eclipse"
exec "`dirname \"$0\"`/eclipse" $@
3

None of the above worked for me. you have to set Eclipse -> Preferences -> Terminal -> Arguments set to --login That will instruct Eclipse to login with your account just after opening Terminal.

See screenshot:

enter image description here

Reference: https://marketplace.eclipse.org/comment/4259#comment-4259

Ahmed Hammad
  • 622
  • 8
  • 17
1

Link to Eclipse doesn't use the path set in .bashrc

  1. Create simple script
#!/bin/bash
source /home/user/.environment_variables
/home/user/eclipse_cpp/eclipse -Duser.name="My Name"

2. Next put your all system variables in file /home/user/.environment_variables (any file you want)

My looks like:

export COCOS_ROOT=/home/user/Projects/edukoala
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/

3. Now you can delete your variables in .bashrc and put line

source /home/user/.environment_variables

Everything works fine :)

Community
  • 1
  • 1
Gelldur
  • 11,187
  • 7
  • 57
  • 68
1

As pointed out in https://github.com/atom/atom/issues/7045, the environment variables can be loaded automatically, without explicit source ~/.bash_profile by using

#!/usr/bin/env bash -l

instead of

#!/bin/bash
source ~/.bash_profile

after that, in both cases, follows

exec "`dirname \"$0\"`/eclipse" $@

It works great for me, thanks for all previous work.

0

After setting env variables in .bash_profile. Simply open the application through terminal!

open /Application/{path/to/app}.app