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
edit
tomcat startup services? - How to Start a service
automatically
when the tomcat starts
To run java program automatically on tomcat startup, need to use Servlet and this Servlet initialized on tomcat startup automatically.
To execute a program, you have to use
Servlet
and Servlet should define in deployment descriptor web.xml
file under WEB-INF
folder.Here is how to auto generate subtitles from any video with Google docs. It also works if you want to convert audio to text. Useful for creating subtitles. Automatically save your work in case of disaster. Schedule common tasks on your Mac. I've setup a custom module that will automatically change the group of sellers on my site depending on their sales and orders. The script runs fine when manually executing it, however when setting up a cron for it, the cron continually dies according AOE Scheduler, with the message: TRIGGER: shutdown function and the status died. I have the following code.
In the Advanced dialog, click Repeat task, then specify Every=15 minutes, Until:Time=5am. Edit: The above instructions are for Windows 2003 Server or XP. If Windows Server 2008 is like Windows 7, then you can do the same thing through the Triggers tab. Set a daily trigger, Start=5pm, Repeat Task Every=15 minutes, For a duration of=12 hours.
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 method
of 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.
![Tasks Tasks](https://img.creativemark.co.uk/uploads/images/473/11473/largeImg.png)
Below is a simple trick to run your java program automatically on Tomcat Startup.
Step-1
Modify Web.xml file with below information. Where
CrunchifyServletExample
is a class name and crunchify.com.tutorials
is a package name.Modify these values as per your need.
web.xml
2 4 | <servlet-name>CrunchifyTutorials</servlet-name> <servlet-class>crunchify.com.tutorials.CrunchifyServletExample</servlet-class> </servlet> |
This is my complete
web.xml
filecomplete web.xml
2 4 6 8 10 12 14 16 | <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'> <welcome-file-list> <welcome-file>index.htm</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.jsp</welcome-file> <servlet> <servlet-class>crunchify.com.tutorials.CrunchifyServletExample</servlet-class> </servlet> |
Step-2
CrunchifyServletExample.java
2 4 6 8 10 12 14 16 18 20 | importjavax.servlet.http.HttpServlet; /** */ @SuppressWarnings('serial') publicclassCrunchifyServletExampleextendsHttpServlet { System.out.println('---------- CrunchifyServletExample Initialized successfully ----------'); } |
Step-3
Now Clean your project using Maven or
Project Menu
-> Clean
Step-4
- Deploy your project to Tomcat
- Start Tomcat
- Check your
system out logs
and you should see output like this
Cornette 1 9 – Launch Tasks Automatically Updating
Console Output
2 | ----------CrunchifyServletExample Initialized successfully---------- |
Enjoy and Happy Coding.
Join the Discussion
If you liked this article, then please share it on social media or leave us your comments. Limited time 3 months free WPEngine hosting.
Other Popular Articles..
Problem: You want to run a script once every 5 minutes or at some other regular interval, but don’t want to do it manually.
You will need:
- Access to your UNIX (Any *nix should work) shell
- A script you want to run. If you don’t have a script you want to run, you can follow along with <ahref='https://github.com/jessicayung/blog-code-snippets/blob/master/record_time.py'target='_blank'>record_time.py</a> that appends a timestamp to a text file.
Optional Background: Cron is a tool in Unix that allows tasks to be automatically run in the background at regular intervals. Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.
Four (three if you’re lucky) Simple Steps:
Step 1: Go to your crontab file.
- Go to Terminal / your command line interface. It might look like this:
- Type crontab-e. The flag -e stands for edit. If your crontab file does not yet exist, this command will create the file for you.
- Optional: If you want you can choose your own editor. I like nano, so I typed export EDITOR=nano;crontab-e. All this is doing is specifying a code editor by setting the variable EDITOR to equal nano.
- You should see something like this:
- crontab file in nano editor (I happened to open this in the mail directory)
Step 2: Write your cron command.
- A Cron command first specifies (1) the interval at which you want to run the script followed by (2) the command to execute. The exact syntax is given below:
- 2jessica@comp:~$cd var/mail
- Note: Don’t type what you see before the dollar sign! And replace $USERNAME with your username, which is usually the word before the @ sign. E.g. my username is jessica. You can also type ls to list all the files in that directory. and then type nano$FILENAME to access that mail file.
- You’ll find error logs inside the mail file that can tell you more about what went wrong.
Step 4: Debugging potential problems Monologue 1 0 – quick and easy notes for a. Fontbook 5 16.
Cornette 1 9 – Launch Tasks Automatically Join
- Python path
- Some modules such as urllib.request are only available in Python 3+. Your default Python installation may be e.g. Python 2.7. So you may have to specify your (different) Python path. I use the Anaconda installation and my most up-to-date installation is at ~/Users/jessica/anaconda/bin/python, so I wrote my command asThe equivalent first line for most Python installations is #!/usr/bin/python .2# Your Python script here
- Some modules such as urllib.request are only available in Python 3+. Your default Python installation may be e.g. Python 2.7. So you may have to specify your (different) Python path. I use the Anaconda installation and my most up-to-date installation is at ~/Users/jessica/anaconda/bin/python, so I wrote my command as
- Crontab Restrictions: User does not have permission to execute crontab
- I did not encounter this on my laptop, but this may be an issue especially if you’re trying this on a machine at work. As written in the Crontab Quick Reference, you should check if your username appears in the file /usr/lib/cron/cron.allow . If the file does not exist, check if your username appears in the file /usr/lib/cron/cron.deny . You want your username to be in the cron.allow file and not in the cron.deny file. Only one username should be listed on each line. If neither file exists, then only the root user can execute cron.
Further reading: