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

允我心安 提交于 2019-12-02 22:13:43

问题


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. :)


回答1:


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.



来源:https://stackoverflow.com/questions/18645811/java-lang-noclassdeffounderror-main-wrong-name-com-leslie-quiz-main

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!