Step by step instructions of how to build and run java code as a normal application, as a java web start application and as an applet.
Replace with newer code and add file names!
It is advisable to separate core functionality from the final deployment mechanism:
import javax.swing.*; public class MyApplication extends JFrame { private void createAndShowGUI() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("My App Title"); JPanel panel= new MyContent(); /* get app-specific content */ getContentPane().add(panel); /* and put it into app frame */ pack(); setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { MyApplication frame = new MyApplication(); frame.createAndShowGUI(); } }); } }
The usage of SwingUtilities.invokeLater
is used by Sun as well.
import javax.swing.*; public class MyApplet extends JApplet { public void init() { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { JPanel panel= new MyContent();/* create app-specific */ add(panel); /* content and put it into app frame */ } }); } catch (Exception e) { System.err.println("createGUI didn't complete successfully"); } } }
import javax.swing.*; public class MyContent extends JPanel { private JLabel myLabel; MyContent() { myLabel= new JLabel("¡Hola Mundo!"); add(myLabel); } }
Structure:
. |-- build | `-- classes | |-- PjpApplication.class | `-- PropsPanel.class `-- src `-- pjp |-- PjpApplication.java `-- PropsPanel.java
The compile command is: (The destination path must exist!)
javac -sourcepath src/pjp -d build/classes src/pjp/PjpApplication.java
The run command is:
java -cp build/classes PjpApplication
Structure:
. |-- build | `-- classes | `-- pjp | |-- PjpApplication.class | `-- PropsPanel.class `-- src `-- pjp |-- PjpApplication.java `-- PropsPanel.java
Include package pjp;
at the beginning of both source files.
The compile command is: (Note that the dir pjp
in build/classes
ist generated automatically)
javac -sourcepath src -d build/classes/ src/pjp/PjpApplication.java
The run command is:
java -cp build/classes pjp.PjpApplication
Create a dir for jar files: mkfile build/jar
Add a manifest file, e.g. application.manifest
and specify the main class as entry point:
Main-Class: pjp.PjpApplication
Structure:
. |-- application.manifest |-- build | |-- classes | `-- jar `-- src `-- pjp |-- PjpApplication.java `-- PropsPanel.java
Compile the sources like above and then create the jar file:
jar cfm build/jar/pjpApplication.jar application.manifest -C build/classes .
-C build/classes .
means: Cd to build/classes and archive all files there. (Just type jar
to get jar help)
The run command is:
java -jar build/jar/pjpApplication.jar
<project name="Pjp" default="maintarget"> <property name="src.dir" value="src"/> <property name="build.dir" value="build"/> <property name="classes.dir" value="${build.dir}/classes"/> <property name="jar.dir" value="${build.dir}/jar"/> <property name="application" value="${ant.project.name}Application"/> <target name="clean"> <delete dir="${build.dir}"/> </target> <target name="compile"> <mkdir dir="${classes.dir}"/> <javac srcdir="${src.dir}" destdir="${classes.dir}"/> </target> <target name="jar" depends="compile"> <mkdir dir="${jar.dir}"/> <jar destfile="${jar.dir}/${application}.jar" basedir="${classes.dir}"> <manifest> <attribute name="Main-Class" value="pjp.${application}"/> </manifest> </jar> </target> <target name="run" depends="jar"> <java jar="${jar.dir}/${application}.jar" fork="true"/> </target> <target name="maintarget" depends="clean,run"/> </project>
See also Tutorial: Hello World with Ant
Project Structure:
. |-- build.xml |-- jws.html |-- pjp-jws.jnlp `-- src `-- pjp |-- PjpApplet.java |-- PjpApplication.java `-- PropsPanel.java
pjp-jws.jnlp:
<?xml version="1.0" encoding="UTF-8"?> <jnlp spec="1.0+" codebase="http://etb-111.kaeser-net.de/~becki/test/printJavaProps" href="pjp-jws.jnlp"> <information> <title>Info Title</title> <vendor>Info Vendor</vendor> </information> <resources> <j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se"/> <jar href="build/jar/PjpApplication.jar" main="true" /> </resources> <application-desc name="application-desc name" main-class="pjp.PjpApplication" width="500" height="200"> </application-desc> <update check="background"/> </jnlp> <xml> jws.html contains: <code html> <a href="pjp-jws.jnlp">Launch Webstart Application</a>
Create applet.manifest
:
Main-Class: pjp.PjpApplet
add Ant task
<applet code="pjp.PjpApplet" archive="build/jar/pjpApplet.jar" width="500" height="200"></applet>
Note that there are more sophisticated ways to deploy an applet!
http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html#ext-libs http://ant.apache.org/manual/dirtasks.html (Manual → Ant Tasks → Core Tasks → Concepts and Types → Directory-based Tasks - How very easy to find!!)