Page 1 of 6 123 ... LastLast
Results 1 to 20 of 101
  1. #1
    Join Date
    Mar 2012
    Posts
    54
    Rep Power
    0

    Default Java Applet Not Running on html.

    So I was making this Tetris game which works nicely but I'm having some trouble. So I'm using this html code:
    XML Code:
    <HTML>
    <HEADING>
    <TITLE>
    Tetris produced by Superhamster Games
    </TITLE>
    </HEADING>
    <BODY>
    <APPLET CODE = "tetris.Tetris.class" WIDTH = 900 HEIGHT = 
    
    800
    	ALT = "You need Java to play Tetris.">
    </APPLET>
    </BODY>
    </HTML>
    My folders go like this:
    -Whole folder
    -Blocks
    -some highscores.txt
    -Tetris
    -Tetris.class <---- This is the main class
    -other classes.class
    -a few images.jpeg
    -Html link

    Just in case if important I'll give the java class:
    Java Code:
    package Tetris;
    
    import java.awt.*;
    import java.applet.*;
    import java.io.IOException;
    
    public class Tetris extends Applet implements Runnable{
    	private BlockController bc;
    	private Thread game, paint;
    	private KeyHandler kh;
    	private Painter painter;
    	private MainMenu menu;
    	private boolean onMenu;
    	private ScoreKeeper sk;
    	private Timer t;
    	
    	public void init(){
    		t = new Timer();
    		sk = new ScoreKeeper();
    		bc = new BlockController(sk, t);
    		kh = new KeyHandler(bc);
    		Image offScreenImage = createImage(900, 800);
    		menu = new MainMenu(this);
    		onMenu = true;
    		painter = new Painter(bc, offScreenImage,menu, onMenu, sk, t);
    	}
    	
    	public void start(){
    		game = new Thread(this);
    		game.start();
    		paint = new Thread(painter);
    		paint.start();
    	}
    	
    	public void run(){
    		while(true){
    			long time = System.currentTimeMillis();
    			if(!onMenu){
    				bc.update();
    				if(bc.gameOver){
    					painter.drawGameOver();
    					onMenu = true;
    					bc.gameOver = false;
    					t.playing = false;
    					repaint();
    					try{
    						Thread.sleep(5000);
    					}catch(Exception e){}
    					painter.onMenu = true;
    				}
    			}
    			repaint();
    			long newTime = System.currentTimeMillis();
    			try{
    				Thread.sleep(time - newTime + 50);
    			}catch(Exception e){}
    		}
    	}
    	
    	public void update(Graphics g){
    		paint(g);
    	}
    	
    	public void paint(Graphics g){
    		if(painter.painting){
    			while(painter.painting || !painter.painted){
    				try {
    					Thread.sleep(1);
    				} catch (InterruptedException e) {
    				}
    			}
    			painter.capturingImage = true;
    			try{
    				Thread.sleep(10);
    			}catch(Exception e){}
    		}
    		g.drawImage(painter.offScreenImage, 0,0,null);
    		painter.capturingImage = false;
    		painter.painted = false;
    	}
    	
    	public void stop(){
    		bc.stop();
    	}
    	
    	public boolean keyUp(Event evt, int key){
    		if(!onMenu)
    			kh.keyUp(key);
    		return true;
    	}
    	
    	public boolean keyDown(Event evt, int key){
    		if(!onMenu)
    			kh.keyDown(key);
    		return true;
    	}
    	
    	public Image getImage(String imgName, MediaTracker m){
    		Image image = getImage(getCodeBase(), "Tetris/" + imgName + ".jpg");
    		m.addImage(image, 0);
    		return image;
    	}
    	
    	public boolean mouseDown(Event evt, int x, int y){
    		if(onMenu){
    			onMenu = !menu.checkClick(x, y, bc);
    			if(!onMenu){
    				painter.onMenu = onMenu;
    				if(menu.usingTimer()){
    					t.setTimer();
    					bc.updateHScores(menu.using5Block());
    					try{
    						t.t.start();
    					}catch(Exception e){}
    				}else{
    					t.stop();
    					bc.updateHScores(menu.using5Block());
    				}
    			}
    		}
    		return true;
    	}
    }
    And here is the exception:

    Java Code:
    java.lang.NoClassDefFoundError: tetris/Tetris (wrong name: Tetris/Tetris)
    	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)
    Exception: java.lang.NoClassDefFoundError: tetris/Tetris (wrong name: Tetris/Tetris)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Java Applet Not Running on html.

    Java is case sensitive. You have spelled the packagename/classname with different cases.

  3. #3
    Join Date
    Mar 2012
    Posts
    54
    Rep Power
    0

    Default Re: Java Applet Not Running on html.

    I thought that too but for some reason it's not working. Later today I could copy the whole folder into a zip and let you look at it if that would help. I would probably put it on mediafire if that works for you.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Java Applet Not Running on html.

    it's not working
    Please explain and show the error message, code and html.

  5. #5
    Join Date
    Mar 2012
    Posts
    54
    Rep Power
    0

    Default Re: Java Applet Not Running on html.

    So basicly, I have checked over the names of folders and classes at least 100 times and tested it with different cases(I don't know if it's the right word but I mean a & A) and it still doesn't work. I already have showed the code, html and error message up at the top. Link to download below:
    Blocks.zip
    and the file is called "blocks.zip".

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Java Applet Not Running on html.

    it still doesn't work.
    If you have changed the case of the package/classname the error message will be different.
    This message shows that they have different case:
    java.lang.NoClassDefFoundError: tetris/Tetris (wrong name: Tetris/Tetris)

    Please post the new error message.

    Is the class file in a folder with the same name as the package name?
    Is the HTML file in the same folder as the one that contains the folder(tetris or Tetris) that contains the class
    Last edited by Norm; 03-16-2012 at 12:03 AM.

  7. #7
    Join Date
    Mar 2012
    Posts
    54
    Rep Power
    0

    Default Re: Java Applet Not Running on html.

    Ok. So here's applet code = tetris.Tetris.class:
    Java Code:
    java.lang.NoClassDefFoundError: tetris/Tetris (wrong name: Tetris/Tetris)
    	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)
    Exception: java.lang.NoClassDefFoundError: tetris/Tetris (wrong name: Tetris/Tetris)
    Here's applet code = tetris.tetris.class:
    Java Code:
    java.lang.NoClassDefFoundError: tetris/tetris (wrong name: Tetris/Tetris)
    	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)
    Exception: java.lang.NoClassDefFoundError: tetris/tetris (wrong name: Tetris/Tetris)
    Here's applet code = Tetris.tetris.class:
    Java Code:
    java.lang.NoClassDefFoundError: Tetris/tetris (wrong name: Tetris/Tetris)
    	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)
    Exception: java.lang.NoClassDefFoundError: Tetris/tetris (wrong name: Tetris/Tetris)
    And here Tetris.Tetris.class doesn't show me anything anymore. So I guess this is the correct one but it should pop up with any image at least so I don't know.

    Also, the mediafire link is a bit messed up and is supposed to have the folder "Tetris" filled with all the classes and pictures but the text files stay where they are. Also, thanks for helping, I was expecting so many and so fast replies like this so I'm definetly staying with this forum. Others take day or weeks if at all, and don't help at all so thanks.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Java Applet Not Running on html.

    The posted test show: tetris.Tetris.class, tetris.tetris.class and Tetris.tetris.class

    Did you also try: code = Tetris.Tetris.class:
    And here Tetris.Tetris.class doesn't show me anything anymore.
    What happened when you tried that???

  9. #9
    Join Date
    Mar 2012
    Posts
    54
    Rep Power
    0

    Default Re: Java Applet Not Running on html.

    Quote Originally Posted by Norm View Post
    The posted test show: tetris.Tetris.class, tetris.tetris.class and Tetris.tetris.class

    Did you also try: code = Tetris.Tetris.class:

    What happened when you tried that???
    I'm not exactly sure but it didn't give me an exception but it didn't show me anything on the screen either stangely. And when I made it full-screen mode it turned invisible and I could see windows behind it. So I'm guessing this is a somewhat good sign but it runs fine on eclipse so I figured it wasn't that the code was bad.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Java Applet Not Running on html.

    try adding some println statements to show the execution flow to see where the code is executing.
    Look in the browser's java console for the printed output.

  11. #11
    Join Date
    Mar 2012
    Posts
    54
    Rep Power
    0

    Default Re: Java Applet Not Running on html.

    I would try doing that but I can't even open up the browser's java console.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Java Applet Not Running on html.

    You'll have to open the java console to get the error messages shown there.

    Check the Java icon in the Control Panel to see it you have the java console enabled.

  13. #13
    Join Date
    Mar 2012
    Posts
    54
    Rep Power
    0

    Default Re: Java Applet Not Running on html.

    Ahh. I figured it out. It has the error message saying:

    java.security.AccessControlException: access denied (java.io.FilePermission .\Blocks\NormalHighScores.txt read)
    at java.security.AccessControlContext.checkPermission (Unknown Source)
    at java.security.AccessController.checkPermission(Unk nown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkRead(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at Tetris.HighScores.<init>(HighScores.java:23)
    at Tetris.BlockController.<init>(BlockController.java :27)
    at Tetris.Tetris.init(Tetris.java:20)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Exception: java.security.AccessControlException: access denied (java.io.FilePermission .\Blocks\NormalHighScores.txt read)
    Exception in thread "Thread-12" java.lang.NullPointerException
    at Tetris.Tetris.run(Tetris.java:39)
    at java.lang.Thread.run(Unknown Source)
    I'll try taking the project all together now and see if that helps in any way.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Java Applet Not Running on html.

    access denied (java.io.FilePermission .\Blocks\NormalHighScores.txt read)
    Your applet is not allowed to read the file in the message.

    Where are the files located relative to where the html file and the applet class files are located?
    Can all the files you need be put into a jar file? The applet would be able to read from the jar file by treating the files as resources.

  15. #15
    Join Date
    Mar 2012
    Posts
    54
    Rep Power
    0

    Default Re: Java Applet Not Running on html.

    I guess you didn't see this at the top but heres the folder layout.
    -Whole folder
    --Blocks
    ---some highscores.txt
    --Tetris
    ---Tetris.class <---- This is the main class
    ---other classes.class
    ---a few images.jpeg
    --Html link

    I tried making a jar before but it didn't work because it said something about not finding the main class but I never made a jar before so I figured just do html like I normally do.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Java Applet Not Running on html.

    Try moving the files and changing the paths until the program can read the files.
    If you put everything in a jar file there won't be any restrictions on your being able to read the files.
    Last edited by Norm; 03-17-2012 at 04:41 PM. Reason: no manifest for applets

  17. #17
    Join Date
    Mar 2012
    Posts
    54
    Rep Power
    0

    Default Re: Java Applet Not Running on html.

    Ok. I'll try changing the paths.

    Yeah but when I tried to make a jar, when it asked for main class it didn't recognize any main classes.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Java Applet Not Running on html.

    For applets you tell the browset the name of the jar file and the name of the class in the HTML <applet tag. You do not execute the jar file like an application. The browser does it.

  19. #19
    Join Date
    Mar 2012
    Posts
    54
    Rep Power
    0

    Default Re: Java Applet Not Running on html.

    Oh cool. Thanks for telling me that. So I just do <APPLET CODE = "Tetris.jar" WIDTH = 900 HEIGHT = 800>? <--- Just an example.

    I was wondering for the line
    Java Code:
    writer = new BufferedWriter(new FileWriter("./Blocks/NormalHighScores.txt"));
    What does the . do in the location for the file? Because maybe thats where a problem is.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Java Applet Not Running on html.

    Your <applet tag syntax is wrong, Find the doc for how to code applet tags or do a search here on the forum for samples.

    maybe thats where a problem is.
    Change it and see.
    You could do a test to see what the program sees by creating an instance of the File class with that path and printing out the absolute path for that File object.

Page 1 of 6 123 ... LastLast

Similar Threads

  1. java applet not running when accessed from web
    By jobud9 in forum New To Java
    Replies: 3
    Last Post: 01-22-2012, 02:03 AM
  2. Replies: 7
    Last Post: 12-18-2011, 04:13 AM
  3. Applet Running in Java not from Cmd
    By vin_eets in forum Java Applets
    Replies: 1
    Last Post: 03-29-2011, 04:08 PM
  4. Running as Java Applet in eclipse
    By atomant in forum New To Java
    Replies: 0
    Last Post: 12-05-2010, 02:22 AM
  5. Java Applet Not Running
    By kahaj in forum Java Applets
    Replies: 4
    Last Post: 10-08-2009, 12:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •