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.

TomcatStart.java
package com.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

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

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

  System.out.println("Success:");
  while ((ss = stdInput.readLine()) != null) {
   System.out.println(ss);
  }
 }
}




myStartup.bat
@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

: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


:gotJava
set "EXECUTABLE=%CATALINA_HOME%\bin\startup.bat"
call "%EXECUTABLE%"


tomcat.properties
catalinaHome=C:\Kumar\Softwares\apache-tomcat-8.0.38
javaHome=C:\Program Files\Java\jre1.8.0_111
port=8080

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"
              auth="Container"
              type="javax.sql.DataSource"
              username="${db.user}"
              password="${db.password}"
              driverClassName="${db.driver}"
              url="${db.url}"              
              maxActive="15"
              maxIdle="5"/>


2) Create properties in catalina.properties in /conf folder
db.driver=oracle.jdbc.driver.OracleDriver
db.user=DEVUser
db.password=XPL7TR360
db.url=URL_OF_DB


3) Update Spring configuration xml file to create JNDI mapping
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg ref="dataSource"/>
</bean>
 
<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.



<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-war-plugin</artifactId>
 <configuration>
  <outputDirectory>C:\Kumar\Software\apache-tomcat-8.0.28-windows-x64\apache-tomcat-8.0.28-windows-x64\apache-tomcat-8.0.28\webapps</outputDirectory>
  <warName>prs-war</warName>
 </configuration>
</plugin>


<outputDirectory>
- 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.