0

I have three classes. Main, Core, and Start. Here is the code for Main:

package com.leslie.quiz;

    public class Main {
        public static void main(String[] args) {
            com.leslie.quiz.Start.main(null);
        }
    }

Here is the code for Core:

    package com.leslie.quiz;

    public class Core {
        public void coldlunch() {

        }

        public void hotlunch() {

        }
    }

Here's the code for Start:

    package com.leslie.quiz;

    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import java.awt.BorderLayout;
    import javax.swing.JButton;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;

    public class Start {
        com.leslie.quiz.Core core = new Core();
        float opacity = 1;

        private JFrame frmCafeteriaQuiz;

/**
 * Launch the application.
 */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Start window = new Start();
                        window.frmCafeteriaQuiz.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
             });
         }

/**
 * Create the application.
 */
public Start() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frmCafeteriaQuiz = new JFrame();
    frmCafeteriaQuiz.setTitle("Cafeteria Quiz");
    frmCafeteriaQuiz.setResizable(false);
    frmCafeteriaQuiz.setBounds(100, 100, 471, 331);
    frmCafeteriaQuiz.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel lblWelcomeToThe = new JLabel("Welcome to the Cafeteria Quiz! Are you a responsible hawk?");
    frmCafeteriaQuiz.getContentPane().add(lblWelcomeToThe, BorderLayout.NORTH);

    JButton btnIHaveCold = new JButton("I have Cold Lunch");
    btnIHaveCold.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            core.coldlunch();
        }
    });
    frmCafeteriaQuiz.getContentPane().add(btnIHaveCold, BorderLayout.WEST);

    JButton btnIHaveHot = new JButton("I have Hot Lunch");
    btnIHaveHot.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            core.hotlunch();
                }
            });
    frmCafeteriaQuiz.getContentPane().add(btnIHaveHot, BorderLayout.EAST);
            }

    }

I'm running cmd, and changing directory to the package where all my classes are. When I run Main by typing "java Main" I get

java.lang.NoClassDefFoundError Main (Wrong Name : com/leslie/quiz/Main)

One thing I've read is that the problem could be caused by invoking the class from inside the package? It wasn't very detailed and when I ran the program from in the programs main folder, it did the same thing. If I run the program in eclipse it works great and shows no errors. And I am aware that eclipse uses a different compiler. But nothing I've tried works. Any help would be great. Thanks. :)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Stefan Carlson
  • 372
  • 3
  • 7
  • 21
  • this usually happens if you reference an external library but don't add it to the build path – sha256 Sep 05 '13 at 20:58
  • It will be better showing the stacktrace i.e. that message saying *java.lang.ClassNotFoundError: some.package.which.TheClassNotFound*. – Luiggi Mendoza Sep 05 '13 at 21:01
  • 1
    Are you running this from the command line? If so, please provide your command(s) for compiling/running your program. I'm betting that you're either not running from the package root directory or you're not setting the classpath correctly. – Asaph Sep 05 '13 at 21:09
  • It sounds to me more like your class files and/or source files aren't in the proper directory. Is your `src` directory (a) marked as a source directory? Is it (b) the *root* source directory? Are your source files (c) in the proper package/directory hierarchy? – Dave Newton Sep 05 '13 at 21:14
  • 3
    Since your `Main` class is in the `com.leslie.quiz` package, try running the command `java com.leslie.quiz.Main` from whatever directory is the parent of the `com` directory (which must contain the hierarchy of compiled .class files). Note that the default binary output directory in Eclipse is a project's `bin` directory. – superEb Sep 05 '13 at 21:54
  • @superEb Please promote that comment to an answer. – Andrew Thompson Sep 06 '13 at 01:45
  • possible duplicate of [NoClassDefFoundError: wrong name](http://stackoverflow.com/questions/7509295/noclassdeffounderror-wrong-name) – BalusC Sep 06 '13 at 01:51

1 Answers1

3

Since your Main class is in the com.leslie.quiz package, you should cd to the parent directory of the compiled output and run the command:

java com.leslie.quiz.Main

Note that the default binary output directory in Eclipse is a project's bin directory. Although it's hidden from the Package Explorer view in Eclipse, it will still exist on the file system. You should be able to see it from the Navigator view in Eclipse.

The contents of bin will look something like this:

bin/
  com/
    leslie/
      quiz/
        Core.class
        Main.class
        Start.class

In this case, cd to bin and run the java command.

superEb
  • 5,613
  • 35
  • 38