1. Overview
1.概述
When compiling Java programs on the command line, it’s expected that any mismatch in the expected command-line options or arguments will result in an error.
在命令行上编译Java程序时,预期的命令行选项或参数的任何不匹配都会导致错误。
In this tutorial, we’ll first investigate the “Class Names Are Only Accepted if Annotation Processing Is Explicitly Requested” error. Then, we’ll look at some other common compile errors.
在本教程中,我们将首先研究“Class Names Are Only Accepted if Annotation Processing Is Explicitly Requested”/em>错误。然后,我们将研究其他一些常见的编译错误。
2. Example of Error
2.错误的例子
Let’s imagine we have the following class DemoClass:
让我们想象一下,我们有如下的类DemoClass。
package com.baeldung;
public class DemoClass {
// fields and methods
}
Now, let’s try to compile the DemoClass using the javac command:
现在,让我们试着用DemoClass来编译javac命令来编译。
javac DemoClass
The above command will give an error:
上述命令会出现错误。
error: Class names, 'DemoClass', are only accepted if annotation processing is explicitly requested
1 error
The error seems to be related to annotation processing and is a bit cryptic since the DemoClass has no code related to annotation processing. The actual reason for this error is the DemoClass is not an annotation processing source file.
这个错误似乎与注解处理有关,而且有点令人费解,因为DemoClass没有与注解处理有关的代码。这个错误的实际原因是DemoClass不是一个注释处理源文件。
Annotation processing source files are a handy technique for generating additional source code at compile time. In contrast to standard Java source files, to compile these source files, it’s not required to provide the .java file extension.
注释处理源文件是一种在编译时生成额外源代码的便捷技术。与标准Java源文件相比,要编译这些源文件,不需要提供.java文件扩展名.。
3. Solving the Problem
3.解决问题
Let’s compile the DemoClass again with the correct file name extension .java:
让我们再次编译DemoClass,用正确的文件名扩展名.java。
javac DemoClass.java
As expected, we’ll get the source file compiled into DemoClass.class file.
正如预期的那样,我们会得到源文件被编译成DemoClass.class文件。
4. Additional Tips and Tricks
4. 附加提示和技巧
While it’s an easy fix when we know the correct way to compile, we may still encounter similar difficulties while compiling or running applications.
虽然当我们知道正确的编译方式时,这是一个很容易解决的问题,但我们在编译或运行应用程序时仍然可能遇到类似的困难。
4.1. Using Incorrect File Extension
4.1.使用不正确的文件扩展名
Let’s now try to compile the source file with the below command which has a typo – “.JAVA” in all uppercase:
现在让我们尝试用下面的命令来编译源文件,其中有一个错别字–“.JAVA”全部为大写字母。
javac DemoClass.JAVA
Doing this will produce the same error message we saw above:
这样做会产生与我们上面看到的相同的错误信息。
error: Class names, 'DemoClass.JAVA', are only accepted if annotation processing is explicitly requested
1 error
4.2. Main Class Error
4.2.主类错误
Let’s imagine we have a DemoApplication class that has a main method:
让我们想象一下,我们有一个DemoApplication类,它有一个main方法。
public class DemoApplication {
public static void main(String[] args) {
System.out.println("This is a DemoApplication");
}
}
Let’s now execute the application using the java command:
现在让我们使用java命令来执行该应用程序。
java DemoApplication.class
The result is a ClassNotFoundException:
其结果是一个ClassNotFoundException。
Error: Could not find or load main class DemoApplication.Class
Caused by: java.lang.ClassNotFoundException: DemoApplication.Class
Now, let’s try running the application without any file extension – not even .class or .java:
现在,让我们试着运行没有任何文件扩展名的应用程序–甚至没有.class或.java。
java DemoApplication
We should see the output on our console:
我们应该在我们的控制台看到输出。
This is a DemoApplication
5. Conclusion
5.总结
In this article, we’ve learned how the incorrect usage or omission of the .java file extension causes errors when compiling classes from the command line. Also, we’ve seen a few other errors related to the incorrect usage of command-line arguments both compiling and running standalone applications.
在这篇文章中,我们已经了解了在从命令行编译类时,错误地使用或遗漏.java文件扩展名是如何导致错误的。此外,我们还看到了其他一些与编译和运行独立应用程序时不正确使用命令行参数有关的错误。