Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Running a JApplet in a browser  (Read 1322 times)
0 Members and 1 Guest are viewing this topic.
Offline Wolfner

Senior Newbie





« Posted 2011-06-19 15:04:29 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
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):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
<!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)

I already checked some tutorials but it didn't help  Clueless

[IMG]http://www7.pic-upload.de/19.06.11/f6qp8oiqop8s.gif
Offline aazimon
« Reply #1 - Posted 2011-06-19 15:45:48 »

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".
Offline Wolfner

Senior Newbie





« Reply #2 - Posted 2011-06-19 16:30:15 »

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

The html file looks like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
<!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)

Well, obviously we have some access permission problem here persecutioncomplex
Note: I still have that html file on my harddrive. Nothing online.

[IMG]http://www7.pic-upload.de/19.06.11/f6qp8oiqop8s.gif
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Z-Man
« Reply #3 - Posted 2011-06-19 19:29:11 »

Quote
java.security.AccessControlException: access denied (java.io.FilePermission images\grasland_bkg.jpg read)
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
Offline Wolfner

Senior Newbie





« Reply #4 - Posted 2011-06-19 21:36:26 »

Unfortunately that didn't help me Cry
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.

[IMG]http://www7.pic-upload.de/19.06.11/f6qp8oiqop8s.gif
Offline Z-Man
« Reply #5 - Posted 2011-06-19 21:45:54 »

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.
Offline cylab

JGO Knight


Medals: 34



« Reply #6 - Posted 2011-06-19 22:54:41 »

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:

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


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


Mathias - I Know What [you] Did Last Summer!
Offline Wolfner

Senior Newbie





« Reply #7 - Posted 2011-06-19 23:18:08 »

My html file looks like this at the moment:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
<!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  Undecided

[IMG]http://www7.pic-upload.de/19.06.11/f6qp8oiqop8s.gif
Offline zoto

Senior Member


Medals: 4



« Reply #8 - Posted 2011-06-19 23:50:40 »

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.
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #9 - Posted 2011-06-19 23:55:36 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Knight


Medals: 34



« Reply #10 - Posted 2011-06-20 09:41:28 »

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

Mathias - I Know What [you] Did Last Summer!
Offline philfrei
« Reply #11 - Posted 2011-06-20 10:43:21 »

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.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (63 views)
2014-04-15 18:08:23

BurntPizza (61 views)
2014-04-15 03:46:01

UprightPath (74 views)
2014-04-14 17:39:50

UprightPath (57 views)
2014-04-14 17:35:47

Porlus (74 views)
2014-04-14 15:48:38

tom_mai78101 (100 views)
2014-04-10 04:04:31

BurntPizza (160 views)
2014-04-08 23:06:04

tom_mai78101 (255 views)
2014-04-05 13:34:39

trollwarrior1 (208 views)
2014-04-04 12:06:45

CJLetsGame (215 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!