0

我正在使用 Ubuntu 12.04,并且正在尝试在命令行上运行 Java 程序。

它是来自著名的编程面试书籍Cracking the Coding Interview的程序,目录和java文件的结构可以在以下位置看到

https://github.com/gaylemcd/ctci/tree/master/java

在这里,我正在运行Chapter 2/Question2_5/QuestionB.java,并且我使用LinkedListNode.javaCtCILibrary 目录中的类,我还需要Chapter 2/Question2_5/PartialSum.java

我去了Chapter 2/Question2_5目录,并使用编译命令编译了 QuestionB.java

javac -cp .:../../CtCiLibrary/CtCILibrary/LinkedListNode.java ./PartialSum.java QuestionB.java

并编译(花了一段时间才弄清楚编译命令),并制作了 QuestionB.class 文件。

我跑了java QuestionB,但它抛出了一个错误,

Exception in thread "main" java.lang.NoClassDefFoundError: QuestionB (wrong name: Question2_5/QuestionB)
    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)
Could not find the main class: QuestionB. Program will exit.

当我在谷歌上查找时,有很多参考资料,但没有一个对我有用。

谁能告诉我如何运行它?

4

1 回答 1

0

You didn't say what directory you were running the java command from. since you did not specify the classpath the command it will look in the current directory (I believe) and down the classpath in the environment. make sure that the file(s) are

  • available on the classpath
  • in directory appropriate for their package (if no package name then in the current directory)

Hope this provides some help.

于 2013-07-13T22:01:20.113 回答