我尝试更改类名时出错

时间:2022-06-23 20:49:58
class HelloObject {
    void speak() {
        System.out.println("Hello (from object)!");
    }
}
class HelloTester {
    public static void main(String[] args) {
        HelloObject object = new HelloObject();
        object.speak();
    }
}

When I change the "HelloTester" class name to something like "HelloTester2", the program suddenly works. The class file is called ClassesBegin.java.

当我将“HelloTester”类名更改为“HelloTester2”之类的程序时,程序突然起作用。类文件名为ClassesBegin.java。

Why does the java program not work when I try to change the name of the class?

当我尝试更改类的名称时,为什么java程序不起作用?

EDIT: Sorry I should have clarified more. I changed the class name to HelloTestera and this is the error I get: Exception in thread "main" java.lang.NoClassDefFoundError: HelloTester But it works even when the file name has nothing to do with a class name. It works with HelloTester when the file name is ClassesBegin.java

编辑:对不起,我应该澄清更多。我将类名更改为HelloTestera,这是我得到的错误:线程“main”中的异常java.lang.NoClassDefFoundError:HelloTester但是即使文件名与类名无关,它也能正常工作。当文件名为ClassesBegin.java时,它与HelloTester一起使用

5 个解决方案

#1


You need to change the file name, not just the class name.

您需要更改文件名,而不仅仅是类名。

In Java, the .java and .class names have to be identical to the class name.

在Java中,.java和.class名称必须与类名相同。

Hence, each class has to go to a separate file with its name so that a separate .class file is created.

因此,每个类必须使用其名称转到单独的文件,以便创建单独的.class文件。

Putting two different classes in the same file is a C++ practice that works with its compilation model, not with Java.

将两个不同的类放在同一个文件中的是C ++实践,它与编译模型一起使用,而不是Java。

Edit: User ended up clarifying what caused his error, so obviously my answer here is not relevant. All the above applies to public classes. You can pull that off for package-level classes though I have to say that I consider that a horrible practice. If you're going to have something used by multiple classes in your package, give it its own file. If it's used just by one class, make it an inner class...

编辑:用户最终澄清了导致他的错误的原因,所以显然我的答案与此无关。以上所有内容均适用于公共课程。虽然我不得不说我认为这是一种可怕的做法,但你可以将其用于包级别的课程。如果您要在包中使用多个类所使用的内容,请为其提供自己的文件。如果它仅由一个类使用,那么将其作为内部类......

#2


"EDIT: Sorry I should have clarified more. I changed the class name to HelloTestera and this is the error I get: Exception in thread "main" java.lang.NoClassDefFoundError: HelloTester But it works even when the file name has nothing to do with a class name. It works with HelloTester when the file name is ClassesBegin.java"

“编辑:对不起我应该澄清更多。我将类名更改为HelloTestera,这是我得到的错误:线程中的异常”main“java.lang.NoClassDefFoundError:HelloTester但是即使文件名无关具有类名。当文件名为ClassesBegin.java时,它与HelloTester一起使用“

The file name and the class name must match if the class is public.

如果类是公共的,则文件名和类名必须匹配。

If you chagned the class name to "HelloTestera" but ran "java HelloTester" (which is what java.lang.NoClassDefFoundError: HelloTester would indicate) then the issue is that you passed the wrong class name to "java".

如果您将类名称变为“HelloTestera”但运行“java HelloTester”(这是java.lang.NoClassDefFoundError:HelloTester将指示的那样),则问题是您将错误的类名传递给“java”。

But save yourself a lot of time and name the class and the file the same thing and keep it at one top level class per file. A simple way to "force" that is to make all of your classes public for now (you can only have one public class per file). This will really save you from making some mistakes.

但是节省了很多时间,并将类和文件命名为同一个东西,并将其保存在每个文件的一个*类中。 “强制”的一种简单方法是将所有类公开(现在每个文件只能有一个公共类)。这真的可以避免你犯错误。

#3


You are allowed to have as many non-public classes in your ClassesBegin file as you like in terms of compilation. But only the public (ClassesBegin in this case; until you change the name of the file) is able to be used externally.

在编译方面,您可以在ClassesBegin文件中拥有尽可能多的非公共类。但只有公共(在这种情况下为ClassesBegin;直到您更改文件的名称)才能在外部使用。

In particular, the main() method must be public, and in a public class to be able to be found by java. Rename your file to HelloTester to make it work.

特别是,main()方法必须是public,并且在公共类中可以通过java找到。将文件重命名为HelloTester以使其正常工作。

Or - rename the HelloTester class in your IDE, which probably is relaming the file automatically, since it has a main method, and the IDE knows that it needs to be the public class...

或者 - 在IDE中重命名HelloTester类,这可能是自动重定位文件,因为它有一个main方法,并且IDE知道它需要是公共类...

#4


The easiest way to do this is:

最简单的方法是:

1) only one top level class per file 2) the name of the class and the name of the file must match (name and CasE)

1)每个文件只有一个*类2)类的名称和文件名必须匹配(name和CasE)

This makes it easier to find you classes (the name of the class is the name of the file) and you don't wind up with some odd issues where the compielr cannot find all of the classes to copmpile.

这样可以更容易地找到您的类(类的名称是文件的名称),并且您不会遇到一些奇怪的问题,其中compielr无法找到所有类的copmpile。

Java also has a restriction where the name of a public class 100% must be the same as the name of the file. The restriction is only on public classes (or interfaces or enums). You can have as many non-public types as you want in a file... but don't do that - stick with one top level class/interface/enum per file.

Java还有一个限制,其中公共类的名称100%必须与文件名相同。限制仅适用于公共类(或接口或枚举)。您可以在文件中拥有任意数量的非公共类型...但不要这样做 - 坚持每个文件一个*类/接口/枚举。

#5


You write:

I changed the class name to HelloTestera and this is the error I get: Exception in thread "main" java.lang.NoClassDefFoundError: HelloTester

我将类名更改为HelloTestera,这是我得到的错误:线程“main”中的异常java.lang.NoClassDefFoundError:HelloTester

It seems you are not actually running the renamed class but the old one. Did you call Java with the new changed class name? Did you recompile the file before running the class?

看起来你实际上并没有运行重命名的类而是旧的类。您是否使用新更改的类名称调用Java?在运行类之前是否重新编译了文件?

After renaming the class, you should first run:

重命名该类后,您应该首先运行:

javac ClassesBegin.java

And then:

java HelloTestera

Which for me yields:

对我来说产量:

Hello (from object)!

你好(来自对象)!

Usually, when using an IDE, these issues are handled for you (compile before running).

通常,在使用IDE时,会为您处理这些问题(在运行之前编译)。

#1


You need to change the file name, not just the class name.

您需要更改文件名,而不仅仅是类名。

In Java, the .java and .class names have to be identical to the class name.

在Java中,.java和.class名称必须与类名相同。

Hence, each class has to go to a separate file with its name so that a separate .class file is created.

因此,每个类必须使用其名称转到单独的文件,以便创建单独的.class文件。

Putting two different classes in the same file is a C++ practice that works with its compilation model, not with Java.

将两个不同的类放在同一个文件中的是C ++实践,它与编译模型一起使用,而不是Java。

Edit: User ended up clarifying what caused his error, so obviously my answer here is not relevant. All the above applies to public classes. You can pull that off for package-level classes though I have to say that I consider that a horrible practice. If you're going to have something used by multiple classes in your package, give it its own file. If it's used just by one class, make it an inner class...

编辑:用户最终澄清了导致他的错误的原因,所以显然我的答案与此无关。以上所有内容均适用于公共课程。虽然我不得不说我认为这是一种可怕的做法,但你可以将其用于包级别的课程。如果您要在包中使用多个类所使用的内容,请为其提供自己的文件。如果它仅由一个类使用,那么将其作为内部类......

#2


"EDIT: Sorry I should have clarified more. I changed the class name to HelloTestera and this is the error I get: Exception in thread "main" java.lang.NoClassDefFoundError: HelloTester But it works even when the file name has nothing to do with a class name. It works with HelloTester when the file name is ClassesBegin.java"

“编辑:对不起我应该澄清更多。我将类名更改为HelloTestera,这是我得到的错误:线程中的异常”main“java.lang.NoClassDefFoundError:HelloTester但是即使文件名无关具有类名。当文件名为ClassesBegin.java时,它与HelloTester一起使用“

The file name and the class name must match if the class is public.

如果类是公共的,则文件名和类名必须匹配。

If you chagned the class name to "HelloTestera" but ran "java HelloTester" (which is what java.lang.NoClassDefFoundError: HelloTester would indicate) then the issue is that you passed the wrong class name to "java".

如果您将类名称变为“HelloTestera”但运行“java HelloTester”(这是java.lang.NoClassDefFoundError:HelloTester将指示的那样),则问题是您将错误的类名传递给“java”。

But save yourself a lot of time and name the class and the file the same thing and keep it at one top level class per file. A simple way to "force" that is to make all of your classes public for now (you can only have one public class per file). This will really save you from making some mistakes.

但是节省了很多时间,并将类和文件命名为同一个东西,并将其保存在每个文件的一个*类中。 “强制”的一种简单方法是将所有类公开(现在每个文件只能有一个公共类)。这真的可以避免你犯错误。

#3


You are allowed to have as many non-public classes in your ClassesBegin file as you like in terms of compilation. But only the public (ClassesBegin in this case; until you change the name of the file) is able to be used externally.

在编译方面,您可以在ClassesBegin文件中拥有尽可能多的非公共类。但只有公共(在这种情况下为ClassesBegin;直到您更改文件的名称)才能在外部使用。

In particular, the main() method must be public, and in a public class to be able to be found by java. Rename your file to HelloTester to make it work.

特别是,main()方法必须是public,并且在公共类中可以通过java找到。将文件重命名为HelloTester以使其正常工作。

Or - rename the HelloTester class in your IDE, which probably is relaming the file automatically, since it has a main method, and the IDE knows that it needs to be the public class...

或者 - 在IDE中重命名HelloTester类,这可能是自动重定位文件,因为它有一个main方法,并且IDE知道它需要是公共类...

#4


The easiest way to do this is:

最简单的方法是:

1) only one top level class per file 2) the name of the class and the name of the file must match (name and CasE)

1)每个文件只有一个*类2)类的名称和文件名必须匹配(name和CasE)

This makes it easier to find you classes (the name of the class is the name of the file) and you don't wind up with some odd issues where the compielr cannot find all of the classes to copmpile.

这样可以更容易地找到您的类(类的名称是文件的名称),并且您不会遇到一些奇怪的问题,其中compielr无法找到所有类的copmpile。

Java also has a restriction where the name of a public class 100% must be the same as the name of the file. The restriction is only on public classes (or interfaces or enums). You can have as many non-public types as you want in a file... but don't do that - stick with one top level class/interface/enum per file.

Java还有一个限制,其中公共类的名称100%必须与文件名相同。限制仅适用于公共类(或接口或枚举)。您可以在文件中拥有任意数量的非公共类型...但不要这样做 - 坚持每个文件一个*类/接口/枚举。

#5


You write:

I changed the class name to HelloTestera and this is the error I get: Exception in thread "main" java.lang.NoClassDefFoundError: HelloTester

我将类名更改为HelloTestera,这是我得到的错误:线程“main”中的异常java.lang.NoClassDefFoundError:HelloTester

It seems you are not actually running the renamed class but the old one. Did you call Java with the new changed class name? Did you recompile the file before running the class?

看起来你实际上并没有运行重命名的类而是旧的类。您是否使用新更改的类名称调用Java?在运行类之前是否重新编译了文件?

After renaming the class, you should first run:

重命名该类后,您应该首先运行:

javac ClassesBegin.java

And then:

java HelloTestera

Which for me yields:

对我来说产量:

Hello (from object)!

你好(来自对象)!

Usually, when using an IDE, these issues are handled for you (compile before running).

通常,在使用IDE时,会为您处理这些问题(在运行之前编译)。