Regenuluz
|
 |
«
Posted
2012-01-14 13:59:50 » |
|
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: 1 2 3 4
| <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: 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: 1 2 3 4 5 6 7 8 9 10 11 12
| 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: 1 2 3 4 5 6 7 8 9 10
| 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! 
|
|
|
|
|
ReBirth
|
 |
«
Reply #1 - Posted
2012-01-14 14:14:51 » |
|
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.
|
|
|
|
Regenuluz
|
 |
«
Reply #2 - Posted
2012-01-14 15:19:45 » |
|
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.
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
|
|
Regenuluz
|
 |
«
Reply #4 - Posted
2012-01-14 15:42:12 » |
|
Which I am doing. 
|
|
|
|
|
Riven
|
 |
«
Reply #5 - Posted
2012-01-14 16:14:08 » |
|
Which I am doing.   Oops. Anyway, this: 1 2 3 4
| <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. 1 2 3 4 5
| <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.
|
|
|
|
Regenuluz
|
 |
«
Reply #6 - Posted
2012-01-15 15:16:59 » |
|
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. :/ The structure of my jar file is: 1 2 3 4 5
| Controller/ META-INF/ Model/ View/ Game.class |
and it's in the same directory as my html page. :/
|
|
|
|
|
Riven
|
 |
«
Reply #7 - Posted
2012-01-15 15:18:33 » |
|
why not put it online and post the link? then we can make informed suggestions.
|
|
|
|
|
|
Riven
|
 |
«
Reply #9 - Posted
2012-01-16 09:28:42 » |
|
Class is loaded just fine. The console says this: 1 2 3 4 5 6 7 8 9 10 11 12
| 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.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Regenuluz
|
 |
«
Reply #10 - Posted
2012-01-16 09:38:39 » |
|
How come I never saw that error? :/ 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?
|
|
|
|
|
Riven
|
 |
«
Reply #11 - Posted
2012-01-16 09:46:48 » |
|
How come I never saw that error? :/ I blame Chrome.
I use 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?
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.
|
|
|
|
Regenuluz
|
 |
«
Reply #12 - Posted
2012-01-16 09:48:41 » |
|
Hrm, I didn't see it in the console. Oh well.  What should I use instead of a JFrame? Just a JPanel? (I'll try it out with that now)
|
|
|
|
|
Riven
|
 |
«
Reply #13 - Posted
2012-01-16 09:49:33 » |
|
Look at the 'getting started' link I posted earlier for a proper tutorial.
|
|
|
|
Regenuluz
|
 |
«
Reply #14 - Posted
2012-01-16 09:58:51 » |
|
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! 
|
|
|
|
|
Riven
|
 |
«
Reply #15 - Posted
2012-01-16 10:01:34 » |
|
|
|
|
|
Regenuluz
|
 |
«
Reply #16 - Posted
2012-01-16 10:08:09 » |
|
That's what I did. I got this error: 1
| java.lang.IllegalStateException: Component must have a valid peer |
|
|
|
|
|
ra4king
|
 |
«
Reply #17 - Posted
2012-01-16 10:23:39 » |
|
You can only call that method after you have added the Canvas in the init() method 
|
|
|
|
Regenuluz
|
 |
«
Reply #18 - Posted
2012-01-16 10:26:52 » |
|
oooh  Just rearranged 2 lines and now it works! Awesome! Just need to set the size of the window now xD Thanks a bunch 
|
|
|
|
|
Riven
|
 |
«
Reply #19 - Posted
2012-01-16 11:10:58 » |
|
You can only call that method after you have added the Canvas in the init() method  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.
|
|
|
|
|
|
ra4king
|
 |
«
Reply #21 - Posted
2012-01-16 11:23:50 » |
|
Works for me. @Riven, it shouldn't be much of a problem when "init"-ing the UI however. Modifying the UI in any other method Applet life-cycle method is discouraged yes 
|
|
|
|
|