I am using the Gurobi Optimizer, for which I need to set two environment variables: LD_LIBRARY_PATH and GRB_LICENSE_FILE.
If I set the variables using Eclipse (Run As --> Run Configuration --> Environment), everything works correctly. If I set the variables through the system (using export in Linux), I get the error
no GurobiJni46 in java.library.path
Any ideas why this happens and how to fix this?
In the end, I want to run a web application in Tomcat, which would require those variables set correctly.
In the case where you have set the environment variable using export are you then running your application from inside Eclipse or are you running your application from the command line?
The launcher for Eclipse usually explicitly sets the path using the command line switch -Djava.library.path which would overwrite what you have set in the environment.
If you set the LD_LIBRARY_PATH before running Tomcat it would work, provided that Tomcat doesn't override it in one of its start-up scripts!
Related
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. :)
I have set environment variable by executing the command
export test=abcd
I can see test=abcd when I run printenvcommand
I have deployed a springboot.jar application and I am passing the JAVA_OPTS from the springboot.conf file.
JAVA_OPTS='-Dspring.profiles.active=aaa -Denv=$test'
I started the app by service springboot start . When I check the process, env variable doesn't have the value of $test environment variable.
/usr/bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -Dspring.profiles.active=aaa -Denv=.
How can I access the environment variable in the conf file? I read somewhere the environment variables will be stripped off when run as service. Basically I want to run as service springboot start which internally executes the below command
java -Dspring.profiles.active=aws -Denv=${whatever is set for env environment variable} -jar springboot.jar
I have tried the below configurations but nothing worked
JAVA_OPTS='-Dspring.profiles.active=aaa -Denv='$test
JAVA_OPTS='-Dspring.profiles.active=aaa -Denv='${test}
JAVA_OPTS='-Dspring.profiles.active=aaa -Denv=${test}'
JAVA_OPTS="-Dspring.profiles.active=aaa -Denv=$test"
Be careful about your quotes. Assuming that you use a "normal" shell, variables won't be substituted in single quotes.
java -Dspring.profiles.active=aws -Denv="$myvariable" -jar springboot.jar should lead to env being available in the JVM, no matter if you run it as a service or not.
If you can't get it to work, try to specify a hard coded value like this java -Dspring.profiles.active=aws -Denv=foo -jar springboot.jar. If env is now available in the JVM, your problem is with your shell or run mechanism. Verify that the user who runs the command (i.e. do you use sudo?) has the variable set.
I had the same problem where my .conf was referencing an environment variable which was in the .bashrc.
What I found out is:
The problem is service strips all environment variables but TERM, PATH and LANG which is a good thing. If you are executing the script directly nothing removes the environment variables so everything works.
https://unix.stackexchange.com/questions/44370/how-to-make-unix-service-see-environment-variables
One solution would be to install your app as a systemd service:
https://docs.spring.io/spring-boot/docs/1.3.x-SNAPSHOT/reference/html/deployment-install.html
Or another way is to use docker and you can specify extra configuration in the docker file, like loading a file which contains your environment variables.
As those solutions where not available in my case I ended up with having the value in the .conf file, like: -Denv=prod
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
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
I have different projects using different version of JDK (5.0, 6.0) and Tomcat(6.0, 7.0), so how should my JAVA_HOME and CATALINA_HOME be set in environment variables?
Or maybe it is NOT necessary to set JAVA_HOME and CATALINA_HOME in environment variables if I am running my App by .War file? The jdk/tomcat server will be running the version I picked when I packed it (through Eclipse -> preferences...).
Inside the tomcat startup script /bin/catalina.sh, the following environmental variables are used:
JAVA_HOME is the path of JDK that used to run the tomcat and web applications
CATALINA_HOME is the path of the tomcat binaries files
CATALINA_BASE is the path the tomcat configuration files
So , how about this approach? For example :
Install JDK 5.0 to : /opt/jdk5
Install JDK 6.0 to : /opt/jdk6
Install tomcat 6.0 to :/opt/tomcat6
Install tomcat 7.0 to : /opt/tomcat7
Each of your web application has their own folder to hold their own tomcat 's configuration. For example :
/home/web1 for the web application 1
/home/web2 for the web application 2
Inside each of these folders , we need the following sub directories: conf, logs, temp, webapps, and work.Simply copy these sub directories from the tomcat installation folder (ie. /opt/tomcat7/) .Then put the .war to the corresponding webapps folders (e.g. /home/web1/webapps/webappl.war , /home/web2/webapps/webapp2.war ).
Finally , write a script to start the tomcat using different JDK and tomcat for each application . For example , to start web1, your script should look likes:
JAVA_HOME=/path/to/jdk #eg./opt/jdk6
CATALINA_HOME=/path/to/tomcat/installation #eg./opt/tomcat7
CATALINA_BASE=/home/web1/
export JAVA_HOME JAVA_OPTS CATALINA_HOME CATALINA_BASE
$CATALINA_HOME/bin/catalina.sh start
Reference :
http://www.mohancheema.net/appserver/setting-tomcat-to-run-mutiple-instances-of-it
If you are running it with in Eclipse, you can use the Run Configuration dialog to set any enviornment variables that you need to change. These get automatically set when you execute the specified Run configuration. To use this, Right Click on your project, select Run -> Run Configurations. In that you can select a Run Configuration, and goto the Environment tab, and there you can specify the custom variables you want, and also you can override anything that is set by the O/S.
If you want to do this outside eclipse, and you keep a copy of Tomcat dedicated for each project, edit the startup.sh or startup.bat files depending on your OS and then set the environemnt variables explicitly there.
Ex. For Project 1:, on top of /opt/apache-tomcat6-1/bin/startup.sh file add these lines
export JAVA_HOME='/opt/jdk1'
export JAVA_HOME='/opt/apache-tomcat6-1'
Ex. For Project 2:, on top of /opt/apache-tomcat6-2/bin/startup.sh file add these lines
export JAVA_HOME='/opt/jdk2'
export JAVA_HOME='/opt/apache-tomcat6-2'
If you don't have a dedicated copy of Tomcat, then you can create a shell script / batch file per project which will set the necessary environment variables like this and then invoke the corresponding startup.sh or startup.bat file.
These variables are used by the scripts that start Tomcat, and don't matter otherwise. You can set them immediately before running the startup.sh script, or you can edit the catalina.sh script to set the values in the script itself (this is a good way to do it, since catalina.sh is shared by the other scripts), or you can write your own scripts which set the variables and then call the tomcat scripts... There are many possibilities. You just can't set the variables globally.