Environment variables in Eclipse - java

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.

You can also define an environment variable that is visible only within Eclipse.
Go to Run -> Run Configurations... and Select tab "Environment".
There you can add several environment variables that will be specific to your application.

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

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.

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.

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.

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

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.

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

Related

IntelliJ IDEA does not see my environment variables

I've configured an environment variable in ~/.bashrc as well as ~/.profile.
When I run my application via IDEA's Gradle Configuration, my environment variable is apparently not available. E.g. I am referencing this variable within application.yml, but the default value is being used instead. Running Gradle from the command line correctly picks up my variable.
How can I configure IDEA to load this environment variable in a global way, so I don't have to manually add it to every project where I need it (~20 projects)?
Note: running on Manjaro Linux v18.
It turns out IDEA will pick up environment variables if you define them either in e.g. /etc/environment or /etc/profileor if you use ZSH,~/.zshrc`.
After setting variables, you to execute source ~/.bashrc and source profile to update and execute the file contents.
# josh-m I ran into the same problem in my IntelliJ IDEA setup with Maven.
In the end, I continued to keep the settings on each project, as there were not many, but when I studied the problem I found this IntelliJ support link:
https://youtrack.jetbrains.com/issue/IDEA-141832
At least at that time, this functionality was not available in IntelliJ directly, although the following plugin, mentioned in the comments of the support article, could perhaps be of some help:
https://plugins.jetbrains.com/plugin/7861-envfile/
launchctl setenv HADOOP_VERSION 3.1.1.7.2.8.0-SNAPSHOT
Execute in this way to set your environment variables in MacOS for them to be used by other applications.
After that restart your IDE.

Ensuring MAVEN_HOME is set correctly

A Java and Maven noob here:
Using OSX 10.8
And Installed Maven with HomeBrew
1- If I say which mvn I will get this: /usr/local/bin/mvn
2- If I say echo $MAVEN_HOME I will get nothing back.
3- If I go to my Maven Installation on EClipse it will look like this:
4- If I go to Maven settings in IntelliJ, it will look like this: So far I have been able to run Maven goals with IntelliJ.
Ok That's all I have. Can you please take a look and see what are the inconsistencies I am having and what things to fix to set this correctly everywhere?
Under Mac OS X you should setup your environment variables in /etc/launchd.conf. Variables loaded by launchd are available in your shell as well as in GUI applications like Intellij IDEA.
Intellij IDEA will use M2_HOME to find the Maven installation. Edit or create /etc/launchd.conf:
setenv M2_HOME /usr/share/maven
Unfortunately to see the effect, you have to reboot your machine. Another shortcoming is, that you cannot use spaces in the file. This means, you cannot set MAVEN_OPTS to more than one value.

How to set a JVM option in Jenkins globally for every job?

I've recently installed a new JDK (1.7u9), and I got some very strange VerifyErrors. In a thread I found that it could help me if I use a -XX:-UseSplitVerifier magic switch for the compilation.
What I would like to do is to set this Java option globally in Jenkins, but haven't found any configurations for it. Can someone help me out how can I do this?
The closest thing I was able to come up with is to set the argument through Maven, but I have to do it for each project configuration - and I'd like to avoid that.
Thanks in advance.
Under the main menu item Manage Jenkins->Configure System you can set it in the box for Global MAVEN_OPTS.
It is a bit unclear whether you want the option turned on for the Jenkins container itself or only the jobs running in it, but if the latter and you're only running maven jobs, that's what I would do.
Cheers,
If you deploy the Jenkins to the Tomcat or Glassfish, I would like to suggest you to set further configuration as the following:-
The Tomcat
Set the environment variable named CATALINA_OPTS, e.g.
SET CATALINA_OPTS="-XX:-UseSplitVerifier"
EXPORT CATALINA_OPTS
The Glassfish
Edit the [your_domain]/config/domain.xml
<java-config ....>
....
<jvm-options>-XX:-UseSplitVerifier</jvm-options>
</java-config>
Anyhow if you deploy it to another application server, please refer to your application server administrator guide to configure further JVM options.
UPDATED:
If you only would like to apply this JVM option to the Maven project, please set the environment variable named MAVEN_OPTS, e.g.
SET MAVEN_OPTS="-XX:-UseSplitVerifier"
export MAVEN_OPTS
I hope this may help.
Regards,
Charlee Ch.
On Windows there's a jenkins.xml in Jenkins home directory. Simply add the required JVM options under arguments tag:
<arguments>
-Xrs -Xmx256m -XX:-UseSplitVerifier
-Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle
-jar "%BASE%\jenkins.war" --httpPort=8080
</arguments>
For most of the Linux distributions, modify JENKINS_ARGS inside file:
/etc/default/jenkins (or jenkins-oc)
For CentOS, modify JENKINS_JAVA_OPTIONS inside file:
/etc/sysconfig/jenkins (or jenkins-oc)
Apparently, the only way to set system-wide JVM properties in Jenkins is with a Groovy Script .
Create a init.groovy.d in Jenkins home and place a groovy file in it (load-properties.groovy). In the Groovy script, set system properties programmatically (see link above for details):
props.each { key, value ->
System.setProperty(key, value)
The above solution saved my day as I needed to disable jsse.enableSNIExtension during SCM checkout and it should be available to the SVN plugin, not to Maven.
There's a config.xml file with jdks/jdk/properties XML tags, but it's undocumented.

How to set Environment Variable in Netbeans?

I am running maven based Spring project in netbeans . I am using an environment variable in my project configurations file to Open specific file based on that variable value . I am able to set it In Eclipse but do not know how to set in Netbeans . Can any one help me?
System.getEnv("FOO") == "FOOVALUE"
netbeans 6.7+ -
Right click Project
->Properties
->Actions
->Run project
->Set Properties: Add
Env.FOO=FOOVALUE
Note: You can apply the same technique on other configurations and other actions like Debug project
I could not find that Properties->Actions setting, using NetBeans 7.4. I did not explicitly need an environment variable--just some value I could set outside my application. So, instead, I was able to make a change in the "project.properties" file under the nbproject folder of the main application.
Within project.properties is a "run.args.extra" setting. Any command line argument you wish to pass along must have its 'name' preceded by '-J'. Here is an example.
run.args.extra=-J-DFOO=FOOVALUE
Note that this is NOT an environment variable. Rather, it is a System.getProperty("FOO") accessible value. Of special note: this is also the place to change how much memory your applications allocate, using Xmx/Xms.
On macOS:
In $HOME/.bashrc add alias:
alias netbeans='/Applications/NetBeans/NetBeans\ X.Y.app/Contents/MacOS/netbeans &'
In Terminal:
> netbeans
This will start IDE with user environment variables.
As result environment variables (such as $PATH) available inside NetBeans IDE and tools (such as Ruby debugger).

Does intellij run maven as a different user?

I have configured the enforcer to check for some properties. These are exported as environment variables in my .profile file in my home dir. The build from the console runs fine.
The build from intellij (doubleclicking the goals in the maven menu) fails, as enforcer cannot find the properties...What is going wrong here and how to fix it?
My first guess is that intellij is running maven as a different user but I'm not sure how to verify it or what to do about it.
NOTE: I am not asking for a workaround. One would be to set the properties in maven Runner settings of intellij, a second - to set the properties in /etc/profile. But I would like to avoid them.
EDIT : I have tried restarting intellij, the X server and the machine - to no effect on the enforcer. printenv in a fresh console shows my variables as expected. Starting intellij from the same console leads to the same build failure.
Thank you.
Each program has its own environment. If you change the environment settings in .profile or the like, it doesn't change the setting for running programs. When one program runs another program it inherits the settings/environment of its parent. It doesn't load the settings again.
What you could be finding is that changes you make after IntelliJ has started, have no effect from programs IntelliJ runs. The same thing would happen if you opened a command window, changed the settings and run a new programs (the shell would keep the settings it had when it started) To fix this you need to re-start IntelliJ. If you run it from the command line, make sure its a new window.
This unit test should dump the environment.
#Test
public void dumpEnv() {
for (Map.Entry<String, String> entry : new TreeMap<String, String>(System.getenv()).entrySet()) {
System.out.println(entry);
}
}
.profile is only executed when you log on using KSH. If you use BASH, it has no effect.
.bashrc (if you use BASH) is a much more suitable place because it's executed every time you start a shell (shell scripts, new terminal windows, etc).
For KSH, .kshrc has the same effect.
To make sure the variables are there, use set (no options) to list all of them or set|greppattern to search. As soon as they show up in a new terminal window, they should be there from inside your IDE, too.
[EDIT] If set can see them, so can IntelliJ. The only thing left is that IntelliJ could clean the environment when it starts Maven.
To test that, rename mvn to mvn.orig and create a new script mvn with this content:
#!/bin/bash
set > $HOME/mvn_env.log
mvn.orig "$#"
This script dumps the environment as mvn sees it to $HOME/mvn_env.log. If the variables are missing, then IntelliJ cleans the environment.

Categories

Resources