mvn package not recognizing OS environment variables - java

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.

Related

How to add environmental variables in AWS EC2 Linux

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

java.lang.UnsatisfiedLinkError when running java command as sudo

As you can see, it works when running as non sudo. I however need to run it as sudo.
I have faced this error this comes when there is classpath issue. Set classpath from command prompt, do via terminal, like java -Djava.library.path=blahblahblah... liked helping you.
Try either using sudo -E (which will pass your environment variables to the sudo environment), or pass the specific environment variables that you wish:
sudo LD_LIBRARY_PATH="$LD_LIBRARY_PATH" CLASSPATH="$CLASSPATH" java ...
(You may not need to pass the CLASSPATH, you can try with or without it).

IntelliJ - why does the terminal has different environment variables values

I have recently replaced Java 7 to Java 8.
All regular apps in IntelliJ runs smoothly.
All but the tomcat apps - which run from the cmd command line but not from IntelliJ.
It fails to load due to an error in the JRE_HOME definition.
I have oviously defined JRE_HOME in the env variables and in the jdk settings.
I have executed
echo %jre_home%
both in the IntelliJ terminal and in the cmd and received different values
(IntelliJ terminal showed unupdated value.).
Where are the variables in the terminal declared?
How do I change them?
I don't know about old IntelliJ versions, but in 2019.2.3 one can configure environment variables in project settings. Be aware, you need to restart existing terminal or create new terminal tab to see the effect.
Almost 5 years later, I have the same issue on Windows 10 and had to restart my computer to solve it.
Edit:
The best solution is to not define a JRE_HOME environment variable. I don’t know why but IntelliJ stucks using that env variable.
If you are launching IntelliJ via the JetBrains ToolBox it will cache the $PATH variable so recent changes will not appear.
Just close and re-open JetBrains Toolbox.
For me it was because IntelliJ IDEA did not launch the shell as a "login" shell. All of my environment variables are set as I expect in a login shell, but in a non-login shell, many of them are missing.
By way of example, I expect /usr/local/bin to be in my PATH, but that is only true within login shells (as I get when I run iTerm2). This is an excerpt from a terminal session within IntelliJ IDEA:
amacleod#valjean ~> echo $PATH | grep -c /usr/local/bin
0
(1) amacleod#valjean ~> $SHELL --login
Welcome to fish, the friendly interactive shell
amacleod#valjean ~> echo $PATH | grep -c /usr/local/bin
1
One possible workaround would be to add my expected PATH and other variables to the shell configuration for non-login shells, but I would really rather just always be in a login shell.
I would have hoped that IntelliJ IDEA would have a checkbox for sending --login in the Terminal settings. However, it is possible to override the Shell path setting to include the login flag.
Invalidating the systems cache should do the trick. see JetBrains: Invalidate Cache.
Any other measures (like closing projects and IDE seemed to work only on a per project basis and only after a while (when IDE detected that the cache is invalid by itself ...)
Edit:
Correction - Unfortunately, after having the corrected the environment in all terminals for a while, IntelliJ "reverted" to the old, incorrect environment in any of my projects. Now I hope that after a reboot - as posted by akuman8 - the issue is solved. (If not, I will stick to a commandshell of my choice outside the IDE, where everything works fine ...)
For me, invalidating the system cache or restarting IntelliJ didn't reload the updated environment variables.
After restarting the computer, it enabled IntelliJ to pick up the updated environment variables.
AFAIKT, the intellij terminal on linux is just the same as running a standalone terminal session. It's just conveniently located in the IDE.
In response to the question, "How do I change [the terminal environment variables]?"
I'm using virtualenvwrapper to manage my terminal's environment variables for different environments, such as dev, test and production. It's working just as well for java development as it was for python development.
https://virtualenvwrapper.readthedocs.org
Close all IntellJ windows and any process that may currently be using the wrong SDK.
Navigate to the folder where your JDK is installed (e.g. C:\Program Files\Java).
Rename the wrong SDK so something else (e.g. jdk-11.0.5.OLD). If it won't do this, you may still have a process running using that SDK; go back to 1.
Start IntelliJ again.
It seems you can define project specific environment variables:
In IntelliJ project specific environment variables you can set under
Run/Debug configurations --> Defaults --> Application --> Environment
variables
from:
http://emmanuelbernard.com/blog/2012/05/09/setting-global-variables-intellij/#comment-617922540

error when installing Java EE SDK on Linux

I did have JDK installed, and also put $JDK_HOME/bin on my PATH. However, I still have problem to install Java EE. Could you guys give me some hints? Thanks.
dav#Bruno:~$ ls
[some stuff...]
33511405_3645.jpg glassfish stuff
bea hplip-3.11.12-plugin.run temp
bin hpscan001.png Templates
C03_08.txt java_ee_sdk-6u4-jdk7-linux.sh Tomcat
c4c jdk1.7.0_10 Ubuntu One
[some stuff2...]
dav#Bruno:~$ echo $PATH
/home/dav/jdk1.7.0_10/lib:/usr/share/java:/home/dav/jdk1.7.0_10:/home/dav/jdk1.7.0_10/bin:/home/dav/Fluent.Inc/bin:/home/dav/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/dav/jdk1.7.0_10/lib:/usr/share/java
dav#Bruno:~$ sudo sh java_ee_sdk-6u4-jdk7-linux.sh
Could not locate a suitable jar utility.
Please ensure that you have Java 6 or newer installed on your system
and accessible in your PATH or by setting JAVA_HOME
dav#Bruno:~$
I think the problem you're facing is that the PATH variable you're looking at is set for the user 'dav', but because you're sudoing the sh command, the PATH changes to that of root, which probably doesn't contain the JDK.
I think you should try explicitly setting the JAVA_HOME environment variable, try running:
sudo JAVA_HOME=/home/dav/jdk1.7.0_10 sh java_ee_sdk-6u4-jdk7-linux.sh
in case someone still gets frustrated over this:
("Could not locate a suitable jar utility.
Please ensure that you have Java 7 or newer installed on your system
and accessible in your PATH or by setting JAVA_HOME")
First of all make sure you are not using "sudo" to install java ee.
when you use sudo you use a different environment (root, with not necessarily the same
environment variables). in this case (ee installation) sudo is not required.
In other (which ever) cases when root access is required make sure the required user/system variables are set on system level, not (just) on user level.
to set variables at system level instead of user level define the variables in a system level script, i.e. /etc/profile or /etc/profile.d
otherwise, if you are not using SUDO trying to install jee, and still have this "JAVA_HOME not set"-problem, try this:
the cause seems to be the JAVA_HOME variable that can not be found by the install script.
therefore adjust ~/.bashrc to define JAVA_HOME.
JAVA_HOME of course has to point to your oracle java (se/jdk) installation. If you haven't installed it, that's the first thing to do. JAVA_HOME should point at the root directory of the installation (not to the bin directory in it), i.e.:
export JAVA_HOME=/usr/local/jdk1.7.1_02.
Next change path like so:
PATH=$JAVA_HOME:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
keep the faith
don't let frustrations get the better of you (perhaps making you take it out on your colleagues or loved ones :o )
don't allow stress to build up too much. take a break in time to watch a funny video or
take a stroll to the coffee machine, or have a little chat with that cute person a couple of desks away from you.
You'll need to create an update alternative for the "jar" executable like:
sudo update-alternatives --install "/usr/bin/jar" "jar" "/usr/lib/jvm/jdk1.6.0/bin/jar" 1
Note: You may need to use appropriate path in your case.

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