Junit 我给出了绝对路径,但java无法创建类

Junit 我给出了绝对路径,但java无法创建类,junit,classpath,Junit,Classpath,我不明白为什么会这样 ioannis@ioannis-GA-MA74GM-S2H:~$java-cp/opt/junit4.6/junit-4.6.jar org.junit.runner.JUnitCore/opt/CalculatorTest 给出了错误: Could not find class: /opt/CalculatorTest 这项工作很好: ioannis@ioannis-GA-MA74GM-S2H:~$ java -cp /opt/junit4.6/junit-4.6.ja

我不明白为什么会这样

ioannis@ioannis-GA-MA74GM-S2H:~$java-cp/opt/junit4.6/junit-4.6.jar org.junit.runner.JUnitCore/opt/CalculatorTest

给出了错误:

Could not find class: /opt/CalculatorTest
这项工作很好:

ioannis@ioannis-GA-MA74GM-S2H:~$ java -cp /opt/junit4.6/junit-4.6.jar:/opt org.junit.runner.JUnitCore CalculatorTest
JUnit version 4.6
.
Time: 0.005

OK (1 test)

在第一个例子中,我给出了class/opt/CalculatorTest的完整路径,类名只是类名,而不是路径。如果它在一个包中,您可以使用foo.Bar。如果在默认包中,则只能使用Bar


正如您所注意到的,类路径是标识Java查找类时应查找的物理目录的位置。

完全限定的类名以
.class
格式指定。如果CalculatorTest有
包选项
在其源文件中声明,然后您应该从opt目录所在的目录或opt目录位于类路径中的目录中将其称为
opt.CalculatorTest
。请尝试
opt.CalculatorTest
而不是
/opt/CalculatorTest
类CalculatorTest不在包中