Results 1 to 8 of 8
  1. #1
    stainluss is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Simple rectangle display applet doesn't work in browser

    I got the code from a beginner's book called Big Java..

    Here's the error I get from Firefox:
    Java Code:
    Java Plug-in 1.6.0_22
    Using JRE version 1.6.0_22-b04 Java HotSpot(TM) Client VM
    
    
    java.lang.ClassFormatError: Incompatible magic value 1768779887 in class file RectangleApplet
    	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.ClassFormatError: Incompatible magic value 1768779887 in class file RectangleApplet
    Here's the code:

    Java Code:
    import javax.swing.JApplet;
    import java.awt.Rectangle;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    
    /* an applet that draws 2 rectangles*/
    public class RectangleApplet extends JApplet{
    	public void paint(Graphics g){
    		/* Prepare extended graphics */
    		Graphics2D g2 = (Graphics2D) g;
    		
    		/*Construct a rectangle and draw it*/
    		Rectangle box = new Rectangle(15,15,60,60);
    		g2.draw(box);
    		
    		/*Move rectangle 15 across and 20 down */
    		box.translate(15, 20);
    		
    		/*Draw moved rectangle */
    		g2.draw(box);
    		
    		
    		
    	}
    
    }
    And the HTML that I used:
    XML Code:
    <html>
    <head>Rectangles</head>
    <body>
    <applet code="RectangleApplet.class" width="300" height="400">
    </applet></body>
    </html>
    Im using Eclipse, any help would be appreciated

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

    Default Re: Simple rectangle display applet doesn't work in browser

    Incompatible magic value 1768779887 in class file RectangleApplet
    It looks like your .class file is corrupted.
    Create a new one and try again

    That number converts to the letters: impo
    Is your .class file a renamed .java file?
    Last edited by Norm; 01-31-2012 at 09:36 PM.

  3. #3
    stainluss is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: Simple rectangle display applet doesn't work in browser

    Quote Originally Posted by Norm View Post
    It looks like your .class file is corrupted.
    Create a new one and try again

    That number converts to the letters: impo
    Is your .class file a renamed .java file?
    Thanks for the response.

    I think I may know why. In Eclipse, I started a 'new class' file to make this, but when I ran it it was auto-saved as a '.java' file it didnt work in the html as this (should it have?), so then I manually changed the end of it to '.class'.

    I'll save it myself as .class this time and give it a go.

    Quote Originally Posted by Norm View Post
    That number converts to the letters: impo
    Is your .class file a renamed .java file?
    Just saw your edit there, yes it is! I saw that number translated on a different site as that, I was thinking it was the 'import' part? But thats there in a class file too? Sorry if I seem a bit knucleheaded about this lol
    Last edited by stainluss; 01-31-2012 at 10:01 PM.

  4. #4
    stainluss is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: Simple rectangle display applet doesn't work in browser

    Quote Originally Posted by Norm View Post
    It looks like your .class file is corrupted.
    Create a new one and try again

    That number converts to the letters: impo
    Is your .class file a renamed .java file?
    Tried it again. Every time I try to create a new class in eclipse, it saves it as a .java file and then I guess when I change it to .class it becomes corrupt..
    Anybody know how to overcome this?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Simple rectangle display applet doesn't work in browser

    You need to ask someone about how to use the IDE. There is a part of this forum site for IDEs.

    Every time I try to create a new class in eclipse, it saves it as a .java file
    Have you tried compiling the .java source file? The output from a successful compile is a .class file.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple rectangle display applet doesn't work in browser

    The class files can be found in the bin directory, not the src directory.

  7. #7
    stainluss is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: Simple rectangle display applet doesn't work in browser

    Quote Originally Posted by Fubarable View Post
    The class files can be found in the bin directory, not the src directory.
    Yessssssssssss :D

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple rectangle display applet doesn't work in browser

    But regardless, you should learn how to "Export" your project and create a jar file. The Eclipse help files can help with this.

Similar Threads

  1. Why won't this rectangle display in my JFrame?
    By wikemol in forum New To Java
    Replies: 1
    Last Post: 01-07-2012, 07:14 AM
  2. Trying to create simple Timer, doesn't work
    By Ragoune in forum New To Java
    Replies: 4
    Last Post: 07-06-2011, 09:24 PM
  3. Replies: 4
    Last Post: 08-23-2010, 03:19 AM
  4. Replies: 1
    Last Post: 05-31-2010, 08:46 AM
  5. why doesn't this short applet work?
    By kim85 in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 08:43 PM

Posting Permissions

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