Running a JApplet in a browser

Hi,

I just finished a little game that is executed in an applet and now I want to make it playable in a browser but somehow I can’t get it working.

I wrote the whole project in eclipse. It has one package (battlemode), which consists of 4 classes (Battleview (the main applet class); EnemyManager; Target; Musicloader;). The applet classes signature looks like this:

package battlemode;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JApplet;

import paulscode.sound.SoundSystem;
import paulscode.sound.SoundSystemConfig;
import paulscode.sound.SoundSystemException;
import paulscode.sound.codecs.CodecWav;
import paulscode.sound.libraries.LibraryJavaSound;

public class Battleview extends JApplet implements Runnable{

[...]

}

The games resources are in two seperate folders (images, sounds) which are both in the projects /bin directory.

Now I tried to include the applet into an html file by using the following html code (the html file itself is located in the same folder as the .class-files):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title>Groundhog</title>
</head>

<body>


<p>
<applet code="Battleview.class" height="470" width="800">
</applet>
</p>

</body>
</html>

Unfortunately this doesn’t work. I always get the following error message (Java Console):

[quote]java.lang.NoClassDefFoundError: Battleview (wrong name: battlemode/Battleview)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Ausnahme: java.lang.NoClassDefFoundError: Battleview (wrong name: battlemode/Battleview)
[/quote]
I already checked some tutorials but it didn’t help :clue:

It is ideal to put the classes in a jar. If you have it in a jar, with the correct package structure, you can call just the jar name from your applet tag.
As you have it now.
Is you class in a folder called “battlemode”? It should be, then you should have your applet tag call “battlemode/Battleview.class”.

I tried the thing with the .jar-file but now the console says:

[quote]Load: Class game.jar not found
java.lang.ClassNotFoundException: game.jar
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.ClassNotFoundException: game.jar
[/quote]
The html file looks like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title>Groundhog</title>
</head>

<body>


<p>
<applet code="game.jar" height="470" width="800">
</applet>
</p>

</body>
</html>

The html file is in the same folder as the .jar file.

Also tried to change the path to “battlemode/Battleview.class”. Same error.

Although I haven’t located the html file in the /bin folder yet.
Will try that.

Edit 1:

Alright now I don’t get the error message anymore (html files path is “battlemode/Battleview.class” - the file itself lies in the /bin folder). But: Now there is simply nothing on the screen/in the applet.

Just wonderful empty whiteness. (looks like this)

Edit 2:

Got that out of the Java Console

[quote]java.security.AccessControlException: access denied (java.io.FilePermission images\grasland_bkg.jpg read)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkRead(Unknown Source)
at java.io.File.canRead(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at battlemode.Battleview.loadPictures(Battleview.java:117)
at battlemode.Battleview.init(Battleview.java:92)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Ausnahme: java.security.AccessControlException: access denied (java.io.FilePermission images\grasland_bkg.jpg read)
[/quote]
Well, obviously we have some access permission problem here :persecutioncomplex:
Note: I still have that html file on my harddrive. Nothing online.

[quote]java.security.AccessControlException: access denied (java.io.FilePermission images\grasland_bkg.jpg read)
[/quote]
It looks to me like maybe your browser is denying the applet access to the local filesystem. I don’t know what browser your using or how to allow it the applet to access the files though.

EDIT: I meant “how to allow the applet to access the files though”. The browser already has access. :wink:

EDIT 2: If this is in fact the problem this link might help you solve it. Scroll down to the part with the bold title “Granting Applet Permissions”.
Essentials, Part 1, Lesson 6: File Access and Permissions

Unfortunately that didn’t help me :’(
I still get the access denied error. I hope I didn’t make any mistakes when I made that policy file though.

Also it’s a little bit tricky… in that example the applet is executed from the console with some parameters for the policy. And I have no idea how to start it like this from a browser.

However I found out that the executed JAR file has to be signed if it wants to access files.
At least I hope so, because that signing process is pretty complicated if you do it for the first time.

Ahh well sorry if that didn’t help. I really don’t know much about running applets, I do most (all) of my work using JFrames and a Canvas.

Don’t load from the file system (I assume you use the java File class in there), it won’t work - simply because the needed files aren’t on the users computer…

Package your images and resource in the game-jar and use resource-loading from the classpath instead (getClass().getResourceAsStream("/path/to/resource/in/the/jar").

And for the errors: you have to put the name of the jar in the archive attribute of the applet element and include the package name like this:


<applet archive="game.jar" code="battlemode/Battleview.class"/>

See also http://download.oracle.com/javase/1.4.2/docs/guide/misc/applet.html

My html file looks like this at the moment:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title>Groundhog</title>
</head>

<body bgcolor="#000000">


<p align="center">
<applet code="battlemode.Battleview.class" codebase="./" name="GroundhogHunt" archive="groundhoghunt.jar" height="470" width="800">
</applet>
</p>

</body>
</html>

I still get the accessdenied exception, although I already signed the Jar file.
I also played around with a jnlp file. However I want to execute the game in the browser so that didn’t help either.
Somehow I’m slowly running out of ideas :-\

Did you change the file loading code like cylab suggested?

If you post the relevant code here someone will probably know how to fix it.

How exactly are you loading your images. Could you show us the code you are using to load the images and/or sounds?

Dont use codebase="./" it might have unexpected behaviour on some systems. You shouldnt even need to sign anything if you stick to java2d and loading all your resources from a jar

Hmmm. Is this the only Java Tutorial on loading resources?
http://download.oracle.com/javase/tutorial/deployment/webstart/retrievingResources.html

Need to check out getResource() and getResourceAsStream() for applets. But the explanation in the tutorial is not so great.

http://download.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html
See the method getResource(String). With this command, you can have the images you need as part of the jar file, and you can access them relative to the jar location.