这是我第一次发布问题--我找到了一些类似的问题,但没有涉及这个问题。听起来很简单,但我不太确定为什么会发生这种情况。我的程序在Eclipse中运行得非常好,但从命令行运行时就不行了。我有几个类在一个名为“simpletree”的包中。
以下是BinaryTree.java:
当我执行以下操作时:
以下是BinaryTree.java:
package simpletree;
import java.io.*;
public class BinaryTree implements Serializable {
// Automatically generated UID
private static final long serialVersionUID = -3124224583476129954L;
BinaryTree leftNode; // left node
BinaryTree rightNode; // right node
// some code
}
class Tree implements Serializable {
private static final long serialVersionUID = 6591795896216994405L;
private BinaryTree root;
// some code
}
并且 Program1Test.java:
package simpletree;
public class Program1Test {
public static void main(String[] args) {
Tree tree = new Tree();
// some code
}
}
问题在于:从simpletree内部执行此操作编译正常:
javac BinaryTree.java Program1Test.java
当我执行以下操作时:
java Program1Test
我收到了这个:
Exception in thread "main" java.lang.NoClassDefFoundError: Program1Test (wrong n
ame: simpletree/Program1Test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Program1Test. Program will exit.
有什么想法吗?我已经正确设置了我的类路径,尝试使用包(simpletree.Program1Test)运行和不使用包运行。