Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Applet ClassFormatError and magic values

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Applet ClassFormatError and magic values

    I am trying to turn a console application into an applet I have code that builds a GUI for a calculator that compiles and runs as expected:

    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import java.lang.*;
     
    public class CalculatorGUI extends JFrame
    {
        JButton button;
        JTextField screen;
        //OperandListener opnLis = new OperandListener(this);
        //OperatorListener optLis = new OperatorListener(opnLis);
     
        CalculatorGUI()
        {
            setSize(150, 150);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setResizable(false);
            setVisible(true);
     
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
     
            screen = new JTextField("0", 15);
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = 4;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("7");
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("8");
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("9");
            gbc.gridx = 0;
            gbc.gridy = 2;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("+");
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.gridwidth = 1;
            //button.addActionListener(optLis);
            add(button);
     
            button = new JButton("4");
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.gridwidth = 1;
            button.addActionListener(opnLis);
            add(button);
     
            //button = new JButton("5");
            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            button.addActionListener(opnLis);
            add(button);
     
            //button = new JButton("6");
            gbc.gridx = 1;
            gbc.gridy = 2;
            gbc.gridwidth = 1;
            button.addActionListener(opnLis);
            add(button);
     
            //button = new JButton("-");
            gbc.gridx = 1;
            gbc.gridy = 3;
            gbc.gridwidth = 1;
            button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("1");
            gbc.gridx = 2;
            gbc.gridy = 0;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("2");
            gbc.gridx = 2;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("3");
            gbc.gridx = 2;
            gbc.gridy = 2;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("*");
            gbc.gridx = 2;
            gbc.gridy = 3;
            gbc.gridwidth = 1;
            //button.addActionListener(optLis);
            add(button);
     
            button = new JButton(".");
            gbc.gridx = 3;
            gbc.gridy = 0;
            gbc.gridwidth = 1;
            button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("0");
            gbc.gridx = 3;
            gbc.gridy = 1;
            gbc.gridwidth = 4;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("=");
            gbc.gridx = 3;
            gbc.gridy = 2;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("/");
            gbc.gridx = 3;
            gbc.gridy = 3;
            gbc.gridwidth = 1;
            //button.addActionListener(optLis);
            add(button);
        }
    }

    The code for the applet is:

    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JApplet;
    import javax.swing.JTextField;
    import java.lang.*;
     
    public class CalculatorApplet extends JApplet
    {
        JButton button;
        JTextField screen;
        //OperandListener opnLis = new OperandListener(this);
        //OperatorListener optLis = new OperatorListener(opnLis);
     
        CalculatorApplet()
        {
            setSize(150, 150);
            //setDefaultCloseOperation(EXIT_ON_CLOSE);
            //setResizable(false);
            setVisible(true);
     
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
     
            screen = new JTextField("0", 15);
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = 4;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("7");
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("8");
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("9");
            gbc.gridx = 0;
            gbc.gridy = 2;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("+");
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.gridwidth = 1;
            //button.addActionListener(optLis);
            add(button);
     
            button = new JButton("4");
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("5");
            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("6");
            gbc.gridx = 1;
            gbc.gridy = 2;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("-");
            gbc.gridx = 1;
            gbc.gridy = 3;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("1");
            gbc.gridx = 2;
            gbc.gridy = 0;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("2");
            gbc.gridx = 2;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("3");
            gbc.gridx = 2;
            gbc.gridy = 2;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("*");
            gbc.gridx = 2;
            gbc.gridy = 3;
            gbc.gridwidth = 1;
            //button.addActionListener(optLis);
            add(button);
     
            button = new JButton(".");
            gbc.gridx = 3;
            gbc.gridy = 0;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("0");
            gbc.gridx = 3;
            gbc.gridy = 1;
            gbc.gridwidth = 4;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("=");
            gbc.gridx = 3;
            gbc.gridy = 2;
            gbc.gridwidth = 1;
            //button.addActionListener(opnLis);
            add(button);
     
            button = new JButton("/");
            gbc.gridx = 3;
            gbc.gridy = 3;
            gbc.gridwidth = 1;
            //button.addActionListener(optLis);
            add(button);
        }
     
        public void init()
        {
            new CalculatorApplet();
        }
    }

    The appletviewer throws the following error:

    java.lang.ClassFormatError: Incompatible magic value 1181314149 in class file Project
    	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 1181314149 in class file Project

    Due to personal technology woes, I am using the compilr.com online compiler and embedded appletviewer, so it may be a problem with how the specific compiler builds the class file. I don't have the option of using the JDK compiler, so I can't figure out I have a compiler-specific problem.


  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    12
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Applet ClassFormatError and magic values

    Are you sure that the class files are correct? The magic value should be CA FE BA BE, but yours is 46 69 6C 65, which translates into the ASCII "File", which leads me to believe something has gone awry.

    If you could attach the Project.class and Project.java files, I'd be more than happy to take a look at them.

Similar Threads

  1. JAVA MAGIC SQUARE
    By hiimjoey11 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: November 28th, 2012, 12:42 AM
  2. Magic Square Checker
    By Hypnos in forum Object Oriented Programming
    Replies: 1
    Last Post: April 3rd, 2011, 07:37 PM
  3. Magic square class (modular math problem)
    By mjpam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 30th, 2010, 11:56 AM
  4. Able to set, but not get values
    By Simple in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 23rd, 2010, 05:01 PM
  5. Magic Squares, input confusion
    By bengiles89 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:40 PM