Benutzer-Werkzeuge

Webseiten-Werkzeuge


becki:linux:java_plumbing

Java Plumbing

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.

Deploy code both as Applet and Application

FIXME Replace with newer code and add file names!

It is advisable to separate core functionality from the final deployment mechanism:

Appliction specific Code

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();
            }
        });
    }
}

FIXME The usage of SwingUtilities.invokeLater is used by Sun as well.

Applet specific Code

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");
        }
    }
}

Shared Code

import javax.swing.*;
 
public class MyContent extends JPanel {
    private JLabel myLabel;
 
    MyContent() {
        myLabel= new JLabel("¡Hola Mundo!");
        add(myLabel);
    }
}

Separate code from generated classes

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

Working with Packages

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 package statement must be the first line in the source file!

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
Rule of thumb: The sourcepath, destinationpath and classpath statements end where the package statement starts

More on packages

Create and use a Jar file

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

Use Apache Ant for building

<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

Use Java Web Start

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>

FIXME

Create an Applet

Create applet.manifest:

Main-Class: pjp.PjpApplet

FIXME 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!

Using external Libs

FIXME http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html#ext-libs FIXME http://ant.apache.org/manual/dirtasks.html (Manual → Ant Tasks → Core Tasks → Concepts and Types → Directory-based Tasks - How very easy to find!!)

Use Unit Tests

FIXME

Cookies helfen bei der Bereitstellung von Inhalten. Diese Website verwendet Cookies. Mit der Nutzung der Website erklären Sie sich damit einverstanden, dass Cookies auf Ihrem Computer gespeichert werden. Außerdem bestätigen Sie, dass Sie unsere Datenschutzerklärung gelesen und verstanden haben. Wenn Sie nicht einverstanden sind, verlassen Sie die Website. Weitere Information
becki/linux/java_plumbing.txt · Zuletzt geändert: 2010-04-06 15:56 von becki

Impressum - Datenschutzerklärung