Webstart

From Slick2D Wiki
Revision as of 16:59, 8 August 2013 by Sigtau (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Java Webstart is a very handy way of deploying your game or application over the web. Fortunately, Slick makes it easy to enable your application for use with webstart. For more information see the Webstart Docs, or have a look at Kev's Webstart Walkthrough (archived).

Creating JAR Packages[edit]

The first step is to package up your application into a JAR package. There are a number of ways of doing this depending on your development environment. For more information about JARs see the Java JAR Tutorial.

Creating a JAR Package using the Command Line[edit]

For example using the commandline, the following will create a JAR package containing all the .class files in the current folder (and any subfolders) and all the .bmp files in the subfolder images.

jar cvf MyGame.jar *.class images/*.bmp

Note that you do not have to include a manifest file or specify a start up class if you are going to use the package with webstart. There are many other options available when using the jar command such as being able to update a JAR if you have made changes to some of the files contained in it. To see these options just type jar with no arguments on the command line.

Creating a JAR Package in Eclipse[edit]

In Eclipse the best way to create a JAR from your application is to use the export method. Right click your project and select Export from the menu. You'll then be asked how you want to export the project. Expand the Java node, select JAR file and click Next. Then you'll be able to select the resources you want to include in the jar. Include all the Java classes and packages that your application needs. Then select the other resources in your filespace that your application uses such as images, sounds, particle systems etc. You might find that the filespace shown in Eclipse is not up-to-date in which case you'll need to close the Export dialog and refresh your filespace by right clicking your project and clicking Refresh. Once you have selected the files you want to include in your JAR, select the export destination to store your jar file and then click Next.

In the next screen, make sure you tick “Save the description of this JAR in the workspace”. Type in or click browse to enter a location and file name to store the settings for this export for example “MyProject/BuildJAR”. Then click Next.

In the next screen you can either generate a manifest file automatically, or use an existing manifest file if you have already created one. You can also Select a main class for this JAR which will run when the jar itself is run. This isn't necessary for Webstart though. When you click Finish, the JAR file will be built. If you see warnings then it's likely that Eclipse is informing you that you have some compile warnings in your code. It should be safe to ignore these if you want.

After you have first built your JAR in Eclipse, you will see a .jardesc in your workspace (if you don't then make sure you saved the JAR description in the steps above). If you make changes to your code and you want to recreate your JAR, then simply right click on the .jardesc file and select Create JAR. If you want to include new files or remove old files from the JAR then select Open JAR Packager to change these options.

Creating JARs from ZIPs[edit]

An alternative way to create JARs is to create a zip archive with all the classes and resources your application needs and the simply rename the .zip to .jar. It should work fine with Webstart!

Creating a JNLP[edit]

In Webstart, the JNLP file is the XML file that your users download which describes all the JARs and other libraries and resources that your application needs to run. In a Slick application you only need to reference the JAR of your application and the Slick library.

<?xml version="1.0" encoding="utf-8"?>
<jnlp
  spec="1.0+"
  codebase="http://www.mydomain.com/games"
  href="mygame.jnlp">
  <information>
    <title>My Game</title>
    <vendor>My Vendor Name</vendor>
    <homepage href="http://www.mydomain.com"/>
    <description>My Game</description>
    <description kind="short">My Game</description>
    <icon href="icon.gif"/>
  </information> 
  <resources>
    <j2se href="http://java.sun.com/products/autodl/j2se" version="1.4+" max-heap-size="128m"/>
    <jar href="mygame.jar"/>  
    <extension href="http://slick.cokeandcode.com/demos/slick.jnlp" version="0.4"/>
  </resources>  
  <application-desc main-class="MyMainStartupClass"/>
</jnlp>

Just use the above structure and save the xml file as a .jnlp file. Make sure you change all the details such as the codebase (the location of your jnlp and jar files), the jnlp file name, the game title, vendor and description information. You can also include an icon if you like. This should be 64x64px.

The last few settings are the most important. Firstly the j2se tag points to the download location of the required build of Java needed to run your program. It also specifies the minimum Java version needed and will display an error if the user's version is below that.

The jar tag points to your JAR file. If you have more than one JAR file you can simply add multiple jar tags.

The extension tag is what links your application to Slick. The slick.jnlp file includes the LWJGL libraries, the Slick library itself along with some other supporting files. You can also optionally specify a version attribute to this tag. This will make sure that your application only uses the specified version of Slick. If you want to always use the latest version of Slick, just remove this attribute.

Finally the last option is the main-class attribute. Just set this to the class name (including the package if you're using them) of your main start up class. This is the class that will be run once Webstart has finished everything else.

Uploading and Deploying your Application[edit]

Now that you have your JAR and JNLP files created all that remains to do is to upload them to the web. For this you need a webhost that supports jnlp files. If your host runs on linux you can probably enable support for JNLPs yourself. If your host uses Windows you may have to request JNLP support to be enabled.

If you don't have a host then I recommend using MyJavaServer. This service is currently exclusive to Java developers and restricts access to others by posing a Java related problem which you have to solve to obtain an account.

With your host ready, simply upload your JAR, JNLP and any icon files you're using to some location on your server. Now if you did everything right, you should be able to simply navigate to your jnlp file through your browser and the application should fire up in Webstart. If so, congratulations! You can show share your game with all your friends!