java.lang.NoClassDefFoundError Main(错名称:com / leslie / quiz / Main) [英] java.lang.NoClassDefFoundError Main (Wrong Name : com/leslie/quiz/Main)

查看:119
本文介绍了java.lang.NoClassDefFoundError Main(错名称:com / leslie / quiz / Main)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个班。主,核心和开始。这是Main的代码:

  package com.leslie.quiz; 

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

这是Core的代码:

  package com.leslie.quiz; 

public class Core {
public void coldlunch(){

}

public void hotlunch(){

}
}

以下是开始代码:

  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;

私人JFrame frmCafeteriaQuiz;

/ **
*启动应用程序。
* /
public static void main(String [] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
try {
开始窗口= new Start();
window.frmCafeteriaQuiz.setVisible(true);
} catch(Exception e){
e.printStackTrace();
}
}
});
}

/ **
*创建应用程序。
* /
public Start(){
initialize();
}

/ **
*初始化框架的内容。
* /
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(欢迎来到咖啡馆测验!你是负责任的鹰吗)
frmCafeteriaQuiz.getContentPane()。add(lblWelcomeToThe,BorderLayout.NORTH);

JButton btnIHaveCold =新的JButton(我有冷午餐);
btnIHaveCold.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent arg0){
core.coldlunch();
}
});
frmCafeteriaQuiz.getContentPane()。add(btnIHaveCold,BorderLayout.WEST);

JButton btnIHaveHot =新的JButton(我有热午餐);
btnIHaveHot.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
core.hotlunch();
}
});
frmCafeteriaQuiz.getContentPane()。add(btnIHaveHot,BorderLayout.EAST);
}

}

我正在运行cmd,将目录更改到我所有类的包。当我通过键入java Main运行Main时,我得到


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


我读过的一件事是,问题可能是由于从包中调用类引起的?它不是很详细,当我从程序主文件夹中运行程序时,它做了同样的事情。如果我在eclipse中运行程序,它的效果很好,没有显示任何错误。我知道eclipse使用不同的编译器。但没有我尝试过的作品。任何帮助都会很棒。谢谢。 :

解决方案

由于您的主要类在 com.leslie.quiz 包,你应该cd到编译输出的父目录,并运行命令:

  java com.leslie.quiz.Main 

请注意,默认的二进制输出Eclipse中的目录是项目的 bin 目录。虽然它在Eclipse中的Package Explorer视图中是隐藏的,但它仍然存在于文件系统中。您应该可以从Eclipse中的导航器视图中看到它。



bin 的内容将看起来像这个:

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

在这种情况下, cd到 bin 并运行 java 命令。


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

解决方案

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.

这篇关于java.lang.NoClassDefFoundError Main(错名称:com / leslie / quiz / Main)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆