Java @ Desk: Tomcat | Java at your desk

How to start Tomcat Server in Java

How to start Tomcat Server in Java

Tomcat server is started using the startup.bat file under bin folder. To start a Tomcat server from command prompt, user is required to hit the command startup.bat and tomcat server gets started in the new command prompt.

To start a server from Java code, its required to create a process of the batch file and execute the process. Below is the source code of the same.

The tomcat server path and the port is mentioned in the properties file along with the Java Path. Properies file is read in the batch file to set the values of Catalina Home and Java Home. If the values are not set in the environment variable for CATALINA_HOME & JAVA_HOME, then they are picked from properties file.

Here is the complete source code.
package com.test;


public class TomcatStartLocalCommand {
 public static void main(String args[]) throws IOException, InterruptedException {
  String command = "C:/Kumar/Documents/ChatBot/myStartUp.bat C:\\Kumar\\Documents\\ChatBot\\";
  Process process = Runtime.getRuntime().exec(command);
  String ss = null;

  BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

  while ((ss = stdInput.readLine()) != null) {

@echo off
if not "%CATALINA_HOME%" == "" goto gotHome
set arg1=%1
echo Tomcat Properties Location is %arg1%
For /F "tokens=1* delims==" %%A IN (%arg1%) DO (
 IF "%%A"=="catalinaHome" set catalinaHome=%%B
set CATALINA_HOME=%catalinaHome%
echo Catalina Home is %CATALINA_HOME%
if exist "%CATALINA_HOME%\bin\catalina.bat" goto gotHome

if not "%JAVA_HOME%" == "" goto gotJava
set arg1=%1
echo Tomcat Properties Location is %arg1%
For /F "tokens=1* delims==" %%A IN (%arg1%) DO (
 IF "%%A"=="javaHome" set javaHome=%%B
set JAVA_HOME=%javaHome%
echo Java Home is %JAVA_HOME%
if exist "%JAVA_HOME%\bin\java.exe" goto gotJava

set "EXECUTABLE=%CATALINA_HOME%\bin\startup.bat"
javaHome=C:\Program Files\Java\jre1.8.0_111

Spring MVC JdbcTemplate with JNDI Datasource in Tomcat Server

Spring MVC JdbcTemplate with JNDI Datasource in Tomcat Server

Configuring a datasource through JNDI mapping in Tomcat is beneficial since the database configuration remains independent of application. Configuring a JNDI in Tomcat and using the reference within Spring application to create a JdbcTemplate object achieves the object of loose coupling.

Any database configuration changes would be done at the Tomcat end without affecting the application deployed.

Below are the steps to define a JNDI in Tomcat and use in Spring configuration XML file to map with JdbcTemplate:

1) Create a JNDI configuration in Tomcat:
Open context.xml file in /conf folder and add a Resource entry

<Resource name="jdbc/MyDataSource"

2) Create properties in in /conf folder

3) Update Spring configuration xml file to create JNDI mapping
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg ref="dataSource"/>
<jee:jndi-lookup id="dataSource" expected-type="javax.sql.DataSource" jndi-name="jdbc/MyDataSource" resource-ref="true"/>

4) Inject or Autowire the JdbcTemplate object in classes to perform DB operation

Deploy Maven War Project in to Tomcat automatically

Deploy Maven War Project in to Tomcat automatically

Maven allows auto deployment of a war file into tomcat. In order to do so, maven-war-plugin is utilized. The plugin helps copying the war file into tomcat webapp's folder defined in the configuration.


- Path of the tomcat web app folder.

This helps in automatic deploying the war into tomcat and no need to restart the server after each build.