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
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 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 have imported an Existing Application into Eclipse Helios Version.
I am using Tomcat 6.0 server.
Inside our code we have this:
instanceName = System.getProperty("tata.instanceName");
systemPath = System.getProperty("tata.home");
Please tell me where should I define this properties? (SO that it reads this values from our environemnt)
For your information, I would be developing application inside the Windows and deploy it into Remote Linux server .
You can either do it as part of the java command by specifing the -Dkey=value pairs or do it programmatically.
java com.foo.Bar -Dtata.instanceName=baz
or
System.setProperty("tata.instanceName", "baz");
One option would be using -D flags when running Tomcat.
On the command line you can do
-Dtata.instanceName=name -Dtata.home=home
Add lines like this to $TOMCAT_HOME/bin/setenv.sh
export JAVA_OPTS="${JAVA_OPTS} -Dpropname=value "
in eclipse, double-click the server, "Open launch configuration", go to "Arguments" tab, and add the properies.
outside of eclipse, add them to JAVA_OPTS in catalina.bat / catalina.sh
The format of the properties is:
-Dtata.instanceName=foo -Dtata.home=bar
I´m trying to install an application health monitoring application that can monitor J2EE web transactions and I need to put a javaagent into my Tomcat somehow but am not clear on exactly how to do this, I am using Linux and have been instructed by the software company that makes this product to do something like below:
-javaagent:<Path to the WebTransactionAgent.jar>
I have received further support from them and they basically said to put this into the appropriate .sh file (but they weren´t able to tell me which file that is for Tomcat)
I tried putting this in the catalina.sh file but it does not seem to be working:
JAVA_OPTS=%JAVA_OPTS% -Xms128m -Xmx512m -XX:MaxPermSize=256m -javaagent:"C:\WebTransactionAgent.jar"
Any advice is appreciated
For Unix/Linux, do this in <tomcat_home>/bin/setenv.sh, e.g.
CATALINA_OPTS="$CATALINA_OPTS -javaagent:/path/to/YourJar.jar"
You might need to create this file if not present and chmod it to 711 or 755.
For Windows, the counterpart is setenv.bat.
To add to mindas' answer, the -javaagent command could also be added to the JAVA_OPTS environment variable in one of the following (if they exist):
<tomcat_home>/conf/tomcat6.conf
JAVA_OPTS="${JAVA_OPTS} -javaagent:/full/path/to/YourJar.jar"
or <tomcat_home>/bin/catalina.sh
export JAVA_OPTS="$JAVA_OPTS -javaagent:/full/path/to/YourJar.jar"
Use JAVA_OPTS as CATALINA_OPTS would not allow JVM options [option2]=[value2].
Example, for adding jacocoagent.jar with options as below, only JAVA_OPTS will work.
JAVA_OPTS="${JAVA_OPTS} -javaagent:$CATALINA_HOME/lib/jacocoagent.jar=destfile=/tmp/jacoco.exec"
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.