====== 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 [[javatut>deployment/webstart/developing.html|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 [[javatut>uiswing/start/compile.html|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
[[javatut>/java/package/|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 ==
See also Tutorial: [[http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html|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:
Info Title
Info Vendor
jws.html contains:
Launch Webstart Application
FIXME
===== Create an Applet ==
Create ''applet.manifest'':
Main-Class: pjp.PjpApplet
FIXME add Ant task
Note that there are [[javatut>deployment/applet/deployingApplet.html|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