我正在尝试使用 java 文件和相应的命令行工具来获得一个基本的 JUnit 4 设置(在我继续使用 eclipse 之前 - 我怀疑这会更容易开始工作,但我仍然想先这样做)和以下是我认为会引发问题的不言自明的输入/输出:
deniz@debian:~$ cd /tmp/temp2/src/com/example/example
deniz@debian:/tmp/temp2/src/com/example/example$ echo $CLASSPATH
.:/home/deniz/CLASSPATH_DIR:/usr/share/java/jogl.jar:/usr/share/java/gluegen-rt.jar:/usr/share/java/junit4.jar
deniz@debian:/tmp/temp2/src/com/example/example$ ls -l /usr/share/java/junit4.jar
lrwxrwxrwx 1 root root 16 Feb 8 2011 /usr/share/java/junit4.jar -> junit4-4.8.2.jar
deniz@debian:/tmp/temp2/src/com/example/example$ ls
MathUtils.java MathUtilsTest.java
deniz@debian:/tmp/temp2/src/com/example/example$ javac ./*.java
deniz@debian:/tmp/temp2/src/com/example/example$ ls
MathUtils.class MathUtils.java MathUtilsTest.class MathUtilsTest.java
deniz@debian:/tmp/temp2/src/com/example/example$ java MathUtils
Exception in thread "main" java.lang.NoClassDefFoundError: MathUtils (wrong name: com/example/example/MathUtils)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: MathUtils. Program will exit.
deniz@debian:/tmp/temp2/src/com/example/example$ java MathUtilsTest
Exception in thread "main" java.lang.NoClassDefFoundError: MathUtilsTest (wrong name: com/example/example/MathUtilsTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: MathUtilsTest. Program will exit.
deniz@debian:/tmp/temp2/src/com/example/example$ cat MathUtils.java
package com.example.example;
public class MathUtils
{
public static double multiply(double a, double b)
{
return a * b;
}
public static void main(String[] args)
{
double num = multiply(4.0,-5.0);
System.out.println("The number is: " + num);
}
}
deniz@debian:/tmp/temp2/src/com/example/example$ cat MathUtilsTest.java
package com.example.example;
import org.junit.Test;
import static org.junit.Assert.*;
public class MathUtilsTest
{
@Test
public void testMultiply()
{
double a = 5.0;
double b = -4.0;
double expected = -20.0;
double result = MathUtils.multiply(a, b);
assertEquals(expected, result, 0.00001);
}
}
deniz@debian:/tmp/temp2/src/com/example/example$
有人可以告诉我发生了什么以及如何解决这个问题吗?甚至 MathUtils 类也不起作用,我觉得它做得很好;它定义了包并有一个 main 方法。
如果需要比我提供的更多信息,请询问。
如果我犯了任何愚蠢的错误和/或假设,我深表歉意,因为我打字时非常疲倦;尽管如此,请纠正我。
任何帮助将不胜感激!提前致谢!