Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, August 3, 2008

Making Executable JAR

As a Java programmer, you probably need to make JAR file, a compressed file which contains all necessary .class files, like tar (a widely used file format in Unix). For some reasons, you may want to make executable JAR file, which can run by "java -jar filename.jar" or just double click it on Windows. Here are the steps about how to make an executable JAR.

Suppose these are the source code:
src/com/wjb/goose.java
src/com/wjb/dog.java
src/com/wjb/zoo.java
Each source file contains only one class (except inner class) and belongs to the package "com.wjb".

For convenience, you'd better put the .class file into another directory, for example, classes. Here, directory classes and src are in the save level. The best way to do it is using "-d" argument. Open the terminal (or "command line prompt" on Windows), typing these commands:
cd src # Enter source code directory
javac -d ../classes com/wjb/goose.java
javac -d ../classes com/wjb/dog.java
javac -d ../classes com/wjb/zoo.java
-d ../classes means put the .class file into ../classes directory and build proper sub-directories according to the package name. For this example, it will make classes/com/wjb and all the class file will be put there.

Now suppose the Zoo class has the main method, i.e. it is the starting point of the whole programs. If you want to make executable JAR, you must specify the class that has the main method. So you need a special file: manifest.txt.

Create a text file named manifest.txt and save it in "classes" directory. This file has only one line:
Main-Class: com.wjb.Zoo
Note that there is a blank character after the colon ":" and you must use its full class name (containing package name).

Next, go to "classes" directory and execute this command:
jar -cmvf manifest.txt zoo.jar com
If everything is OK, the zoo.jar will be created. It is executable. You can use "java -jar zoo.jar" to test it or just double click its icon if you are using Windows.

In addition, the simple usage of "jar" to create a JAR file is:
jar -cmvf <manifest.txt> <filename.jar> <input files>

Here, "<input files>" are files or directories. You can specify more than one file or directory if you want to add many files into JAR. If you need the complete reference, please read the jar manual.

Saturday, July 12, 2008

Installing Tomcat on Ubuntu Linux

I installed Tomcat on my laptop which runs Ubuntu Linux. APT package manager makes it very easy to install many software packages.

Firstly, make sure that you have configured software sources correctly. Go to "System"--"Administration"--"Software Sources" and do it as below:
Screenshot-Software Sources
If you skip this step, apt-get may not find tomcat package.

Before installing tomcat, I have to install Java programming environment. Just use "sudo apt-get install sun-java6-jdk sun-java6-jre" to install JDK and JRE. I suggest choosing Sun JDK or JRE. If you have more than one, you'd better use "sudo update-alternatives --config java" and "sudo update-alternatives --config javac" to choose Sun Java interpretor and compiler as default.

Then you can install tomcat-5.5 with "sudo apt-get install tomcat5.5". After that, Tomcat will start. The default port is 8180. i.e. you can visit http://localhost:8180 to test your tomcat. If you see a blank page, it means tomcat works.

One of configure files is /etc/tomcat5.5/server.xml. I want to make tomcat use port 8080. Open it and find:
<Connector port="8180" maxHttpHeaderSize="8192" .... />
Change ""8180" to "8080", save it and use "sudo /etc/init.d/tomcat5.5 restart" to restart tomcat.

The last step you should specify a directory as web root directory. Go to /etc/tomcat5.5/Catalina/localhost directory, add an XML file (for example: ROOT.xml) which contains:
<Context path="/" docBase="/home/albert/Programs/JavaEE" reloadable="true" />
Put a JSP file (test.jsp) in /home/albert/Programs/JavaEE/, which contains:
<% out.println("It works"); %>
Restart tomcat and visit http://localhost:8080/test.jsp to test it.