Linux/bash environment variable is null when reading from java - java

In Ubuntu I have exported the below environment variable in ~/.bashrc:
export MY_VAR="MY_VAR_VALUE"
When I re-open a terminal I can do:
~ $ echo $MY_VAR
MY_VAR_VALUE
~ $
But when I do this in Java/Eclipse (which I have restarted after exporting the test env. var):
String myVar = System.getenv("MY_VAR");
System.out.println(myVar);
I just get null. Does eclipse not see variables set in ~/.bashrc ?

In Linux (unlike Windows) you need a direct child relatinship between the Java executable, started by Eclipse, started by the Shell where you have exported the variable.
So you must start Eclipse from within that Shell. It wont work if you doubleclick on the executable in desktop or menu.
But I would specify the environment variables you need in the run configuration and not rely on such a setup.

Related

Windows 10 - setting Java and Maven as enviroment and system variables with bash terminal

I am working on a project on a virtual machine that is running on windows 10, and where I don't have the user access to system variables in GUI. Since, I need to use Maven I am wondering how can I set the java and maven variables with bash terminal?
I have tried to set the variable with this command:
export M2_HOME="/c/Program Files/apache-maven-3.6.3"
export M2="$M2_HOME/bin"
export MAVEN_OPTS="-Xms256m -Xmx512m"
And then if ran mvn --version in the terminal, I got:
bash: mvn: command not found
If I ran printenv I see in the list that the variables were set:
M2_HOME=/c/Program Files/apache-maven-3.6.3
M2=/c/Program Files/apache-maven-3.6.3/bin
MAVEN_OPTS=-Xms256m -Xmx512m
But, if I close the terminal and open it again, then I don't see these variables again.
What is the right way to set up Java and Maven env variables through bash on Windows?
You need to escape the string by surrounding it with quotes:
export M2_HOME="/c/Program Files/apache-maven-3.6.3"
EDIT:
To answer the question in the comments, note that you still need to add mvn's path to the $PATH variable in order to use it:
export PATH=${PATH}:${M2}

Not able to set JAVA_HOME variable in ubuntu 14.04 on manual installation

I've downloaded java jdk1.8.0.7.tar.gz file from the official website and unzipped it into my home directory. Now to set the $JAVA_HOME variable I used the follwing commands command nano .bashrc and then appending export $JAVA_HOME=/home/shivam/Java/jdk1.8.0.7 at the end of the file . But whenever I run the command sudo $CATALINA_HOME/bin/startup.sh I get an error message saying
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program
I don't understand why it is unable to find the java path though I've installed tomcat and set its home variable the this way, which worked.
I wan't to install Java manually without using apt-get. Kindly guide .
You want to append the following to .bashrc:
JAVA_HOME=/home/shivam/Java/jdk1.8.0.7
Note the missing $ at the start.
Also remember that the file won't effect your current shell without sourceing it first.
Also, when running a command with sudo, you are running it as the root user, not as yourself. So the environment variable needs to be set for the root user, not yourself.
You can run sudo env | grep JAVA_HOME to see whether it is set for root.
Have you run .bashrc ?
source .bashrc
Try add the following 2 lines in your .bashrc file:
JAVA_HOME=/home/shivam/Java/jdk1.8.0.7/
export JAVA_HOME

Batch file to setup jdk from memory stick

I'm doing a year in computer science at my university and it turns out there are very few computers that have the jdk installed on them. I can run eclipse portable from my memory stick, so compiling java should be possible.
What I was wanting was a batch file that set up the environment variables on the system so that I am able to run the portable version of the jdk from my memory stick. I don't appear to be able to alter the system variables but can change the user ones.
So far I have the below code as a batch file, though it still leaves me with a javac not recognize message when run. As a final note I should point out I don't have a great deal of experience with batch files.
echo %path% > stored_path.txt
setx PATH "%cd%PortableApps\CommonFiles\OpenJDK\bin;%PATH%"
setx JAVA_HOME "%cd%PortableApps\CommonFiles\OpenJDK"
setx CLASSPATH "%cd%PortableApps\CommonFiles\OpenJDK\bin;%CLASSPATH%"
Thanks for any help you can give me.
Update 04/10/13 - 16:54:
Just to clarify Eclipse itself works fine and is fully usable. However I have a jdk saved on the memory stick that works as if I call javac from a console that is open to that directory it works fine. What I want to know is if there is a way for windows to recognise this command from any directory on the system by just changing the user environment variables.
A thought that just occurred to me (I can't test it right now), is that would making a shortcut to the javac exe file in another folder work?
No need to actually set the environment variables permanently!
Just set them for the current session of the program. This is the batch script I made to launch my eclipse setup.
#echo off
setlocal
pushd "%~dp0"
if not exist eclipse.exe exit /b 1
if not exist jdk1.6.0_45\ exit /b 2
if not exist apache-maven-2.2.1\ exit /b 3
set "JAVA_HOME=%~dp0\jdk1.6.0_45"
set "MAVEN=%~dp0\apache-maven-2.2.1"
set "M2_HOME=%MAVEN%"
set "MAVEN_OPTS=-Xms512m -Xmx1024m"
set "CLASSPATH=.;%JAVA_HOME%\jre\lib\ext;%CLASSPATH%"
set "PATH=%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;%MAVEN%\bin;%PATH%"
rem M2_REPO
mklink /d jre "jdk1.6.0_45\jre"
start eclipse.exe -vm "%JAVA_HOME%\bin\javaw.exe" -data ".\workspace"
rem cmd /k
popd
endlocal
exit /b 0
This sets all that I need and starts eclipse with these settings without having to modify the variables.
My folder structure:
eclipse\
jdk1.6.0_45\
apache-maven-2.2.1\
workspace\
eclipse.exe
all other eclipse folders and files.
Our school-computers didn't have git installed, so i put the portable version of msysgit on my USB-Stick. That gives you (aside from git) a portable MSYS, which is a bourne shell with most of the standard linux tools (like ls and df).
So, i used the shell, instead of the windows Command Line. Then, for the shell, I created a small bash-script to set up the environment-path's. From the gist:
To make the script ("env_setup.sh") execute every time the git-shell
is started, you'll want to put the script into the "etc/"-directory in
your msysgit-folder.
After that, you'll need to edit the "profile"-file, which is executed
by the shell every time it's launched.
Add the following line at
the end of the file:
# Setup the environment-variables
. /etc/env_setup.sh
This is the script, which sets up the environment variables:
#!/bin/bash
# Resources:
# http://stackoverflow.com/questions/623518
# http://stackoverflow.com/questions/59895
# http://markashleybell.com/articles/portable-git-windows-setting-home-environment-variable
# Get the directory this script is in:
DIR=$(cd $(dirname "$0"); pwd)
# Get only the Flash-Drive letter (e.g. "F")
FLASH=${DIR:1:1}
echo "We determined your USB-drive to be on $FLASH:\\"
# Set the home-path to the Flash-drive to get portable SSH-keys:
# --- You'll want to change this to your prefered Home-directory!
export HOME=/$FLASH/PortableApps/git/home/luke
echo "I set your Home-directory to '$HOME'";
### --- EXAMPLES ---
# Set the Java-Home variable to a JDK on USB-Stick:
export JAVA_HOME=/$FLASH/JDK1.6
# Add the Java-Tools to the JDK-folder:
export PATH=$PATH:/$FLASH/JDK1.6/bin
# Add a Maven from your USB-drive to the PATH:
export PATH=$PATH:/$FLASH/PortableApps/apache-maven/bin
# Add Node.js from the local pc to your PATH:
export PATH=$PATH:/c/Programms/nodejs
Here, you can see a few examples, including a JDK I had on the same USB-Stick. It is added to the PATH, so you can write javac everywhere you like and always use the JDK specified.
Note that this is only for one session and that it only works in the MSYS shell!

System.getenv() does not list all the environment variables

I have noticed that some of my environment variables are not being picked up by the JVM.
In my .bash_profile I defined the following:
IO_HOME='some_value'
export IO_HOME
and by doing in shell:
echo $IO_HOME
I get the correct result.
But neither System.getProperties() nor System.getenv() is showing this variable being set. I tried both Java 6 and Java 7.
Is there something I am missing?
Exporting environment to spawned processes is pretty stable; if System.getenv() is not including a variable then it is because it is not in the environment. A couple of things to check, both relating to how the process is started:
Are you starting the java process from an environment where the variable is exported? For example, if it is in your .bash_profile and you are executing the java program from a menu or desktop then you have to log out and log in after adding it in .bash_profile for your desktop to see the variable.
Is the variable explicitly removed from environment for the process? ProcessBuilder allows this, as do most of all APIs that spawn processes.
One thing to try is to start the process from command line shell, after ensuring the variable is exported in that shell.
From Windows, I recently saw some crazy behaviour where IntelliJ refused to show all env vars from System.getenv() after setting either user or system env vars. The trick was to launch IntelliJ from a DOS box. For Windows users: Maybe a reboot (or logoff/logon) will fix the issue.

Java System.getEnv()

In mac OSX and in Linux CentOS, I insert a new system environment variable (i.e. "MYAPP") using .bashrc & .bash_profile. I even restarted my laptop (mac) and my server (linux).
When I use the command line "env", that environment variable showed with the correct value. But somehow every time I try to get it in a Java app (desktop app or web app or EJB or servlet any other java app) in either mac or linux, that environment variable ("MYAPP") is not retrieved.
I tried to iterate through the entire environment variables that Java can retrieve and it turns out that it retrieves every environment variables other than "MYAPP". This is very odd.
Anyone know how to solve this?
Did you export MYAPP=...? Exporting the variable makes it available to child processes, like java being run by your shell.
In Linux, if you only set the variable (or export it) in a bash session, it will be available to a kind of "sub" session, which is only available to the command you just executed, and nothing else.
You could probably use the dot operator in bash (also called "source" command). From the page:
When a script is run using `source' it runs within the existing shell, any variables created or modified by the script will remain available after the script completes.
So you could try doing . export VARIABLE=value, and then running your java program. This is similar to setting a variable in a Windows terminal, and then opening a new terminal and expecting the env var to be there. It won't.
This way, you are telling bash "this command should be available in this specific session (the session's process)". OTherwise you are telling it "set this env var for the bash session that will end after I run this export command" thus, it won't exist when you run your Java program.
After having defined and exported the environment variable. Launch your IDE from the same Terminal.
Try to write
"$System.env.STOREPWD"

Categories

Resources