Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Making an applet run? (solved)  (Read 726 times)
0 Members and 1 Guest are viewing this topic.
Offline Regenuluz

Jr. Member
**

Posts: 73
Medals: 1



« on: 2012-01-14 07: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:
1  
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:
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! Smiley
Offline ReBirth

JGO Wizard
****

Posts: 1275
Medals: 19



« Reply #1 on: 2012-01-14 08: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.

Offline Regenuluz

Jr. Member
**

Posts: 73
Medals: 1



« Reply #2 on: 2012-01-14 09: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! Go get 'em!
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #3 on: 2012-01-14 09:22:12 »

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

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Regenuluz

Jr. Member
**

Posts: 73
Medals: 1



« Reply #4 on: 2012-01-14 09:42:12 »

Which I am doing. Smiley
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #5 on: 2012-01-14 10:14:08 »

Which I am doing. Smiley
Lips Sealed 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.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Regenuluz

Jr. Member
**

Posts: 73
Medals: 1



« Reply #6 on: 2012-01-15 09: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. :/
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #7 on: 2012-01-15 09:18:33 »

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

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Regenuluz

Jr. Member
**

Posts: 73
Medals: 1



« Reply #8 on: 2012-01-16 03:15:02 »

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. :/ )

It was far easier to get my minesweeper to work! (Made in html/javascript Tongue http://infloop.org/tmp/minesweeper/ for those who wants to see it xD)
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #9 on: 2012-01-16 03: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.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Games published by our own members! Go get 'em!
Offline Regenuluz

Jr. Member
**

Posts: 73
Medals: 1



« Reply #10 on: 2012-01-16 03: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?
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #11 on: 2012-01-16 03: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.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Regenuluz

Jr. Member
**

Posts: 73
Medals: 1



« Reply #12 on: 2012-01-16 03:48:41 »

Hrm, I didn't see it in the console. Oh well. Smiley

What should I use instead of a JFrame? Just a JPanel? (I'll try it out with that now)
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #13 on: 2012-01-16 03:49:33 »

Look at the 'getting started' link I posted earlier for a proper tutorial.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Regenuluz

Jr. Member
**

Posts: 73
Medals: 1



« Reply #14 on: 2012-01-16 03: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! Cheesy
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #15 on: 2012-01-16 04:01:34 »

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

Just add the canvas to the applet.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Regenuluz

Jr. Member
**

Posts: 73
Medals: 1



« Reply #16 on: 2012-01-16 04:08:09 »

That's what I did. I got this error:
1  
java.lang.IllegalStateException: Component must have a valid peer
Offline ra4king

JGO Kernel
*****

Posts: 3158
Medals: 196


I'm the King!


« Reply #17 on: 2012-01-16 04:23:39 »

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

Offline Regenuluz

Jr. Member
**

Posts: 73
Medals: 1



« Reply #18 on: 2012-01-16 04:26:52 »

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

Thanks a bunch Smiley
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #19 on: 2012-01-16 05:10:58 »

You can only call that method after you have added the Canvas in the init() method Wink
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.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Regenuluz

Jr. Member
**

Posts: 73
Medals: 1



« Reply #20 on: 2012-01-16 05:21:10 »

It's now working! Yay! Playable version right here: http://infloop.org/tmp/tetris/ Smiley

Thanks for the help guys!
Offline ra4king

JGO Kernel
*****

Posts: 3158
Medals: 196


I'm the King!


« Reply #21 on: 2012-01-16 05: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 Smiley

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.161 seconds with 20 queries.