How to add environmental variables in AWS EC2 Linux - java

I am writing an application in Java and I need to use environmental variables in AWS EC2 machine (Linux). I am using System.getenv("myvariable") in the application to get the particular environment variable. I need to set the permanent environmental variables and currently I have set the variable in ~/.bashrc , ~/.profile and also ~/.bash_profile.
I am giving export myvariable=xyz in al the 3 files and then I ran source ~/.bashrc , source ~/.profile and source ~/.bash_profile. However I am getting null in the application.
I still don't understand why I am not able to get any environmental variables even though I am exporting the variable in ~/.bash_profile.I have even checked by running echo $myvariable and I can see xyz.
If I set the same environment variable in my local MAC machine in the same way I set above and use the same shell to run the same java code, I can see the variable with the value.
So basically I am getting null every time in my AWS EC2 linux machine.
Is there any other place I need to set the variable? I have even restarted the machine but it didn't help.

It sounds like the problem is how you are setting the env variables. Sudo does not preserve them by default, so you need to use the sudo -E option.
This is explained in more detail in: How to keep Environment Variables when Using SUDO

Related

mvn package not recognizing OS environment variables

I'm trying to deploy an API do AWS EC2.
And this API has environment variables inside application.properties, such as $DB_HOST, $DB_USER and $DB_PASS, and then I set them at
vim .bashrc
source .bashrc
All the process occurs alright for the deployment, but when I run
sudo mvn package
It doesn't recognize the environment variables setted from the OS (linux) and build fail.
I can't really find any similar solution here.
Also tried putting them inside the /˜profile with
nano /˜profile
source /˜profile
Anyone has any ideas? :)
When you use sudo, it doesn't use your environment variables but those of the root user (or in case you use sudo -u <user>, that user).
I suggest not using sudo without specifying a user. That means that the code is running as root, which is a big security risk. I'd create a new user dedicated for your application. You can configure that user's environment variables as well.

System.getenv() call in JVM

I am setting environment variable in my machine using export MY_KEY=foo. And I am trying to fetch it in JVM using System.getenv("MY_KEY"). This returns null. But running echo $MY_KEY shows foo on the terminal.
I have tried restarting the IDE. Doesn't work, still.
The environment variable is only available to sub processes of the shell that exported it. Did you start your IDE from that shell?
If you want the variable to be available all the time, you need to add it
to the /etc/profile file or create a extra file in /etc/profile.d. It depends on your operating system.

System.getenv still picks up deleted env variable value on MacOS Mojave - Java

I added some exportable environment variables in my bash_profile and my profile files in the following format export "X=y". This worked as it should, now I want to remove them permanently . I've tried the following methods:
Deleting the exported variables from the bash_profile and the profile files, and saving the files, and calling source ~/.bash_profile and source ~/.profile.
Called "unset X" in terminal window. Before I could call "echo $X" which would display y, now it displays nothing.
Rebooting the Mac Computer.
Still however, whenever I call Java's System.getenv(X), env variable X's value (y) is still returned. What else do I need to do to completely eliminate an env variable from my system in MacOS Mojave?
It appears you believe environment variables are global. That is, modifying the value in one program, such as a running terminal, will affect the value in a different program. That is not how env vars work in a UNIX like OS. Each process is provided a copy of the env vars provided by the parent process. That is, they are inherited from the parent process.
The fact that you have to unset X in a terminal to remove it means that either it is being inherited by the shell from the terminal process or your shell is setting it. In the latter case the specific files read when a shell starts depends on the shell. But /etc/profile is read by most interactive shells so you might want to look there.
You say your Java app is run by Tomcat but failed to mention how Tomcat is started. That is important for the reasons I mention above.
Note that macOS uses a daemon named launchd to manage running most services. Those services are configured via "plist" files. See man launchd.plist. Those launchd config files support defining custom env vars. That is slightly unusual but worth looking at. See if any of the files in ~/Library/LaunchAgents, /Library/LaunchAgents, or /Library/LaunchDaemons mention the env var.

System.getenv("TEST_HOME") is returning null

I am working on a ubuntu 14.0.4 machine.
I exported a variable TEST_HOME in my .bashrc file using
export TEST_HOME=/home/dev/code/test
When I tried echo $TEST_HOME from terminal, it returned /home/dev/code/test
So far, so good.
When I try from Java code :
String value = System.getenv("TEST_HOME");
value is null.
Am I missing something here?
Mentioning the variable in .bashrc will work only for programs started from shell. For system wide environment variables mention it in /etc/environment.
Refer Ubuntu Environment variables
.bashrc would set environment variable only for bash shell. To set it system wide set it in /etc/environment file.
Since you are using eclipse, and it does not run within bash shell, it is not getting the variable you are setting. If you run your programme using java command line in your terminal then it should get it.
Setting variable in /etc/environment would make it available to eclipse. You will need to restart your machine once you update /etc/environment.

reading environment variables from java using Eclipse

Everywhere I search, it says you can get an environment variable by using System.getenv(str).
It's not working for me. Here's what I am doing:
OS : Mac OS x 10.7
Java 1.6.x
If I do export abc=/hello/ in my terminal and then echo $abc, it gives me the variable. If I close the terminal, reopen it again and do echo $abc, it's gone. To overcome this, I edited my .bash_profile file and inserted export abc=/hello/. Close the terminal, do echo $abc and it works. So I understood that the env variable is permanent now.
Now if in my java console app, I print System.getenv("abc"), it returns null. What am I missing?
The reason that you needed to put the export in your .bash_profile is that setting environment variables in a shell only persist the variables in that shell, and - since you used export - to children of that shell, or in other words, other programs launched by that shell.
If you're running your java code from Eclipse, and you launch Eclipse from a shell with your environment variables set, then your program should see the added environment variables. To launch Eclipse from the shell, you'll need to use the OS X open command:
$ open /Applications/eclipse/Eclipse.app
Alternately, you can set the environment variables within your Eclipse project, and you'll need to do this if you're not launching Eclipse from a shell with the proper environment. In the Run Configurations dialog, look for a tab named Environment. Here you'll find a table for adding environment variables that will be passed to your program.
It's better to add the environment variables to the Run Configuration since that way they'll always be available to your project. Your code doesn't actually care where the environment variables are coming from, and adding them to the project is simpler, and will work the same way on different platforms.
Of course, when you run your program outside Eclipse, you'll need to make sure that the same environment variables exist in the shell where you e.g. run java.
Eclipse does not use the system's env variables unless launched directly from the shell (which is how it is generally launched, by clicking its icon). In that case you will have to explicitly set the required env variables in the environment tab of the run configuration of the program.
I too faced The same issue , I resolved it this way.
Open Terminal
cd to the folder where eclipse.app is located E.g cd /Users/Shared/eclipse/jee-2020-09
Type open Eclipse.app/
Eclipse will now open and will be able to access the system environment variables as well.
Check it using the code:
System.getenv().forEach((k, v) -> {
System.out.println("ENV : " + k + ":" + v);
});

Categories