我有三个班。主要、核心和开始。这是主要的代码:
package com.leslie.quiz;
public class Main {
public static void main(String[] args) {
com.leslie.quiz.Start.main(null);
}
}
这是核心的代码:
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;
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);
}
}
我正在运行 cmd,并将目录更改为我所有类所在的包。当我通过键入“java Main”运行 Main 时,我得到
java.lang.NoClassDefFoundError Main(错误名称:com/leslie/quiz/Main)
我读过的一件事是,问题可能是由从包内部调用类引起的?它不是很详细,当我从程序主文件夹中运行程序时,它做了同样的事情。如果我在 Eclipse 中运行该程序,它工作得很好并且没有显示错误。而且我知道 eclipse 使用不同的编译器。但是我尝试过的没有任何效果。任何帮助都会很棒。谢谢。:)