无法运行简单的编译java程序?

时间:2010-09-27 17:11:08

标签: java compiler-construction javac

我在Arch Linux上,我刚刚安装了JRE和JDK,并且所有正确的bin文件(javac和java)都在/ opt / java / bin /

我只是编译了一个标准的hello世界,并使用运行javac ./hello.java的javac编译它并创建了一个类。

现在我的问题是运行它。我运行java ./helloworld.class并且它给了我一个错误,即使我指向java的文件是不存在的:

Exception in thread "main" java.lang.NoClassDefFoundError: //helloworld/class
Caused by: java.lang.ClassNotFoundException: ..helloworld.class
(..omitted for clarity..)
Could not find the main class: ./helloworld.class.  Program will exit.

你会注意到错误的第一行,它会引导路径//helloworld/class

当我向java提供一个绝对路径时,即java /home/foo/helloworld.class它会给出相同的错误,但在第一行中用/替换路径.,再次使用。

你认为有什么不对?我真的不知道为什么会这样做..

4 个答案:

答案 0 :(得分:3)

当你运行java时,你只需要传递完全限定的类名(包括包),而不是文件名。

java helloworld将寻找helloworld.class。

java helloworld.class将寻找helloworld / class.class

答案 1 :(得分:1)

您不运行文件 # java file.class 你把它作为 # javac PATH/file.java # java PATH/file

使用JAVA命令时不要添加.class。

答案 2 :(得分:1)

实际上你应该像这样编译它

javac helloword.java

运行程序

java helloword

答案 3 :(得分:0)

还有一件事:添加命令行选项“-classpath”。或者它是短版“-cp。”,即你的命令行应如下所示: java -cp。的HelloWorld

这是因为您的班级位于当前目录中。除此以外 ”。”应该被可以找到类的路径替换。

相关问题