Archive

Posts Tagged ‘jarsigner’

Creating a Java Web Applet

April 20th, 2009 admin No comments

Today’s topic of discussion is Java. Ah yes, the programming language that sounds so familiar (and brings up feelings of dread) among high school programming students. There is no doubt that the language is very delicate, and in many ways very similar (yet different) than C++. But the steps to creating a program and the steps to creating a web are sometimes different.

Now, writing a Java applet is more or less the same as any other Java program; I will include examples later. But how should one go about packaging the applet? Packaging: the key word – no? In an applet, each class must be part of the package. We will go into depth on this later, but it’s important to keep in mind. Also, sometimes users just use the .class file, but this is not always the best for all programs. If you have many classes, you may wish to create a .jar archive which can load all of your files. So in this tutorial, we’ll explain all that as well as signing your jar file.

So let’s begin with a simple example. In fact, the most simple and infamous example known to man in all languages: the “Hello World.”

HelloWorld.java

/**
 * HelloWorld.java by Dennis M.
 *
 * Simple tutorial! :)
 *
 */

// Import all necessary files
import java.applet.*;
import java.awt.*;

// Start with our class
public class HelloWorld extends Applet {
  public void init(){
    // Just begins the program
  }
  public void stop(){
    // Stop the program when page is left or browser
    // closed
  }
  // Allowed to do this because of AWT library
  public void paint(Graphics g){
    g.drawString("Hello World!",20,20);
    g.drawString("Your name is: "+getParameter("YourName"),20,40);
  }
}

That code using this applet code:

<applet code="HelloWorld.class">
<param name="YourName" value="John Doe" />
You must have java enabled to see this text!
</applet>

Now notice, I’ve left the code in just “class” form. Since we only have one class file, there is no real reason to compile file into a jar. This next example will be slightly more complex. We will have a few class files so we’ll archive it and sign the jar. Although signing the jar may not be absolutely necessary in this case (because we’re not requesting access to the user’s computer), it may be a necessary skill you’ll need later on. For instance, I just finished a project working on a simple Java Uploader script; I needed to archive it and sign it. If it was not signed, it could not access the user’s files to upload!

test/testmain.java

/**
 * Simple Test Java Applet by Dennis M.
 *
 */
// Package should go according to structure!
package test;

// Imports
import java.applet.*;
import java.awt.*;
import test.dir1.test1; // We can import this because of "package"
			// and our structure!

public class testmain extends Applet{
  // Declare vars
  test1 test = new test1();

  // Start and stop functions.
  public void init(){
    System.out.println("init();");
  }
  public void stop(){
    System.out.println("stop();");
  }

  // Draw some text again
  public void paint(Graphics g){
    // This function included in dir1.test1
    if(test.randomTest() != 1){
      g.drawString("Did not load correctly!",20,20);
      System.out.println("Doh! bad load :( ");
    } else {
      // Let's continue to give the params and values! :)
      g.drawString("Name:  "+getParameter("name"),20,20);
      g.drawString("Email: "+getParameter("email"),20,40);
      g.drawString("Sex:   "+getParameter("sex"),20,60);
      System.out.println("Success! :D ");
    }
  }
}

test/dir1/test1.java

/**
 * Simple Java Applet by Dennis M.
 *
 */
// Remember.. STRUCTURE STRUCTURE STRUCTURE!
package test.dir1;

public class test1{
  public int randomTest(){
    System.out.println("Successfully called to external file!");
    return 1;
  }
}

ArchivedApp.html

<applet code="test.testmain" archive="bin/archivedapp.jar">
You must have a java-enabled browser to view this!
</applet>

Notice: In this particular applet, since we are loading from an archive, the code base is the name of the first file. The code should always go: package.Mainfile_name. Now if you’re main file happens to be like “package test1.test2,” then you simply adjust the code line to “test1.test2.Mainfile_name”

To compile the code use the following command (in the src/test directory)

javac testmain.java dir1/test1.java -d ../../bin

Now that we have it compiled, go to the “bin” directory. In here run the following command to generate a key:

keytool -genkey -keystore keystore -dname "CN=Default,OU=Default, O=Default, L=Default, ST=Illinois, C=US"

Use password: default

Now, to sign the jar file use this:

jarsigner -keystore keystore archivedapp.jar mykey

Enter the password you used earlier for the mykey certificate in the keystore. Your jar file has been successfully signed!

As usual, everything is in the archive below for you to download! All compiled classes/jars and sources are included. Enjoy!
Creating a Java Web Applet Tutorial Files

Regards,
Dennis M.

Categories: Java Tags: , , , , , ,