67

I am able to run a sample hadoop program from the command prompt and am trying to run the same program from Eclipse, so that I can debug it and understand it better.

For the command line program, some environment variables are set in the .bashrc and the same are being read as System.getenv().get("HADOOP_MAPRED_HOME") in the hadoop program. But, when I am running a java program with System.getenv().get("HADOOP_MAPRED_HOME"), from Eclipse I am getting null.

I tried passing -DHADOOP_MAPRED_HOME=test to VM parameters in the runtime configurations from Eclipse, but still getting null in the standalone program. How to make the environment variables visible within Eclipse? When I iterate through System.getenv() in Eclipse, I see lot of variables like DISPLAY, USER, HOME and others. Where are they set? I am using Ubuntu 11.04.

8 Answers 8

135

You can also define an environment variable that is visible only within Eclipse.

Go to Run -> Run Configurations... and Select tab "Environment".

enter image description here

There you can add several environment variables that will be specific to your application.

7
  • 14
    Max, this is applicable only for the current running Program. If you are running tests or other programs, each of those has to have the environment variable set again. Dec 28, 2012 at 13:19
  • You can do this, but probably shouldn't. It risks the environment variable's value being later changed in the OS or in Eclipse, such that the two end up contradicting each other.
    – user82216
    May 29, 2017 at 19:36
  • This is the best way to do it imho, as it allows you to give each app your are developing on it's own config, instead of having to change your global environment each time. Sep 29, 2017 at 18:45
  • 3
    But if you want to run unit tests individually, you have to set this for each test individually, which can become annoying. How does one set that for all run configurations globally?
    – Stewart
    Aug 30, 2018 at 18:31
  • 1
    But my problem is that environment variables from this tab are not being picked up
    – demongolem
    Oct 22, 2020 at 11:37
41

I've created an eclipse plugin for this, because I had the same problem. Feel free to download it and contribute to it.

It's still in early development, but it does its job already for me.

https://github.com/JorisAerts/Eclipse-Environment-Variables

enter image description here

8
  • 1
    This worked extremely well. I was fighting with a bad install of osx and unable to reinstall at this time but this plugin circumvented the problem.
    – gwnp
    Jul 18, 2017 at 15:50
  • 3
    This is such a useful plugin, you should do some finishing touch and publish it in the eclipse marketplace.
    – Dag
    Sep 6, 2017 at 6:47
  • 3
    Thank you for the plugin! Works on STS 3.9.1 (Eclipse 4.7.1a) Oct 26, 2017 at 16:08
  • 2
    Super! This really works & removed my long term headache. Thank you!
    – Karthik
    Feb 20, 2018 at 9:25
  • 2
    I've pusblished it: marketplace.eclipse.org/content/… Feb 12, 2019 at 8:27
33

The .bashrc file is used for setting variables used by interactive login shells. If you want those environment variables available in Eclipse you need to put them in /etc/environment.

5
  • 1
    Thanks - it works - just had to reboot the machine - any way to avoid a reboot. Aug 13, 2011 at 3:14
  • 4
    You can try source /etc/environment, but it's not garaunteed to replace environment variables in processes that are already running. Reboot is the safest bet.
    – Perception
    Aug 13, 2011 at 3:23
  • 4
    Is there a equivalent in Mac Os X? Dec 28, 2012 at 13:22
  • @ChandraMohan - see here for the best method to set environment variables for Mac OSX apps.
    – Perception
    Dec 28, 2012 at 16:42
  • 1
    In linux it's enough to restart the greeter (gdm, lxdm, xdm, kdm, etc). Restart the greeter from outside Xorg, open a console (for example using CTRL+ALT+F2), login and restart the greeter service. Then go back to the Xorg session with (CTRL+ALT+F1), can be another F key on your system just try everyone of them.
    – Marcs
    Feb 9, 2015 at 21:12
7

You can set the Hadoop home directory by sending a -Dhadoop.home.dir to the VM. To send this parameters to all your application that you execute inside eclipse, you can set them in Window->Preferences->Java->Installed JREs-> (select your JRE installation) -> Edit.. -> (set the value in the "Default VM arguments:" textbox). You can replace ${HADOOP_HOME} with the path to your Hadoop installation.

Select the JRE you use for running programs in Eclipse

Sending the value for hadoop.home.dir property as a VM argument

3
  • The previous solution did work for me and shows up in getenv() results. The JRE Default VM arguments does not work. I tried on Mac OS X 10.8 using Eclipse Juno.
    – Dorrin
    Sep 24, 2013 at 15:40
  • @Dorrin To access VM arguments, you have to use System.getProperty(). geetenv is for OS level variables.
    – Paddy
    Jun 15, 2016 at 1:21
  • I was looking for a "global" solution to setting environment variable at one place for all applications running within Eclipse. This helped. +1 Sep 18, 2017 at 13:41
6

You can also start eclipse within a shell.

You export the enronment, before calling eclipse.

Example :

#!/bin/bash
export MY_VAR="ADCA"
export PATH="/home/lala/bin;$PATH"
$ECLIPSE_HOME/eclipse -data $YOUR_WORK_SPACE_PATH

Then you can have multiple instances on eclipse with their own custome environment including workspace.

1

I was trying to achieve this but in the context of a MAVEN build. As part of my pom.xml configuration, I had a reference to an environment variable as part of a path to a local JAR:

<dependency>
  <groupId>the group id</groupId>
  <artifactId>the artifact id</artifactId>
  <version>the version</version>
  <scope>system</scope>
    <systemPath>${env.MY_ENV_VARIABLE}/the_local_jar_archive.jar</systemPath>
</dependency>

To compile my project, I had to define the environment variable as part of the run configuration for the maven build as explained by Max's answer. I was able to launch the maven compilation and the project would compile just fine.

However, as this environment variable involves some dependencies, the default "problems" view of Eclipse (where compilation errors/warnings usually show) would still show errors along the lines of Could not find artifact and systemPath should be an absolute path but is ${env.MY_ENV_VARIABLE}/the_local_jar_archive.jar.

How I fixed it

Go into Window -> Preferences -> General -> Worksapce -> Linked Resources and define a new path variable.

image capture of the Eclipse Preferences window

Finally, in my case I just needed to Right click on my pom.xml file, select Maven -> Update Project and the errors disappeared from the "Problems" view.

0

For the people who want to override the Environment Variable of OS in Eclipse project, refer to @MAX answer too.

It's useful when you have release project end eclipse project at the same machine.

The release project can use the OS Environment Variable for test usage and eclipse project can override it for development usage.

0

I was able to set the env. variables by sourcing (source command inside the shell (ksh) scirpt) the file that was settign them. Then I called the .ksh script from the external Tools

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.