How to Run Java Program Automatically on Tomcat Startup
2018-02-26 18:18
2441 查看
如何在启动tomcat时候,自动启动一个jar程序。
觉得这个应该在打包tomcat web服务为一个独立程序的时候应该有点作用。原文博客特别不错。
原文地址:https://crunchify.com/how-to-run-java-program-automatically-on-tomcat-startup/
Recently I wanted to start my standalone Java Application on Tomcat Startup. Also found so many other related questions on net. i.e.I need to run an application that can run automatically that when the tomcat starts..? any suggestions…?
how can I start my application by default on tomcat server start/restart?
Is it possible to
How to Start a service
To run java program automatically on tomcat startup, need to use Servletand this Servlet initialized on tomcat startup automatically.To execute a program, you have to use
This is my complete
Start Tomcat
Check your
Console Output
Enjoy and Happy Coding..
觉得这个应该在打包tomcat web服务为一个独立程序的时候应该有点作用。原文博客特别不错。
原文地址:https://crunchify.com/how-to-run-java-program-automatically-on-tomcat-startup/
Recently I wanted to start my standalone Java Application on Tomcat Startup. Also found so many other related questions on net. i.e.I need to run an application that can run automatically that when the tomcat starts..? any suggestions…?
how can I start my application by default on tomcat server start/restart?
Is it possible to
edittomcat startup services?
How to Start a service
automaticallywhen the tomcat starts
To run java program automatically on tomcat startup, need to use Servletand this Servlet initialized on tomcat startup automatically.To execute a program, you have to use
Servletand Servlet should define in deployment descriptor
web.xmlfile under
WEB-INFfolder.web.xml file contain tags
<load-on-startup>and
<servlet>tag. Servlet tag keeps information of Servlet class. When tomcat starts, all Servlet loads in web container and init method of Servlet loaded first. Any java statement in
init methodof Servlet can be executed on running tomcat startup batch or shell.In init method we can define our scripts which have to be executed e.g. sending emails, sending newsletters, starting scheduler, etc..
Below is a simple trick to run your java program automatically on Tomcat Startup.
Step-1
Modify Web.xml file with below information. WhereCrunchifyServletExampleis a class name and
crunchify.com.tutorialsis a package name.Modify these values as per your need.web.xml
<servlet> <servlet-name>CrunchifyTutorials</servlet-name> <servlet-class>crunchify.com.tutorials.CrunchifyServletExample</servlet-class> <load-on-startup>1</load-on-startup></servlet> |
web.xmlfilecomplete web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>CrunchifyTutorials</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>CrunchifyTutorials</servlet-name> <servlet-class>crunchify.com.tutorials.CrunchifyServletExample</servlet-class> <load-on-startup>1</load-on-startup></servlet></web-app> |
Step-2
CrunchifyServletExample.javaJavapackage crunchify.com.tutorials; import javax.servlet.*;import javax.servlet.http.HttpServlet; /** * @author Crunchify.com */ @SuppressWarnings("serial")public class CrunchifyServletExample extends HttpServlet{ public void init() throws ServletException { System.out.println("----------"); System.out.println("---------- CrunchifyServletExample Initialized successfully ----------"); System.out.println("----------"); }} |
Step-3
Now Clean your project using Maven orProject Menu->
Clean
Step-4
Deploy your project to TomcatStart Tomcat
Check your
system out logsand you should see output like this
Console Output
-------------------- CrunchifyServletExample Initialized successfully -------------------- |
相关文章推荐
- How to run a Java Program as a daemon (service) on Linux (openSUSE) using a shell script
- How to run a (Tomcat)Java application server on a Azure virtual machine
- How to run program or process on specific CPU cores on Linux(转)
- Tomcat - 在Mac的安装和运行(How to Install and Run Tomcat on Mac)
- How to Run Multiple Tomcat Instances on One Server?
- How to compile and run a simple java file in jenkins on Windows
- How to Compile and Run C/C++ program on Ubuntu 11.10
- 普通用户操作tomcat项目时报: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
- 启动tomcat的时候,报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 的解决方案
- How to Install Apache Tomcat 8 (on Windows, Mac OS X, Ubuntu) and Get Started with Java Servlet Prog
- NDK_报错 Unable to launch cygpath. Is Cygwin on the path?] java.io.IOException: Cannot run program。
- How to run a user control assembly that is hosted on Internet Information Services (IIS) in Internet Explorer
- How to Run Android Applications on Ubuntu
- 2014-11-16:How to run java project(containing several package) in linux
- How to get the control inside the ControlTemplate/DataTemplate on run time.
- How to build and run ARM Linux on QEMU from scratch
- HOW TO RUN YOUR WEB SERVER AND MQTT WEBSOCKETS BROKER ON THE SAME PORT
- Java how to program(Third Edition)——我的java手记(三)
- Java how to program(Third Edition)——我的java手记(五)
- How to run Selenium webdriver on IE