Using JOGL in Java Web Start: Difference between revisions

From JogampWiki
Jump to navigation Jump to search
(→‎Testing your program outside JWS: use the native jar lib loading mech, no hassle to deal w/ native libs manually)
 
(18 intermediate revisions by 2 users not shown)
Line 3: Line 3:
= Base class =
= Base class =


First, a base class that we've used before [[Using JOGL in AWT SWT and Swing|here]]. This class draws one triangle, abstracting out all the pure OpenGL calls that don't depend on the choice of window toolkit.
First, a base class that we've used before [[Using JOGL in AWT SWT and Swing|here]]. This class draws one triangle, abstracting out all the pure OpenGL calls that don't depend on the choice of window toolkit. Save this file as <tt>OneTriangle.java</tt>.


<pre>
<pre>
Line 46: Line 46:
= Drawing a triangle with AWT =
= Drawing a triangle with AWT =


Now, a class that draws the triangle in an AWT Frame. Java Web Start can use any Java windowing toolkit, we've just chosen this one for convenience.
Now, a class that draws the triangle in an AWT Frame. Java Web Start can use any Java windowing toolkit, we've just chosen this one for convenience. Save this file as <tt>OneTriangleAWT.java.</tt>


<pre>
<pre>
Line 67: Line 67:
  */
  */
public class OneTriangleAWT {
public class OneTriangleAWT {
    static {
        // setting this true causes window events not to get sent on Linux if you run from inside Eclipse
        GLProfile.initSingleton( false );
    }


     public static void main( String [] args ) {
     public static void main( String [] args ) {
Line 119: Line 114:
* Create a directory to hold your JWS application.
* Create a directory to hold your JWS application.
* Create a subdirectory <tt>name/wadewalker/jogl2tests/onetriangle</tt> inside your application directory. Or if you changed the packages of the files above, create a subdirectory that matches your package names.
* Create a subdirectory <tt>name/wadewalker/jogl2tests/onetriangle</tt> inside your application directory. Or if you changed the packages of the files above, create a subdirectory that matches your package names.
* Save the files above as <tt>OneTriangle.java</tt> and <tt>OneTriangleAWT.java</tt> inside the subdirectory.
* Put your <tt>OneTriangle.java</tt> and <tt>OneTriangleAWT.java</tt> files inside the subdirectory.
* Copy all JOGL JARs into the application directory. You can get the JARs from a JOGL autobuild [http://jogamp.org/deployment/autobuilds/master/ here]. Usually I pick the latest <tt>jogl-<build number>-<date></tt> link, download all the <tt>.7z</tt> files inside, and unzip them with [http://www.7-zip.org/ 7zip]. These are the JARs you should have in your directory:
 
== Get local copies of the JogAmp JAR files (optional) ==
 
A Java Web Start app can either use its own local copies of the [{{SERVER}}/jogl/doc/deployment/JOGL-DEPLOYMENT.html#GluegenJARs GlueGen], [{{SERVER}}/jogl/doc/deployment/JOGL-DEPLOYMENT.html#JOGLAllInOneJARs JOGL], and native JAR files, or it can use the copies hosted at jogamp.org.
 
Using local copies of the JogAmp JAR files gives you complete control over which version of JOGL your app uses, and guarantees that version will never change unexpectedly. However, it does require more work on your part to install and sign the JARs, and it increases the load on your server.
 
To get local copies of the JogAmp JAR files, do this:
 
* Read [[Downloading_and_installing_JOGL]] for instructions on how to get the autobuild or released JAR files.
* Copy the following JAR files into the application directory:


<pre>
<pre>
gluegen-rt.jar
gluegen-rt.jar
nativewindow.all.jar
jogl-all.jar
jogl.all.jar
newt.all.jar


gluegen-rt-natives-windows-i586.jar
gluegen-rt-natives-windows-i586.jar
jogl-natives-windows-i586.jar
jogl-all-natives-windows-i586.jar
nativewindow-natives-windows-i586.jar
newt-natives-windows-i586.jar


gluegen-rt-natives-windows-amd64.jar
gluegen-rt-natives-windows-amd64.jar
jogl-natives-windows-amd64.jar
jogl-all-natives-linux-amd64.jar
nativewindow-natives-windows-amd64.jar
newt-natives-windows-amd64.jar


gluegen-rt-natives-linux-i586.jar
gluegen-rt-natives-linux-i586.jar
jogl-natives-linux-i586.jar
jogl-all-natives-linux-i586.jar
nativewindow-natives-linux-i586.jar
newt-natives-linux-i586.jar


gluegen-rt-natives-linux-amd64.jar
gluegen-rt-natives-linux-amd64.jar
jogl-natives-linux-amd64.jar
jogl-all-natives-linux-amd64.jar
nativewindow-natives-linux-amd64.jar
newt-natives-linux-amd64.jar


gluegen-rt-natives-macosx-universal.jar
gluegen-rt-natives-macosx-universal.jar
jogl-natives-macosx-universal.jar
jogl-all-natives-macosx-universal.jar
nativewindow-natives-macosx-universal.jar
newt-natives-macosx-universal.jar
</pre>
</pre>


Line 161: Line 154:


<pre>
<pre>
javac -classpath "gluegen-rt.jar;nativewindow.all.jar;jogl.all.jar;newt.all.jar" name\wadewalker\jogl2tests\onetriangle\*.java
javac -classpath "gluegen-rt.jar;jogl-all.jar" name\wadewalker\jogl2tests\onetriangle\*.java
</pre>
</pre>


Line 167: Line 160:


<pre>
<pre>
javac -classpath "gluegen-rt.jar:nativewindow.all.jar:jogl.all.jar:newt.all.jar" name\wadewalker\jogl2tests\onetriangle\*.java
javac -classpath "gluegen-rt.jar:jogl-all.jar" name\wadewalker\jogl2tests\onetriangle\*.java
</pre>
</pre>


Line 178: Line 171:
= Testing your program outside JWS =
= Testing your program outside JWS =


It's probably a good idea to test your program outside JWS first, to make sure it does what you think it should. To do that, you'll need to unzip the native binaries so Java can see them. Since I ran first on Windows 7 64-bit, I unzipped all the <tt>*-natives-windows-amd64.jar</tt> files, the collected the DLLs inside into a directory called <tt>lib-windows-amd64</tt>. Since I test on the other platforms too, I did the same for the other four platforms, creating four more directories <tt>lib-windows-i586</tt>, <tt>lib-linux-i586</tt>, <tt>lib-linux-amd64</tt>, and <tt>lib-macosx-universal</tt>.
It's probably a good idea to test your program outside JWS first, to make sure it does what you think it should.


Then to test your program on 64-bit Windows, cd to your application directory and type
We utilize the [{{SERVER}}/jogl/doc/userguide/index.html#automatednativelibraryloading automated native library loading mechanism], hence we do not bother to hassle with the native libraries ourselves.
 
Then to test your program, cd to your application directory and type


<pre>
<pre>
java -classpath "gluegen-rt.jar;nativewindow.all.jar;jogl.all.jar;newt.all.jar;onetriangle.jar" -Djava.library.path=lib-windows-amd64 name.wadewalker.jogl2tests.onetriangle.OneTriangleAWT
java -classpath "gluegen-rt.jar;jogl-all.jar;onetriangle.jar" name.wadewalker.jogl2tests.onetriangle.OneTriangleAWT
</pre>
</pre>


On 64-bit Linux, type
The result should look like this:
 
[[File:JWS-cmdline-test.png|300px|thumb|none|JWS OneTriangle test]]
 
= Writing the JNLP files =
 
In general we will have one JNLP file for each JWS application, which will reference the JOGL extension's JNLP file.
The latter may reference even more JNLP files, but this does not matter in this discussion. Read the JWS [http://download.oracle.com/javase/1,5.0/docs/guide/javaws/developersguide/syntax.html Java Network Launching Protocol (JNLP)] specification for details of the JNLP syntax.
 
== Option 1: Use JogAmp's hosted JAR and JNLP files ==
 
If you're using JogAmp's hosted JAR and JNLP files, you only have to write your application's JNLP file, since it will reference JogAmp's official release as an extension.


<pre>
<pre>
java -classpath "gluegen-rt.jar:nativewindow.all.jar:jogl.all.jar:newt.all.jar:onetriangle.jar" -Djava.library.path=lib-linux-amd64 name.wadewalker.jogl2tests.onetriangle.OneTriangleAWT
<?xml version="1.0" encoding="utf-8"?>
</pre>
<jnlp spec="1.0+" codebase="./"
  href="OneTriangleWebStart.jnlp">
 
  <information>
    <title>OneTriangle Demo</title>
    <vendor>JogAmp Community</vendor>
    <homepage href="http://jogamp.org/"/>
    <description>OneTriangle Java Web Start Demo</description>
    <description kind="short">The simplest possible JOGL Java Web Start demo - draws one triangle.</description>
    <offline-allowed/>
  </information>
  <update check="background" policy="always"/>


On Mac OS X, type
  <resources>
    <j2se href="http://java.sun.com/products/autodl/j2se" version="1.4+"/>
    <property name="sun.java2d.noddraw" value="true"/>
    <extension name="jogl-all-awt" href="http://jogamp.org/deployment/jogamp-current/jogl-all-awt.jnlp" />
    <jar href="onetriangle.jar" main="true"/>
  </resources>


<pre>
  <application-desc main-class="name.wadewalker.jogl2tests.onetriangle.OneTriangleAWT">
java -classpath "gluegen-rt.jar:nativewindow.all.jar:jogl.all.jar:newt.all.jar:onetriangle.jar" -Djava.library.path=lib-macosx-universal name.wadewalker.jogl2tests.onetriangle.OneTriangleAWT
  </application-desc>
</jnlp>
</pre>
</pre>


The result should look like this:
Save this file to your application directory as <tt>OneTriangleWebStart.jnlp</tt>.


[[File:JWS-cmdline-test.png|300px|thumb|none|JWS OneTriangle test]]
If you are using JogAmp's hosted files you can skip the next chapter.


= Writing the JNLP file =
== Option 2: Use local copies of the JogAmp JAR files ==


JWS uses two [http://download.oracle.com/javase/1,5.0/docs/guide/javaws/developersguide/syntax.html Java Network Launching Protocol (JNLP)] files to tell Java where to find the JAR files that make up your application. Our main JNLP file looks like this:
Here we have to write one JNLP file for our application and one for our self-deployed JogAmp extension.


<pre>
<pre>
Line 220: Line 243:
   </information>
   </information>
   <update check="background" policy="always"/>
   <update check="background" policy="always"/>
  <security>
    <all-permissions/>
  </security>


   <resources>
   <resources>
Line 238: Line 257:


Save this file to your application directory as <tt>OneTriangleWebStart.jnlp</tt>.
Save this file to your application directory as <tt>OneTriangleWebStart.jnlp</tt>.
Our extension JNLP file for the JOGL JARs looks like this:
Our extension JNLP file for the JOGL JARs looks like this:


Line 261: Line 281:
   <resources>
   <resources>
     <jar href="gluegen-rt.jar" />
     <jar href="gluegen-rt.jar" />
    <jar href="nativewindow.all.jar" />
     <jar href="jogl-all.jar" />
     <jar href="jogl.all.jar" />
    <jar href="newt.all.jar" />
   </resources>
   </resources>


   <resources os="Windows" arch="x86">
   <resources os="Windows" arch="x86">
     <nativelib href = "gluegen-rt-natives-windows-i586.jar" />
     <nativelib href = "gluegen-rt-natives-windows-i586.jar" />
     <nativelib href = "jogl-natives-windows-i586.jar" />
     <nativelib href = "jogl-all-natives-windows-i586.jar" />
    <nativelib href = "nativewindow-natives-windows-i586.jar" />
    <nativelib href = "newt-natives-windows-i586.jar" />
   </resources>
   </resources>
   <resources os="Windows" arch="amd64">
   <resources os="Windows" arch="amd64">
     <nativelib href = "gluegen-rt-natives-windows-amd64.jar" />
     <nativelib href = "gluegen-rt-natives-windows-amd64.jar" />
     <nativelib href = "jogl-natives-windows-amd64.jar" />
     <nativelib href = "jogl-all-natives-windows-amd64.jar" />
    <nativelib href = "nativewindow-natives-windows-amd64.jar" />
    <nativelib href = "newt-natives-windows-amd64.jar" />
   </resources>
   </resources>
   <resources os="Windows" arch="x86_64">
   <resources os="Windows" arch="x86_64">
     <nativelib href = "gluegen-rt-natives-windows-amd64.jar" />
     <nativelib href = "gluegen-rt-natives-windows-amd64.jar" />
     <nativelib href = "jogl-natives-windows-amd64.jar" />
     <nativelib href = "jogl-all-natives-windows-amd64.jar" />
    <nativelib href = "nativewindow-natives-windows-amd64.jar" />
    <nativelib href = "newt-natives-windows-amd64.jar" />
   </resources>
   </resources>
   <resources os="Linux" arch="i386">
   <resources os="Linux" arch="i386">
     <nativelib href = "gluegen-rt-natives-linux-i586.jar" />
     <nativelib href = "gluegen-rt-natives-linux-i586.jar" />
     <nativelib href = "jogl-natives-linux-i586.jar" />
     <nativelib href = "jogl-all-natives-linux-i586.jar" />
    <nativelib href = "nativewindow-natives-linux-i586.jar" />
    <nativelib href = "newt-natives-linux-i586.jar" />
   </resources>
   </resources>
   <resources os="Linux" arch="x86">
   <resources os="Linux" arch="x86">
     <nativelib href = "gluegen-rt-natives-linux-i586.jar" />
     <nativelib href = "gluegen-rt-natives-linux-i586.jar" />
     <nativelib href = "jogl-natives-linux-i586.jar" />
     <nativelib href = "jogl-all-natives-linux-i586.jar" />
    <nativelib href = "nativewindow-natives-linux-i586.jar" />
    <nativelib href = "newt-natives-linux-i586.jar" />
   </resources>
   </resources>
   <resources os="Linux" arch="amd64">
   <resources os="Linux" arch="amd64">
     <nativelib href = "gluegen-rt-natives-linux-amd64.jar" />
     <nativelib href = "gluegen-rt-natives-linux-amd64.jar" />
     <nativelib href = "jogl-natives-linux-amd64.jar" />
     <nativelib href = "jogl-all-natives-linux-amd64.jar" />
    <nativelib href = "nativewindow-natives-linux-amd64.jar" />
    <nativelib href = "newt-natives-linux-amd64.jar" />
   </resources>
   </resources>
   <resources os="Linux" arch="x86_64">
   <resources os="Linux" arch="x86_64">
     <nativelib href = "gluegen-rt-natives-linux-amd64.jar" />
     <nativelib href = "gluegen-rt-natives-linux-amd64.jar" />
     <nativelib href = "jogl-natives-linux-amd64.jar" />
     <nativelib href = "jogl-all-natives-linux-amd64.jar" />
    <nativelib href = "nativewindow-natives-linux-amd64.jar" />
    <nativelib href = "newt-natives-linux-amd64.jar" />
   </resources>
   </resources>
   <resources os="Mac OS X" arch="i386">
   <resources os="Mac OS X" arch="i386">
     <nativelib href = "gluegen-rt-natives-macosx-universal.jar" />
     <nativelib href = "gluegen-rt-natives-macosx-universal.jar" />
     <nativelib href = "jogl-natives-macosx-universal.jar" />
     <nativelib href = "jogl-all-natives-macosx-universal.jar" />
    <nativelib href = "nativewindow-natives-macosx-universal.jar" />
    <nativelib href = "newt-natives-macosx-universal.jar" />
   </resources>
   </resources>
   <resources os="Mac OS X" arch="x86_64">
   <resources os="Mac OS X" arch="x86_64">
     <nativelib href = "gluegen-rt-natives-macosx-universal.jar" />
     <nativelib href = "gluegen-rt-natives-macosx-universal.jar" />
     <nativelib href = "jogl-natives-macosx-universal.jar" />
     <nativelib href = "jogl-all-natives-macosx-universal.jar" />
    <nativelib href = "nativewindow-natives-macosx-universal.jar" />
    <nativelib href = "newt-natives-macosx-universal.jar" />
   </resources>
   </resources>


Line 329: Line 329:
Note that the <tt>codebase</tt> values are set to the current working directory, and the <tt>href</tt> values are simple filenames. This is so we can test the JNLP app locally, without using a web server. When we put the app on a web server later, the <tt>codebase</tt> and <tt>href</tt> become full URLs.
Note that the <tt>codebase</tt> values are set to the current working directory, and the <tt>href</tt> values are simple filenames. This is so we can test the JNLP app locally, without using a web server. When we put the app on a web server later, the <tt>codebase</tt> and <tt>href</tt> become full URLs.


= Signing your JARs =
=== Signing your local copies of the JogAmp JARs ===


For the JWS app to work properly in a web browser, all its JARs must be signed with the same key. If you don't already have a key you want to use, you can create one like this
For the JWS app to work properly in a web browser, all its JARs must be signed with the same key. Note that none of your applet JAR files needs to be signed. Only the JogAmp files (GlueGen, JOGL, et cetera) require code signing.
 
If you don't already have a key you want to use, you can create one like this


<tt>keytool -genkey -keystore testKeys -alias ww</tt>
<tt>keytool -genkey -keystore testKeys -alias ww</tt>
Line 338: Line 340:


<pre>
<pre>
jarsigner -keystore testKeys onetriangle.jar ww
jarsigner -keystore testKeys gluegen-rt.jar ww
jarsigner -keystore testKeys gluegen-rt.jar ww
jarsigner -keystore testKeys nativewindow.all.jar ww
jarsigner -keystore testKeys jogl-all.jar ww
jarsigner -keystore testKeys jogl.all.jar ww
jarsigner -keystore testKeys newt.all.jar ww


jarsigner -keystore testKeys gluegen-rt-natives-windows-i586.jar ww
jarsigner -keystore testKeys gluegen-rt-natives-windows-i586.jar ww
jarsigner -keystore testKeys jogl-natives-windows-i586.jar ww
jarsigner -keystore testKeys jogl-all-natives-windows-i586.jar ww
jarsigner -keystore testKeys nativewindow-natives-windows-i586.jar ww
jarsigner -keystore testKeys newt-natives-windows-i586.jar ww


jarsigner -keystore testKeys gluegen-rt-natives-windows-amd64.jar ww
jarsigner -keystore testKeys gluegen-rt-natives-windows-amd64.jar ww
jarsigner -keystore testKeys jogl-natives-windows-amd64.jar ww
jarsigner -keystore testKeys jogl-all-natives-windows-amd64.jar ww
jarsigner -keystore testKeys nativewindow-natives-windows-amd64.jar ww
jarsigner -keystore testKeys newt-natives-windows-amd64.jar ww


jarsigner -keystore testKeys gluegen-rt-natives-linux-i586.jar ww
jarsigner -keystore testKeys gluegen-rt-natives-linux-i586.jar ww
jarsigner -keystore testKeys jogl-natives-linux-i586.jar ww
jarsigner -keystore testKeys jogl-all-natives-linux-i586.jar ww
jarsigner -keystore testKeys nativewindow-natives-linux-i586.jar ww
jarsigner -keystore testKeys newt-natives-linux-i586.jar ww


jarsigner -keystore testKeys gluegen-rt-natives-linux-amd64.jar ww
jarsigner -keystore testKeys gluegen-rt-natives-linux-amd64.jar ww
jarsigner -keystore testKeys jogl-natives-linux-amd64.jar ww
jarsigner -keystore testKeys jogl-all-natives-linux-amd64.jar ww
jarsigner -keystore testKeys nativewindow-natives-linux-amd64.jar ww
jarsigner -keystore testKeys newt-natives-linux-amd64.jar ww


jarsigner -keystore testKeys gluegen-rt-natives-macosx-universal.jar ww
jarsigner -keystore testKeys gluegen-rt-natives-macosx-universal.jar ww
jarsigner -keystore testKeys jogl-natives-macosx-universal.jar ww
jarsigner -keystore testKeys jogl-all-natives-macosx-universal.jar ww
jarsigner -keystore testKeys nativewindow-natives-macosx-universal.jar ww
jarsigner -keystore testKeys newt-natives-macosx-universal.jar ww
</pre>
</pre>


Line 377: Line 365:
To run the JWS app locally without a web server, cd to the application directory and type
To run the JWS app locally without a web server, cd to the application directory and type


<tt>javaws OneTriangleWebStart.jnlp</tt>
<pre>javaws OneTriangleWebStart.jnlp</pre>


This runs the app with the JWS launcher. The result should look just like when we ran the app without the launcher, but there'll be a dialog box that pops up first asking the user's permission to load the app. If you have any JAR signing problems, they should show up at this point.
This runs the app with the JWS launcher. The result should look just like when we ran the app without the launcher, but there'll be a dialog box that pops up first asking the user's permission to load the app. If you have any JAR signing problems, they should show up at this point.
Line 383: Line 371:
= Clearing the JWS cache =
= Clearing the JWS cache =


JWS caches JAR and JNLP files to reduce load time. Unfortunately, if you edit one of these files and re-launch, sometimes you don't see your changes because JWS is still using the cached copy. To clear the JWS cache, type
JWS caches JAR and JNLP files to reduce load time. Unfortunately, if you edit one of these files and re-launch, sometimes you won't see your changes because JWS is still using the cached copy. To clear the JWS cache, type


<tt>javaws -Xclearcache</tt>
<pre>javaws -Xclearcache</pre>


=Running the JWS application from a web server=
=Running the JWS application from a web server=


'''This section is unfinished'''
'''*This section is unfinished*'''


Once you're sure that the JWS application launches correctly from the local file system, you're ready to deploy it to a web server.
Once you're sure that the JWS application launches correctly from the local file system, you're ready to deploy it to a web server.


* Change the <tt>codebase</tt> and <tt>href</tt> entries in the JNLP to URLs referring to a new application directory on the web server
* Create or choose an application directory on your web server.
* Copy all the JAR and JNLP files to the new directory on your web server
* Change the <tt>codebase</tt> entries in the JNLP from directory names to URLs that refer to the application directory on your web server.
* Put a link to the OneTriangleWebStart.jnlp file on a page on your web server (more details [http://download.oracle.com/javase/6/docs/technotes/guides/javaws/developersguide/launch.html here)
* Copy all the JAR and JNLP files from your local application directory to the application directory on your web server.
* Put a link to the <tt>OneTriangleWebStart.jnlp</tt> file on a page on your web server (more details [http://download.oracle.com/javase/6/docs/technotes/guides/javaws/developersguide/launch.html here]).

Latest revision as of 00:03, 21 August 2012

You can use JOGL in a Java Web Start (JWS) application, which lets you launch a full-featured, standalone Java program from a web page. This page shows an example of how to do this. The example program just draws one triangle that fills a resizable window.

Base class

First, a base class that we've used before here. This class draws one triangle, abstracting out all the pure OpenGL calls that don't depend on the choice of window toolkit. Save this file as OneTriangle.java.

package name.wadewalker.jogl2tests.onetriangle;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.glu.GLU;

public class OneTriangle {
    protected static void setup( GL2 gl2, int width, int height ) {
        gl2.glMatrixMode( GL2.GL_PROJECTION );
        gl2.glLoadIdentity();

        // coordinate system origin at lower left with width and height same as the window
        GLU glu = new GLU();
        glu.gluOrtho2D( 0.0f, width, 0.0f, height );

        gl2.glMatrixMode( GL2.GL_MODELVIEW );
        gl2.glLoadIdentity();

        gl2.glViewport( 0, 0, width, height );
    }

    protected static void render( GL2 gl2, int width, int height ) {
        gl2.glClear( GL.GL_COLOR_BUFFER_BIT );

        // draw a triangle filling the window
        gl2.glLoadIdentity();
        gl2.glBegin( GL.GL_TRIANGLES );
        gl2.glColor3f( 1, 0, 0 );
        gl2.glVertex2f( 0, 0 );
        gl2.glColor3f( 0, 1, 0 );
        gl2.glVertex2f( width, 0 );
        gl2.glColor3f( 0, 0, 1 );
        gl2.glVertex2f( width / 2, height );
        gl2.glEnd();
    }
}

Drawing a triangle with AWT

Now, a class that draws the triangle in an AWT Frame. Java Web Start can use any Java windowing toolkit, we've just chosen this one for convenience. Save this file as OneTriangleAWT.java.

package name.wadewalker.jogl2tests.onetriangle;

import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.awt.GLCanvas;

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * A minimal program that draws with JOGL in an AWT Frame.
 *
 * @author Wade Walker
 */
public class OneTriangleAWT {

    public static void main( String [] args ) {
        GLProfile glprofile = GLProfile.getDefault();
        GLCapabilities glcapabilities = new GLCapabilities( glprofile );
        final GLCanvas glcanvas = new GLCanvas( glcapabilities );

        glcanvas.addGLEventListener( new GLEventListener() {
            
            @Override
            public void reshape( GLAutoDrawable glautodrawable, int x, int y, int width, int height ) {
                OneTriangle.setup( glautodrawable.getGL().getGL2(), width, height );
            }
            
            @Override
            public void init( GLAutoDrawable glautodrawable ) {
            }
            
            @Override
            public void dispose( GLAutoDrawable glautodrawable ) {
            }
            
            @Override
            public void display( GLAutoDrawable glautodrawable ) {
                OneTriangle.render( glautodrawable.getGL().getGL2(), glautodrawable.getWidth(), glautodrawable.getHeight() );
            }
        });

        final Frame frame = new Frame( "One Triangle AWT" );
        frame.add( glcanvas );
        frame.addWindowListener( new WindowAdapter() {
            public void windowClosing( WindowEvent windowevent ) {
                frame.remove( glcanvas );
                frame.dispose();
                System.exit( 0 );
            }
        });

        frame.setSize( 640, 480 );
        frame.setVisible( true );
    }
}

Setting up the application directory

  • Create a directory to hold your JWS application.
  • Create a subdirectory name/wadewalker/jogl2tests/onetriangle inside your application directory. Or if you changed the packages of the files above, create a subdirectory that matches your package names.
  • Put your OneTriangle.java and OneTriangleAWT.java files inside the subdirectory.

Get local copies of the JogAmp JAR files (optional)

A Java Web Start app can either use its own local copies of the GlueGen, JOGL, and native JAR files, or it can use the copies hosted at jogamp.org.

Using local copies of the JogAmp JAR files gives you complete control over which version of JOGL your app uses, and guarantees that version will never change unexpectedly. However, it does require more work on your part to install and sign the JARs, and it increases the load on your server.

To get local copies of the JogAmp JAR files, do this:

  • Read Downloading_and_installing_JOGL for instructions on how to get the autobuild or released JAR files.
  • Copy the following JAR files into the application directory:
gluegen-rt.jar
jogl-all.jar

gluegen-rt-natives-windows-i586.jar
jogl-all-natives-windows-i586.jar

gluegen-rt-natives-windows-amd64.jar
jogl-all-natives-linux-amd64.jar

gluegen-rt-natives-linux-i586.jar
jogl-all-natives-linux-i586.jar

gluegen-rt-natives-linux-amd64.jar
jogl-all-natives-linux-amd64.jar

gluegen-rt-natives-macosx-universal.jar
jogl-all-natives-macosx-universal.jar

Compiling and JARing your program

This step assumes you have Java set up so you can use it from the command line. For instructions on this, see here.

To compile the program on Windows, cd to your application directory and type

javac -classpath "gluegen-rt.jar;jogl-all.jar" name\wadewalker\jogl2tests\onetriangle\*.java

To compile the program on Linux or Mac OS X, cd to your application directory and type

javac -classpath "gluegen-rt.jar:jogl-all.jar" name\wadewalker\jogl2tests\onetriangle\*.java

To JAR the program, type

jar cvf onetriangle.jar name\wadewalker\jogl2tests\onetriangle\*.class

Testing your program outside JWS

It's probably a good idea to test your program outside JWS first, to make sure it does what you think it should.

We utilize the automated native library loading mechanism, hence we do not bother to hassle with the native libraries ourselves.

Then to test your program, cd to your application directory and type

java -classpath "gluegen-rt.jar;jogl-all.jar;onetriangle.jar" name.wadewalker.jogl2tests.onetriangle.OneTriangleAWT

The result should look like this:

JWS OneTriangle test

Writing the JNLP files

In general we will have one JNLP file for each JWS application, which will reference the JOGL extension's JNLP file. The latter may reference even more JNLP files, but this does not matter in this discussion. Read the JWS Java Network Launching Protocol (JNLP) specification for details of the JNLP syntax.

Option 1: Use JogAmp's hosted JAR and JNLP files

If you're using JogAmp's hosted JAR and JNLP files, you only have to write your application's JNLP file, since it will reference JogAmp's official release as an extension.

<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="./"
  href="OneTriangleWebStart.jnlp">

  <information>
    <title>OneTriangle Demo</title>
    <vendor>JogAmp Community</vendor>
    <homepage href="http://jogamp.org/"/>
    <description>OneTriangle Java Web Start Demo</description>
    <description kind="short">The simplest possible JOGL Java Web Start demo - draws one triangle.</description>
    <offline-allowed/>
  </information>
  <update check="background" policy="always"/>

  <resources>
    <j2se href="http://java.sun.com/products/autodl/j2se" version="1.4+"/>
    <property name="sun.java2d.noddraw" value="true"/>
    <extension name="jogl-all-awt" href="http://jogamp.org/deployment/jogamp-current/jogl-all-awt.jnlp" />
    <jar href="onetriangle.jar" main="true"/>
  </resources>

  <application-desc main-class="name.wadewalker.jogl2tests.onetriangle.OneTriangleAWT">
  </application-desc>
</jnlp>

Save this file to your application directory as OneTriangleWebStart.jnlp.

If you are using JogAmp's hosted files you can skip the next chapter.

Option 2: Use local copies of the JogAmp JAR files

Here we have to write one JNLP file for our application and one for our self-deployed JogAmp extension.

<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="./"
  href="OneTriangleWebStart.jnlp">

  <information>
    <title>OneTriangle Demo</title>
    <vendor>JogAmp Community</vendor>
    <homepage href="http://jogamp.org/"/>
    <description>OneTriangle Java Web Start Demo</description>
    <description kind="short">The simplest possible JOGL Java Web Start demo - draws one triangle.</description>
    <offline-allowed/>
  </information>
  <update check="background" policy="always"/>

  <resources>
    <j2se href="http://java.sun.com/products/autodl/j2se" version="1.4+"/>
    <property name="sun.java2d.noddraw" value="true"/>
    <extension name="JOGL" href="JOGL.jnlp" />
    <jar href="onetriangle.jar" main="true"/>
  </resources>

  <application-desc main-class="name.wadewalker.jogl2tests.onetriangle.OneTriangleAWT">
  </application-desc>
</jnlp>

Save this file to your application directory as OneTriangleWebStart.jnlp.

Our extension JNLP file for the JOGL JARs looks like this:

<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="./"
  href="JOGL.jnlp">

  <information>
    <title>JOGL libraries</title>
    <vendor>JogAmp Community</vendor>
    <homepage href="http://jogamp.org/"/>
    <description>JOGL libraries</description>
    <description kind="short">All JARs and native libraries for JOGL.</description>
    <offline-allowed/>
  </information>
  <update check="background" policy="always"/>

  <security>
    <all-permissions/>
  </security>

  <resources>
    <jar href="gluegen-rt.jar" />
    <jar href="jogl-all.jar" />
  </resources>

  <resources os="Windows" arch="x86">
    <nativelib href = "gluegen-rt-natives-windows-i586.jar" />
    <nativelib href = "jogl-all-natives-windows-i586.jar" />
  </resources>
  <resources os="Windows" arch="amd64">
    <nativelib href = "gluegen-rt-natives-windows-amd64.jar" />
    <nativelib href = "jogl-all-natives-windows-amd64.jar" />
  </resources>
  <resources os="Windows" arch="x86_64">
    <nativelib href = "gluegen-rt-natives-windows-amd64.jar" />
    <nativelib href = "jogl-all-natives-windows-amd64.jar" />
  </resources>
  <resources os="Linux" arch="i386">
    <nativelib href = "gluegen-rt-natives-linux-i586.jar" />
    <nativelib href = "jogl-all-natives-linux-i586.jar" />
  </resources>
  <resources os="Linux" arch="x86">
    <nativelib href = "gluegen-rt-natives-linux-i586.jar" />
    <nativelib href = "jogl-all-natives-linux-i586.jar" />
  </resources>
  <resources os="Linux" arch="amd64">
    <nativelib href = "gluegen-rt-natives-linux-amd64.jar" />
    <nativelib href = "jogl-all-natives-linux-amd64.jar" />
  </resources>
  <resources os="Linux" arch="x86_64">
    <nativelib href = "gluegen-rt-natives-linux-amd64.jar" />
    <nativelib href = "jogl-all-natives-linux-amd64.jar" />
  </resources>
  <resources os="Mac OS X" arch="i386">
    <nativelib href = "gluegen-rt-natives-macosx-universal.jar" />
    <nativelib href = "jogl-all-natives-macosx-universal.jar" />
  </resources>
  <resources os="Mac OS X" arch="x86_64">
    <nativelib href = "gluegen-rt-natives-macosx-universal.jar" />
    <nativelib href = "jogl-all-natives-macosx-universal.jar" />
  </resources>

  <component-desc />
</jnlp>

Save this file to your application directory as JOGL.jnlp.

Note that the codebase values are set to the current working directory, and the href values are simple filenames. This is so we can test the JNLP app locally, without using a web server. When we put the app on a web server later, the codebase and href become full URLs.

Signing your local copies of the JogAmp JARs

For the JWS app to work properly in a web browser, all its JARs must be signed with the same key. Note that none of your applet JAR files needs to be signed. Only the JogAmp files (GlueGen, JOGL, et cetera) require code signing.

If you don't already have a key you want to use, you can create one like this

keytool -genkey -keystore testKeys -alias ww

The alias can be anything, I just chose ww for this example. Once you have a key, you can sign the JARs like this:

jarsigner -keystore testKeys gluegen-rt.jar ww
jarsigner -keystore testKeys jogl-all.jar ww

jarsigner -keystore testKeys gluegen-rt-natives-windows-i586.jar ww
jarsigner -keystore testKeys jogl-all-natives-windows-i586.jar ww

jarsigner -keystore testKeys gluegen-rt-natives-windows-amd64.jar ww
jarsigner -keystore testKeys jogl-all-natives-windows-amd64.jar ww

jarsigner -keystore testKeys gluegen-rt-natives-linux-i586.jar ww
jarsigner -keystore testKeys jogl-all-natives-linux-i586.jar ww

jarsigner -keystore testKeys gluegen-rt-natives-linux-amd64.jar ww
jarsigner -keystore testKeys jogl-all-natives-linux-amd64.jar ww

jarsigner -keystore testKeys gluegen-rt-natives-macosx-universal.jar ww
jarsigner -keystore testKeys jogl-all-natives-macosx-universal.jar ww

Unfortunately, if you sign this way you'll have to type your password every time. Alternately, you can use the Ant <signjar> task to sign these from an Ant file that contains your password.

Running the JWS application locally

To run the JWS app locally without a web server, cd to the application directory and type

javaws OneTriangleWebStart.jnlp

This runs the app with the JWS launcher. The result should look just like when we ran the app without the launcher, but there'll be a dialog box that pops up first asking the user's permission to load the app. If you have any JAR signing problems, they should show up at this point.

Clearing the JWS cache

JWS caches JAR and JNLP files to reduce load time. Unfortunately, if you edit one of these files and re-launch, sometimes you won't see your changes because JWS is still using the cached copy. To clear the JWS cache, type

javaws -Xclearcache

Running the JWS application from a web server

*This section is unfinished*

Once you're sure that the JWS application launches correctly from the local file system, you're ready to deploy it to a web server.

  • Create or choose an application directory on your web server.
  • Change the codebase entries in the JNLP from directory names to URLs that refer to the application directory on your web server.
  • Copy all the JAR and JNLP files from your local application directory to the application directory on your web server.
  • Put a link to the OneTriangleWebStart.jnlp file on a page on your web server (more details here).