4

I would like to to use Tomcat and without Eclipse.

I can't find any Tomcat tutorial with command line. They all use Eclipse.

What I need is:

  • Create WAR File
  • Restart Tomcat with new WAR File

  • Update WAR File (When I edit a css/js file with Eclipse, I don't need to refresh Tomcat)

The best would be to use command line. I'm using Ubuntu.

Do you have any tips or resources to achieve this?

Charles
  • 11,367
  • 10
  • 77
  • 114
  • "./catalina.sh restart" in tomcat bin restarts tomcat, change restart to stop or start. Creating a war file - use maven. – NimChimpsky Jan 22 '13 at 16:22

3 Answers3

2

Just follow below steps:

  • Put war file inside webapps directory of tomcat

  • Restart tomcat by running scripts inside bin directory from your terminal(shutdown.sh and startup.sh)

  • Access application by launching browser and then putting below URL

     localhost:8080/app/index.jsp ( if generated app.war)
     localhost:8080/index.jsp (if you generated ROOT.war)
    
rai.skumar
  • 10,309
  • 6
  • 39
  • 55
1

Just to add what @rai.shumar said, I've also had to find this one out recently. Here is what I've learnt so far.

References:

Firstly create a dir-structure:

  • Create a "example" dir that will be your new project's root.
  • create build dir
  • create build/classes dir
  • create dist dir
  • create src dir
  • create WEB-INF dir
  • create WEB-INF/lib dir

then I added a few test files (by copying it from the apache tomcat examples)

  • HelloMaster.java (renamed HelloWorldExample.java) to src/
  • LocalString*.properties to WEB-INF/classes
  • create an empty build.xml file in the "example"-dir
  • create an empty web.xml file in the WEB-INF dir

The project structure should then look as follows:

~/example$ tree .
├── build
│   └── classes
│     
├── build.xml
├── dist
│   
├── src
│   └── HelloMaster.java
└── WEB-INF
    ├── classes
    │   ├── LocalStrings_en.properties
    │   ├── LocalStrings_es.properties
    │   ├── LocalStrings_fr.properties
    │   ├── LocalStrings.properties
    │   └── LocalStrings_pt.properties
    ├── lib
    └── web.xml

Then I've added the following to the WEB-INF/web.xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
     version="2.4">
  <servlet>
    <servlet-name>HelloMaster</servlet-name>
    <servlet-class>HelloMaster</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloMaster</servlet-name>
    <url-pattern>/hellomaster</url-pattern>
  </servlet-mapping>
</web-app>

the build.xml is the one with the most meat to it:

<?xml version="1.0" ?> 
<project name="AntExample1" default="war">

  <!-- hack! setting classpath explicitly to tomcat's classes -->
  <!-- export CLASSPATH=$CLASSPATH:/path_to_apache_tomcat_classes/classes/ -->  

  <path id="compile.classpath">
    <fileset dir="WEB-INF/lib">
      <include name="*.jar"/>
    </fileset>
  </path>

  <target name="init">
    <mkdir dir="build/classes"/>
    <mkdir dir="dist" />
  </target>

  <target name="compile" depends="init" >
    <javac destdir="build/classes" debug="true" srcdir="src">
      <classpath refid="compile.classpath"/>
    </javac>
  </target>

  <target name="war" depends="compile">
    <war destfile="dist/AntExample.war" webxml="WEB-INF/web.xml">
      <fileset dir=""/>
      <lib dir="WEB-INF/lib"/>
      <classes dir="build/classes"/>
    </war>
  </target>

  <target name="clean">
    <delete dir="dist" />
    <delete dir="build" />
  </target>  
</project>

Note I don't know how to set the classpath yet without setting it explicitly:

 > export CLASSPATH=$CLASSPATH:/path_to_apache_tomcat_classes/classes/    
 > ant  #-- this should create the war file in the dist dir

The war file should then be named dist/AntExample.war and you can copy it to your webapps directory

Community
  • 1
  • 1
stephanve
  • 94
  • 5
0

I currently use tomcat without eclipse. I'm not sure that an IDE buys you much except in coding spiffier front ends. Thats an opinion that not many may share. I'm not an expert web programmer either, i'm kind of a jack of all trades programmer.

Tips: First thing, always use your linux distribution's mechanisms and package manangement system to install/manage your software, in this case tomcat and associated libs. My dev box is Fedora 17, so some of what I do is probably fedora centric.

Generally, you make warfiles (like a tar archive) and deploy them to a java application server or container like tomcat. For servlets only, here's what I do in batch script. Generally speaking all of these servers have common API's and then some stuff thats proprietary. In theory if you stick to the common API stuff your servlet should run on most java app servers.

  1. Find the api's you need to compile against, where foo.java is a POJO class used by your servlets: javac -cp /usr/share/java/tomcat6-servlet-api.jar foo.java servlet1.java servlet2.java

  2. Move the compiled classes to your warfile building spot. cp *.class build/WEB-INF/classes/

Its a good idea to rm *.class before you build.

  1. Go into that dir (i name it build usually) and jar cvf myfoo.war * (look for how to make a web.xml file, there's a particular dir hierarchy you must have)

  2. Deploy your warfile, what I do to help out the hot deploy mechanism is rm -rf all the old warfile and warfile cruftifoo and then I

sudo cp myfoo.war /var/lib/tomcat6/webapps/

I'm considering servicing the tomcat (start and stop) because the hot deploy sometimes doesn't.

Mugster
  • 11
  • 1