Set environment variable in shell script/access in Tomcat Application - java

I want to add and environment variable which can access by my tomcat web-app. I have gone through this link but i want to set environment variable in root user. How to do that?

According to the docs (http://tomcat.apache.org/tomcat-7.0-doc/RUNNING.txt) you should set all env vars in $CATALINA_HOME/bin/setenv.sh
EDIT:
For completeness, I guess it's worth mentioning that even though this is the recommended way, the docs above state that:
By default the setenv script file is absent. If the script file is
present both in CATALINA_BASE and in CATALINA_HOME, the one in
CATALINA_BASE is preferred.
In case it is absent, you might also want to look for env vars in:
/etc/tomcat/tomcat[67].conf (suse) or
/etc/default/tomcat[67].conf (e.g. ubuntu) or
/etc/sysconfig/tomcat[67].conf (rhel, fedora)

This is how you can do it
sudo su and cd to /var/lib/tomcat8/bin/ (or whichever is your tomcat bin path)
touch setenv.sh(if it doesn't exist), if file present already do 'vi setenv.sh'
chmod 777 setenv.sh (make file executable)
vi setenv.sh and set following line in setenv.sh
export key=value
sudo systemctl restart tomcat.service
In your java file you can use the following code to check if the variable is set
private static void printEnv() {
System.out.println("******************************Environment Vars*****************************");
Map<String, String> enviorntmentVars = System.getenv();
enviorntmentVars.entrySet().forEach(System.out::println);
System.out.println("******************************system Vars*****************************");
Properties enviorntmentProperties = System.getProperties();
enviorntmentVars.entrySet().forEach(System.out::println);
}

got the solution...what i have done is i have put the export statements in /etc/init.d/tomcat6 at top and restarted the server by command sudo /etc/init.d/tomcat6 restart. So now my web-app running in tomcat server can access that variable.

Doesnt this work?
Go to your environment file.
sudo vi /etc/environment and
Add the required variable. and save the file.
I think in the recent Ubuntu, You would have to restart your system for the changes to take effect.

Related

How to upgrade JDK from Oracle to Zulu in redhat linux

I want to upgrade my Linux JDK from Oracle to Azul-Zulu.can someone helps me to do this.
What to do after downloading the tar.gz Zulu package?
I see that after extracting the downloaded package we need to put it in the path where we need to access java. Is that correct or what we can do more like setting environment variables etc.
the answer to your question depends on what you actually want to start with the JDK. Sometimes is is sufficient to change the path to java binary in a start script, e.g. Eclipse or Tomcat, sometimes you also need to setup the variable JAVA_HOME.
To setup the java for yourself only:
Check your shell by running echo $SHELL
-> /bin/bash
-> /bin/zsh
-> /bin/tcsh
or something like that Locate your shell's config file:
bash: ~/.bashrc
zsh: ~/.zshrc
tcsh: ~/.cshrc or ~/.tcshrc
and optionally most of the shells: ~/.profile or ~/.login check those file(s) if you already have variable declarations for: JAVA_HOME JRE_HOME (and rarely seen) SDK_HOME JDK_HOME
check whether any of those files contain a setup for the variable PATH (probably has)
If you found any of the *_HOME variables from above you can change them to reflect your new java home directory: e.g. zulu11.37.17-ca-jdk11.0.6-linux_x64 located under /opt/zulu11.37.17-ca-jdk11.0.6-linux_x64 would mean to set the variables as follows: zsh and bash:
export JAVA_HOME=/opt/zulu11.37.17-ca-jdk11.0.6-linux_x64
export JRE_HOME=/opt/zulu11.37.17-ca-jdk11.0.6-linux_x64
tcsh
setenv JAVA_HOME /opt/zulu11.37.17-ca-jdk11.0.6-linux_x64
setenv JRE_HOME /opt/zulu11.37.17-ca-jdk11.0.6-linux_x64
SDK_HOME and JDK_HOME may point to the above locations as well but are normally unused.
If you located your PATH somewhere it may read something like this: zsh and bash:
export PATH=~/bin:$PATH
tcsh:
setenv PATH ~/bin:$PATH
In order for your new java to appear "before" the "other" java you need to update (or create if missing) this to e.g.: zsh and bash:
export PATH=~/bin:/opt/zulu11.37.17-ca-jdk11.0.6-linux_x64/bin:$PATH
tcsh:
setenv PATH ~/bin:/opt/zulu11.37.17-ca-jdk11.0.6-linux_x64/bin:$PATH
After completely logging out and in again your JAVA_HOME and PATH can be controlled in a shell like this:
echo $JAVA_HOME -> should point to *your* java folder
echo $PATH -> should contain *your* java folder's bin directroy
which java -> should list the new java binary instead of /bin/java or /usr/bin/java
BTW: the variables JAVA_HOME, JRE_HOME and PATH may also be set in a startscript, e.g. catalina.sh for tomcat or the eclipse.ini for eclipse if you do not want to modify your environment.
If you download the RPM/DEB instead of the tar.gz and install the package you (may) not need configure anything since the packages might re-configure your system for you
Cheers, Holger

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

How to correctly set the JAVA_HOME environment variable?

on a tutorial (related to a product) it is shown hot to set the JAVA_HOME environment variable on a Linux Ubuntu system, in this way:
echo "JAVA_HOME=\"/usr/lib/jvm/default-java\"" | sudo tee -a /etc/environment
I have some trivial doubts:
1) What exactly is the JAVA_HOME environment variable and for what is it used?
From what I have understand it is something like a link to the JVM setted into the operating system that is used from the servlet container\application server to know where the JVM is and so it can be used. Is this assertion true or am I missing something? Exist some other purpose of this environment variable?
2) In the tutorial it is shown that the path of this environment variable is:
/usr/lib/jvm/default-java
but into my system I have something different, infact into the /usr/lib/jvm/ path I have not the default-java directory but I have a path like this:
/usr/lib/jvm/java-8-oracle
that contains the following directories:
bin
db
include
jre
lib
man
and some other files.
What is the correct path to use for the settings of my JAVA_HOME environment variable?
3) What exactly does this section of the previous statment:
sudo tee -a /etc/environment
Tnx
1: You are correct
2: It should point to the actual root folder of the jvm/jdk install, in this case /usr/lib/jvm/java-8-oracle
3: tee is a command to output the input both to a file and the command line, that -a option appends it to the file. So essentially it, as a super-user, appends the output of the previous command echo "JAVA_HOME=\"/usr/lib/jvm/default-java\"" to the file /etc/environment but also outputs it to the command line. Output should be, for you, exactly: JAVA_HOME="/usr/lib/jvm/java-8-oracle"

How to pass the -D additional parameter while starting tomcat?

I have a maven project, after running command mvn install all project as well as module compile and copied to local repository successfully. But now I want to run the generated web application in tomcat6. Client provided some parameter for tomcat like -Dapp.username,-Dapp.username, which will be used internally once project will start.ButI do not know how to set these additional parameter in tomcat6. Below is my development environment
OS = Windows
Tomcat = tomcat 6.0.27
Please help me?
For Tomcat 6 you should add the params to the startup.sh (Windows startup.bat). For Tomcat 7 and above you should set the parameters in the {Catalina Root}/bin/setenv.sh like such:
export CATALINA_OPTS="$CATALINA_OPTS -Dapp.username=username -Dapp.password=password"
Or in Windows:
set CATALINA_OPTS="$CATALINA_OPTS -Dapp.username=username -Dapp.password=password"
NOTE: Notice the $CATALINA_OPTS at the beginning so you don't wipe out any previously set values. Not doing so can create a very hard to debug problem!
If the parameters you are setting are solely to be used by Tomcat then be sure to set it using CATALINA_OPTS.
If your application will be using the parameters then be sure to use JAVA_OPTS instead. Tomcat will also read these parameters. This can also go in the setenv.sh file. For instance:
export JAVA_OPTS="$JAVA_OPTS -Dapp.username=username -Dapp.password=password"
Or in Windows:
set JAVA_OPTS="$JAVA_OPTS -Dapp.username=username -Dapp.password=password"
You can set an environment variable to do that. E.g. in Linux:
export JAVA_OPTS="-Dapp.username -Dapp.username"
Or in Windows:
set JAVA_OPTS="-Dapp.username -Dapp.username"
Do this before starting Tomcat
You will want to set the CATALINA_OPTS system variable - this is read by Tomcat (and only by Tomcat) when starting. As #Betoverse says you can set this using the two methods:
export CATALINA_OPTS="-Dapp.username -Dapp.username"
Or in Windows:
set CATALINA_OPTS="-Dapp.username -Dapp.username"
You can add that command to your ~/.profile on UNIX to have it set automatically.
I have tested params for Tomcat 7/8 on Windows 10 and CentOs 7 (Linux).
1) On Windows need to create setenv.bat in the {TOMCAT_HOME}/bin/ path and insert there such code:
set CATALINA_OPTS=-Dapp.username=admin -Dapp.password=12345
IMPORTANT: do not use quotes (" ") for setting params on windows.
2) On CentOs need to create setenv.sh in the {TOMCAT_HOME}/bin/ path and insert there such code:
export CATALINA_OPTS="-Dapp.username=admin -Dapp.password=12345"
You can also create {TOMCAT_HOME}/conf/conf.d/custom.conf and insert the same export command there.
If you don't want to change your environments or edit the .sh files you can start the server with something like the following
CATALINA_OPTS="-Dparam1=value1 -Dparam2=value2" catalina.sh start
before starting tomcat server right click project --> Run as --> Run Configurations
second tab --> -Dname=values , -Dname=values , -Dname=values
what about +Dname=value ,, values it become encrypted

Tomcat 7 setenv.sh is not found

I downloaded and extracted the apache-tomcat-7.0. As per the instructions in the RUNNING.txt (%CATALINA_BASE%/RUNNING.txt), it should set the JRE_HOME in the "setenv.sh" file.
Where is this file located ? Documentation said, it would be in CATALINA_HOME/bin directory. However this file is not present there.
Documentation does mention about the absence of setenv.(sh|bat) file:
(3.4) Using the "setenv" script (optional, recommended)
Apart from CATALINA_HOME and CATALINA_BASE, all environment variables can
be specified in the "setenv" script. The script is placed either into
CATALINA_BASE/bin or into CATALINA_HOME/bin directory and is named
setenv.bat (on Windows) or setenv.sh (on *nix). The file has to be
readable.
By default the setenv script file is absent. If the script file is present
both in CATALINA_BASE and in CATALINA_HOME, the one in CATALINA_BASE is
preferred
For example, to configure the JRE_HOME and CATALINA_PID variables you can
create the following script file:
On Windows, %CATALINA_BASE%\bin\setenv.bat:
set "JRE_HOME=%ProgramFiles%\Java\jre6"
exit /b 0
On *nix, $CATALINA_BASE/bin/setenv.sh:
JRE_HOME=/usr/java/latest
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
http://tomcat.apache.org/tomcat-7.0-doc/RUNNING.txt
Just create one yourself; it isn't part of the distribution. It's not that hard. For your case, simply add
JRE_HOME=/path/to/your/java/installation
to the file and make it executable (chmod 755 setenv.sh).
You can also add other options (e.g. -Xmx) if necessary.
If you don't find the "setenv.sh" or "setenv.bat" in bin folder of tomcat, follow the following setps:
Create new file in bin folder of tomcat.
Rename it to setenv.sh for linux user or setenv.bat for windows user
Now you can set multiple thing into this file:
Setting JRE_HOME
For linux user
JRE_HOME=/path/to/jre/jre6
For Windows user
set JAVA_HOME=C:\Path\to\jre\jdk6
Setting JAVA_OPTS
For linux user
JAVA_OPTS="$JAVA_OPTS -Xms512m -Xmx512m -DFOOBAR_CONFIGURATION_FILE=file:///C:/foobar.properties"
For windows user
set "JAVA_OPTS=%JAVA_OPTS% -Xms512m -Xmx512m -DFOOBAR_CONFIGURATION_FILE=file:///D:\foobar.properties"
Restart tomcat after setting variables.
That's it.
User Mindas' answer is completely correct: it is necessary to create the setenv.sh file in either the CATALINA_HOME or CATALINA_BASE directory, as the comment in the catalina.sh file indicates.
However, there is a "chicken and egg" problem wherein Tomcat must guess the location of the CATALINA_HOME/bin/ directory in the case where the CATALINA_HOME environment variable is not set.
I have encountered this situation on a Ubuntu server where fortunately Tomcat guesses the CATALINA_HOME/bin/ directory correctly. For a single-user environment where customization of Tomcat for different users isn't necessary, it seems that setting the JAVA_HOME or JRE_HOME environment variable in the catalina.sh script would be a solution to the "chicken and egg" problem.
In my case, I downloaded Tomcat for Windows. It also came with Linux shell scripts, so I created a 'setenv.sh' and tried to run it from Windows Subsystem for Linux, but got the above error. The issue was that the non-binary shell scripts were formatted for Windows, and Linux didn't know how to run them. After converting them to Linux format, it was able to recognize and run the scripts. Try this to convert from Windows to Linux:
dos2unix $CATALINA_HOME/bin/*.sh

Categories

Resources