简体   繁体   English

从命令行运行java程序时,在线程“main”java.lang.NoClassDefFoundError'中出现异常

[英]'Exception in thread “main” java.lang.NoClassDefFoundError' when running java program from command line

What am I doing wrong here: 我在这做错了什么:

class Helo { 
   // main: generate some simple output 
   public static void main (String[] args) { 
      System.out.println ("Hello, world."); // print one line 
      System.out.println ("How are you?"); // print another 
   } 
} 

When I go into terminal I do: 当我进入终端时,我做:

cd ~
javac Atempt2.java (//that's the file name) 
java Atempt2 

and then it gives me this error message: 然后它给了我这个错误信息:

Exception in thread "main" java.lang.NoClassDefFoundError: Atempt2

So all in all this is what I do and what happens: 总而言之,这就是我所做的和所发生的事情:

david-allenders-macbook-pro:~ davidallender$ cd ~
david-allenders-macbook-pro:~ davidallender$ javac Atempt2.java
david-allenders-macbook-pro:~ davidallender$ java Atempt2
Exception in thread "main" java.lang.NoClassDefFoundError: Atempt2
david-allenders-macbook-pro:~ davidallender$ 

I am very new at this so please explain things in a very simple manner. 我是新手,所以请以非常简单的方式解释一下。

Thanks. 谢谢。

自从我完成任何java工作以来已经有一段时间了,但我很确定你的类名需要与你的文件名相匹配。

javac uses the class name to generate the output not the filename. javac使用类名生成输出而不是文件名。 So it will generate a Helo.class classfile. 因此它将生成一个Helo.class类文件。
java will take a class name and call the main function in the corresponding class file, here Hello.class. java将取一个类名并在相应的类文件中调用main函数,这里是Hello.class。

The ClassNotFoundError is thrown because javac never generated an Atemp2 classfile as there is no Atemp2 class in your source file. 抛出ClassNotFoundError是因为javac从未生成Atemp2类文件,因为源文件中没有Atemp2类。

Rename your Atempt2.java to Hello.java to get going, then: 将您的Atempt2.java重命名为Hello.java以继续,然后:

javac Helo.java
java Helo

See here for more discussion and the reasoning . 请参阅此处以获取更多讨论和推理

change: 更改:

class Helo

to

class Atempt2

in your source file. 在您的源文件中。

A .java file that declares a class must have the file name match the declared class name. 声明类的.java文件必须具有与声明的类名匹配的文件名。

The filename must match the name of the public class defined in the file. 文件名必须与文件中定义的public class的名称匹配。 In this case, you would either have to name the file "Helo.java" or renamed the class to Atempt2 . 在这种情况下,您要么必须将文件命名为“Helo.java”,要么将该类重命名为Atempt2

This is the very basic to start with java programming.Any program you write the name of the file must match with the public class of the program. 这是从java编程开始的非常基础。你编写的任何程序文件的名称必须与程序的公共类匹配。 Here in your program public class of the file is Helo so your file name must be Helo.java.Here the compiler is able to compile but JVM will search for Helo.class file to run. 在你的程序中,文件的公共类是Helo,所以你的文件名必须是Helo.java.Here编译器能够编译但JVM将搜索Helo.class文件来运行。 As there is no Helo.class file you are getting runtime Exception Exception in thread "main" java.lang.NoClassDefFoundError: Atempt2 因为没有Helo.class文件,你在线程“main”中得到运行时异常异常java.lang.NoClassDefFoundError:Atempt2

to complement josefx 's answer. 补充约瑟夫的回答。

The argument to the compiler ( javac ) is the name of the file or files to compile (as you did). 编译器的参数( javac )是要编译的文件的名称(正如您所做的那样)。

On the other side, the virtual machine ( java ) gets the name of the class whose main method is to be executed. 另一方面,虚拟机( java )获取要执行其main方法的类的名称。

One option would be 一种选择是

javac Atempt2.java    // the file name
java Helo             // the class name

Normally it is a good idea to have the file named the same way as the class. 通常,将文件命名为与类相同的方式是个好主意。 For public class this is a must (checked by compiler). 对于public class这是必须的(由编译器检查)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM