Results 1 to 3 of 3
  1. #1
    projecttoday is offline Member
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default Very newbie trying to run form program

    I am just starting to learn Java. I want to run the program listed below which is supposed to display a form (or a frame). It comipiles, I think, (javac TopLevelDemo.java) but when I try to run it with java TopLevelDemo I get a bunch of messages.

    Exception in thread "main" java.lang.NoClassDefFoundError: TopLevelDemo <wrong name: components/TopLevelDemo>
    at Java.Lang.ClassLoader

    and the messages continue. Is this the correct way to run a program that makes a form? What am I doing wrong.


    This is the code. I have omitted some comments.
    Java Code:
    package components;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /* TopLevelDemo.java requires no other files. */
    public class TopLevelDemo {
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("TopLevelDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create the menu bar.  Make it have a green background.
            JMenuBar greenMenuBar = new JMenuBar();
            greenMenuBar.setOpaque(true);
            greenMenuBar.setBackground(new Color(154, 165, 127));
            greenMenuBar.setPreferredSize(new Dimension(200, 20));
    
            //Create a yellow label to put in the content pane.
            JLabel yellowLabel = new JLabel();
            yellowLabel.setOpaque(true);
            yellowLabel.setBackground(new Color(248, 213, 131));
            yellowLabel.setPreferredSize(new Dimension(200, 180));
    
            //Set the menu bar and add the label to the content pane.
            frame.setJMenuBar(greenMenuBar);
            frame.getContentPane().add(yellowLabel, BorderLayout.CENTER);
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    I just tried to run your code from eclipse and it worked. Some new frame came out.

    Even when I tried to run this from some local directory, through command prompt, it worked again. So seems like there is something wrong with the way you are running it.

    Compiler is saying that it can't find the class file of the source code you are trying to run. Why don't you use the -cp or -classpath in your command line argument for more clarity.

    Here are the links for how to use command line options for java and javac. Go through them once and you will know how to do this stuff.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by projecttoday View Post
    I am just starting to learn Java. I want to run the program listed below which is supposed to display a form (or a frame). It comipiles, I think, (javac TopLevelDemo.java) but when I try to run it with java TopLevelDemo I get a bunch of messages.

    Exception in thread "main" java.lang.NoClassDefFoundError: TopLevelDemo <wrong name: components/TopLevelDemo>
    at Java.Lang.ClassLoader

    and the messages continue. Is this the correct way to run a program that makes a form? What am I doing wrong.
    You have put your class "TopLevelDemo" in a package "components" so its complete name is "components.TopLevelDemo" and you should run it as such:

    Java Code:
    java components.TopLevelDemo
    But there is more to it: the structure on the file system should be correct: i.e. in a directory "components" should be your compiled file "TopLevelDemo.class". Your classpath should also be set correctly. Read some tutorials on packages before you go on.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 03-22-2010, 09:03 AM
  2. Replies: 0
    Last Post: 10-31-2009, 09:24 AM
  3. Replies: 10
    Last Post: 08-12-2009, 04:02 PM
  4. Help with my first program...newbie
    By giganews35 in forum New To Java
    Replies: 6
    Last Post: 12-01-2008, 07:16 PM
  5. Need Help With VERY Simple Program -- Newbie!!!
    By somethingfast in forum New To Java
    Replies: 17
    Last Post: 11-25-2008, 07:38 AM

Posting Permissions

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