Making an applet run? (solved)

Mkay,

So I’ve been trying to port the Tetris game I made for class into an Applet, so that I could show it to people without them having to download and run the Jar file. But no matter how I try and pack the Jar file and run the applet I keep getting “java.lang.ClassNotFoundException: Game.class” as an error.

The html code I use to try and get it running is:


<applet code="Game.class"
        archive="Tetris.jar"
        width="120" height="120">
</applet>

(Yeah, very small dimension, but I’ll change that once it’s actually fricking running :/)

And the Manifest.txt for the Jar file is:


Main-Class: Game.class

I’ve also tried naming it just “Game” in both the manifest and the html. Just changes the error to be without .class in the end.

The Game.java file contains:


import javax.swing.JApplet;

import Model.Tetris;
import Model.Tetris6_2;

public class Game extends JApplet {

	public void init() {		
		Tetris ts = new Tetris6_2();
		ts.startGame(10, 20);
	}
}

The Jar file runs perfectly when the Game.java file is changed to:


import Model.Tetris;
import Model.Tetris6_2;

public class Game {

	public static void main(String[] args) {		
		Tetris ts = new Tetris6_2();
		ts.startGame(10, 20);
	}
}

Any help with making this stupid Tetris game run as an Applet would be greatly appreciated! :slight_smile:

You put the html and jar at same folder then execute the html right? IMO manifest doesn’t named like that (Manifest.txt). Refer to java doc again.

Yes, I put it in the same folder. And according to the java docs, that’s exactly how to create a manifest file. Like I said, if I change it so that it’s a standalone app, then the jar file works.

Applets are deployed radically different from applications. There is no ‘main class’ with a main-method. You have to extend the Applet class:

http://docs.oracle.com/javase/tutorial/deployment/applet/getStarted.html

Which I am doing. :slight_smile:

:-X Oops.

Anyway, this:


<applet code="Game.class"
        archive="Tetris.jar"
        width="120" height="120">
</applet>

makes the classloader search for a class called “Game.class”, which obviously is a filename, not a classname.


<applet code="my.package.MyClass" <-- note there is no ".class"
        archive="Tetris.jar"
        width="120" height="120">
    <PARAM name="codebase_lookup" value="false"> <-- also prevent the classloader from fetching "./my/package/MyClass.class" over HTTP
</applet>

That would do the job. It’s how the applets in JGO are embedded. If that doesn’t work, maybe “Tetris.jar” is not in the directory you think it is, or it is actually named “tetris.jar” (case sensitive). It might also mean the package is mandatory, you currently did not put the class in a package.

I’m still not having any luck, no matter what I do. When I run the game from Eclipse it’s running the applet without any problems. I even tried using Eclipse to port it as a jar file for me, still no luck. Moved Game.java from the “root” to a package, and still no luck. :confused:

The structure of my jar file is:


Controller/
META-INF/
Model/
View/
Game.class

and it’s in the same directory as my html page. :confused:

why not put it online and post the link? then we can make informed suggestions.

Alright, it can be found here: http://infloop.org/tmp/tetris/ (It’s still not working, and I really haven’t got a clue as to why it’s not working. :confused: )

It was far easier to get my minesweeper to work! (Made in html/javascript :stuck_out_tongue: http://infloop.org/tmp/minesweeper/ for those who wants to see it xD)

Class is loaded just fine.

The console says this:

java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.0)
	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.checkExit(Unknown Source)
	at javax.swing.JFrame.setDefaultCloseOperation(Unknown Source)
	at View.GameWindow.<init>(GameWindow.java:23)
	at Model.Tetris6_2.startGame(Tetris6_2.java:43)
	at Game.init(Game.java:12)
	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Exception: java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.0)

So, do not use:
javax.swing.JFrame.setDefaultCloseOperation(EXIT_ON_CLOSE)
as that is not allowed in the security sandbox.

How come I never saw that error? :confused: I blame Chrome.

Now I just need it to not open up in a weird new window when it starts. Probably need to change it from JFrame?

I use Chrome.

Well, that’s the whole point in coding your game in an Applet, as opposed to using an Applet to kickstart a JFrame. Look at the ‘getting started’ link I posted earlier for a proper tutorial.

Hrm, I didn’t see it in the console. Oh well. :slight_smile:

What should I use instead of a JFrame? Just a JPanel? (I’ll try it out with that now)

Hrm, it seems that I can’t use getBufferStrategy() with a Canvas? Guess I’ll have to go through the guide more throughly(sp?). But thanks a bunch so far! :smiley:

http://docs.oracle.com/javase/6/docs/api/java/awt/Canvas.html#getBufferStrategy()

Just add the canvas to the applet.

That’s what I did. I got this error:

java.lang.IllegalStateException: Component must have a valid peer

You can only call that method after you have added the Canvas in the init() method :wink:

oooh :stuck_out_tongue: Just rearranged 2 lines and now it works! Awesome! Just need to set the size of the window now xD

Thanks a bunch :slight_smile:

Please consider that Applet.init(), Applet.start(), Applet.stop() and Applet.destroy() are not executed on the EventDispatchThread.

Adding components (or making any other changes to UI components) is therefore highly discouraged as issues can be hard to reproduce and/or fix.