How to read Ubuntu environment variables into SpringBoot Program? - java

Here's what I do in SpringBoot on Windows to read an environment variable (location of log folder).
In Windows Server, I set a System environment variable for "LOG_HOME" with the value with the directory that SpringBoot should use to write logs.
In SpringBoot's application.properties, I have:
logging.file.name= ${LOG_HOME}/ws.log
Works great!
But in Ubuntu Linux 20.04, the same approach doesn't work for me at all.
When the WAR file tries to deploy on Ubuntu 20.04 using this similar technique:
(in .bashrc): export LOG_HOME = /home/ubuntu/logs
reboot (to reload the environment for sure)
I get this error in the Tomcat log when trying to deploy the WAR file:
java.lang.IllegalArgumentException: Could not resolve placeholder 'LOG_HOME' in value "${LOG_HOME}/ws.log"
So, it seems that Spring doesn't see the environment variable set in Ubuntu.
I wrote a simple Java program just to check the value of the environment variables and they were all created as expected including the LOG_HOME as shown in Linux "printenv".
If possible, I need a technique that will work on Ubuntu without changing the working SpringBoot implementation on Windows Server.
Thanks in advance for suggestions.

Instead of exporting in shell session like
export LOG_HOME = /home/ubuntu/logs
try this as -D VM argument in your starup command
eg:
java -cp=xxx mainclass -DLOG_HOME=/home/ubuntu/log
if you are using tomcat then :
VM args can be added catalina.sh file under CATALINA_OPTS.

For tomcat, add your environment variables to $TOMCAT_HOME/bin/setenv.sh where $TOMCAT_HOME is the directory of your tomcat installation.

The solution for me posted by the extremely helpful satyesht above, was to edit the Catalina.sh file and add the "-D" name-value pair option under CATALINA_OPTS. Thanks to all who posted. :)

Related

OPENSHIFT editing LD_LIBRARY_PATH & PATH for environment setting

I am currently working on trying to deploy my java web application onto Openshift. My web application imports Gurobi library which requires a Gurobi License Environment to run my Linear Programming model. As such i've been going around trying everything i can to get the license installed to the openshift linux server etc
I've managed to get a few things going.
I've SSH into my application and registered the HostName of the openshift linux platform to the license.
I've managed to change the .bash_profile file which sets up the custom environments. below are the input i have placed into the file.
export GUROBI_HOME="/var/lib/openshift/54d4e281e0b8cd3a1400015c/jbossews/gurobi600/linux64"
export PATH="${PATH}:${GUROBI_HOME}/bin"
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${GUROBI_HOME}/lib"
export GRB_LICENSE_FILE="/var/lib/openshift/54d4e281e0b8cd3a1400015c/jbossews/gurobi.lic"
The problem with the input above is that I believe for PATH & LD_LIBRARY_PATH, openshift automatically reinstates them everytime I access my app through the shell.
I think this has resulted in my application being unable to instanciate a GRBENV object which is a Gurobi Environment object which checks the gurobi.lic file and use the LD_LIBRARY_FILE.
Has anyone did something similar to this before? Really need help in this.
Brandon
The way openshift instantiates your app and the ssh shell seems to be different. The correct place to set the environment variables are one of the start action_hooks. For instance, if you edit the .openshift/action_hooks/start file and add:
export PATH="${PATH}:${GUROBI_HOME}/bin"
this will work for the instance that runs your APP, but not for the SSH Shell. I did the following test:
echo $PATH > $OPENSHIFT_DATA_DIR/PATH
to confirm that the path running the APP can be set by the start hook.
I´m not sure if this solves your question (because you mention SSH shell, and I still don´t know how to set the PATH for that).

How to configure Apache Tomcat on a Windows 8.1 installation?

I'm trying to install a 64bit Apache Tomcat distribution on a Windows 8.1 machine but the way in which I have set the environment variables seems to be incorrect.
The steps I have gone through in setting up are as follows:
Copy/Paste apache-tomcat-7.0.57 folder into Program Files (x86)
Set environment variables in the environment variables gui.
Set TOMCAT_HOME to the location of the Tomcat folder: C:\Program Files (x86)\apache-tomcat-7.0.57
Set CATALINA_HOME to the location of the Tomcat folder also: C:\Program Files (x86)\apache-tomcat-7.0.57
But when I execute %TOMCAT_HOME%\bin\startup to check it has been installed correctly I get the following error in my CMD:
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
I found a related question on SO and it gave me the following fix but that http://goo.gl/aNmxGM but that threw another error below:
files was unexpected at this time
I gathered from this that there is a problem in the way I'm setting up the environment variables or possibly the placement of the Tomcat folder. Does anyone have any advice on a fix for this issue?
I think that what you really need is to put it between "" as your path contains space
"%TOMCAT_HOME%\bin\startup"
or
"%TOMCAT_HOME%"\bin\startup
I hope this could help!
Use
"%CATALINA_HOME%"\bin\startup
in the command prompt if you have set the environment variables correctly. I have tried it and it has worked well.

Using Tomcat 7 on Ubuntu 12.10 --- How To?

I am a newbie to Ubuntu 12.10 and moved to it from Windows.
In Windows I have configured the environment variable to include servlet-api.jar in the CLASSPATH variable so I do not have to type -cp <path to servlet-api.jar> every time I compile.
For normal Java programs, I have set the JAVA_HOME using:
export JAVA_HOME="/usr/lib/jvm/java-7-openjdk-i386" >> ~/.bashrc
With that solved, I need to set the CLASSPATH but do not know how to.
Please help on that!
Also I read on some forums that I should change the CLASSPATH manually each time I compile because changing the CLASSPATH may upset other applications.
Next, I tried this command to start Tomcat
service tomcat7 start but I got an error :
You need root privileges to run this script I do not know why. Help me solve that!
And if within the scope, please tell me how the directory structure of tomcat in Windows differs from that in Ubuntu.
Misc
I used sudo apt-get install tomcat7 to get Tomcat 7.
You don't say whether you're using an Ubuntu Tomcat package or a standalone Tomcat installation.
For a standalone Tomcat, you will be starting and stopping it using the scripts in its bin directory: startup.sh and shutdown.sh. In that case, Tomcat will use the environment variable values set at the time of invocation.
When doing this, you can set the environment variables prior to running these commands.
The same is true for the Tomcat 6 or Tomcat 7 that Ubuntu distributes as packages.
These neatly separate the $CATALINA_HOME directory (where the Tomcat 7 distribution is supposed to be) from the $CATALINA_BASE directory (where all of the changes and additions for a particular Tomcat instance are supposed to be. For the tomcat7 package, the first directory is /usr/share/tomcat7, while the second is /var/lib/tomcat7.
You can find startup.sh and shutdown.sh in /usr/share/tomcat7/bin and of course you can use them.
However, if you use the Tomcat provided with Ubuntu, you're probably going to want to run it just like any other standard system service, and the packages support this: e.g. in the case of tomcat7, you can just use
sudo service tomcat7 status
sudo service tomcat7 start
sudo service tomcat7 stop
sudo service tomcat7 restart
just like you can for any system service, and it uses the same mechanism as other system services do:
the shell script /etc/init.d/tomcat7 is used to start and stop Tomcat 7
/etc/default/tomcat7 is its configuration file, allowing a few parameters to be set
Using this method, you cannot specify additional environment variables for Tomcat to use by setting them in your shell or in /etc/default/tomcat7; they will not be passed to Tomcat.
However, there is a second method to set environment variables: if you set them in the shell script /var/lib/tomcat7/bin/setenv.sh (or, if you must, /usr/share/tomcat7/bin/setenv.sh), they will be picked up and passed to Tomcat. This method always works.
Finally, Tomcat offers finer control than just using a $CLASSPATH when it comes to specifying additional classes or JARs to be loaded on startup: in its conf/catalina.properties configuration file, you can set the common.loader, server.loader and shared.loader to a list of directories and/or JAR files to be loaded in additional to the standard ones.
All it means is that you should do that as a superuser - which translates to administrator in windows so you should try something like sudo service tomcat7 start
Try export CLASSPATH=/usr/share/tomcat7/lib/servlet-api.jar:/usr/share/tomcat7/lib/jsp-api.jar - This will depend on where your tomcat installation in located.
You can get more here
Cheers

Java settings, classpath and environment variables

I'm not sure if it is best practice but I add MySQL-connector jar to the extensions directory of my Java install directory to I can easily connect to MySQL databases.
I also set environment variables to point to various directories so that I can develop on different machines and only define environment variables locally and code doesn't have to be modified for file paths.
In either case of the above I find that unless I reboot my computer java does not recogise either. What happens during a reboot to Java? Is some config file updates by a java process? Can you update this without having to reboot?
To test this I have created a new environment variable on both Mac (adding to .MacOS/environment.plist), Linux (Ubuntu 12.04) and windows 7 (via control panel). I then used System.getenv("TestVar"); which returns null. Running set from the command line shows it exists though. After a reboot System.getenv("TestVar"); returns the expected value.
Ultimately your goal is to include jar files in CLASSPATH . its up to you how include jars in classpath but this is not good practice to put jars inside extensions directory . While running your program modify CLASSPATH value .
java -cp jar1:jar2:jar3:dir1:. HelloWorld
java -classpathjar1:jar2:jar3:dir1:. HelloWorld
As far as setting environment variables goes the on Ubuntu a log out is required
https://superuser.com/questions/339617/how-to-reload-etc-environment-without-rebooting

'javac' cannot be found problem

I have a Java application hosted on a remote tomcat instance that executes a bat file which contains the following line
javac filename.java
I receive the following error :-
'javac' is not recognized as an
internal or external command, operable
program or batch file.
I have set the path in the environmental variables. I tried running the same bat file on the machine, it works(which means there is nothing wrong with the path). But running it through the application gives the error. What might be the problem?
Setting the correct path in your environment does not fix the path in the environment of the running tomcat process. If you changed the path in the system settings, a tomcat restart (or system reboot) probably solves your problem.
Once you add %JAVA_HOME%\bin to the PATH var on the remote machine (assuming you have %JAVA_HOME% set there), you will need to run up a new cmd window for the new PATH to be available.
You should set the classpath for the user who is executing tomcat, not only for your login user.
If you have the JRE installed, and not the JDK, you'd be able to run Tomcat but there wouldn't be a javac.exe. Go to JAVA_HOME/bin and see if there's a javac.exe. If not, you have the JRE installed, and you'll have to go get the JDK.
Just curious - why is your Tomcat app calling javac.exe? Are you creating classes on the fly? If yes, why would you not be generating byte code using ASM instead?

Categories

Resources