The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Common Error Messages on Microsoft Windows SystemsMicrosoft Windows系统上的常见错误消息
'javac' is not recognized as an internal or external command, operable program or batch file
If you receive this error, Windows cannot find the compiler (如果收到此错误,Windows将找不到编译器(javac
).javac
)。
Here's one way to tell Windows where to find 这里有一种方法告诉Windows在哪里可以找到javac
.javac
。Suppose you installed the JDK in 假设您在C:\jdk1.8.0
.C:\jdk1.8.0
中安装了JDK。At the prompt you would type the following command and press Enter:在提示下,键入以下命令并按Enter键:
C:\jdk1.8.0\bin\javac HelloWorldApp.java
If you choose this option, you'll have to precede your 如果选择此选项,则每次编译或运行程序时,都必须在javac
and java
commands with C:\jdk1.8.0\bin\
each time you compile or run a program.javac
和java
命令之前加上C:\jdk1.8.0\bin\
。To avoid this extra typing, consult the section Updating the PATH variable in the JDK 8 installation instructions.为了避免这种额外的输入,请参阅JDK 8安装说明中更新PATH变量一节。
Class names, 'HelloWorldApp', are only accepted if annotation processing is explicitly requested
If you receive this error, you forgot to include the 如果收到此错误,则在编译程序时忘记包含.java
suffix when compiling the program. Remember, the command is javac HelloWorldApp.java
not javac HelloWorldApp
..java
后缀。请记住,该命令是javac HelloWorldApp.java
,而不是javac HelloWorldApp
。
Common Error Messages on UNIX SystemsUNIX系统上的常见错误消息
javac: Command not found
If you receive this error, UNIX cannot find the compiler, 如果收到此错误,UNIX将找不到编译器javac
.javac
。
Here's one way to tell UNIX where to find 这里有一种方法告诉UNIX在哪里可以找到javac
.javac
。Suppose you installed the JDK in 假设您在/usr/local/jdk1.8.0
./usr/local/jdk1.8.0
中安装了JDK。At the prompt you would type the following command and press Return:在提示下,键入以下命令并按Return键:
/usr/local/jdk1.8.0/javac HelloWorldApp.java
Note: If you choose this option, each time you compile or run a program, you'll have to precede your 注意:如果选择此选项,则每次编译或运行程序时,都必须在javac
and java
commands with /usr/local/jdk1.8.0/
.javac
和java
命令之前加上/usr/local/jdk1.8.0/
。To avoid this extra typing, you could add this information to your PATH variable.为了避免这种额外的键入,可以将此信息添加到PATH变量中。The steps for doing so will vary depending on which shell you are currently running.执行此操作的步骤因当前运行的shell而异。
Class names, 'HelloWorldApp', are only accepted if annotation processing is explicitly requested
If you receive this error, you forgot to include the 如果收到此错误,则在编译程序时忘记包含.java
suffix when compiling the program..java
后缀。Remember, the command is 请记住,该命令是javac HelloWorldApp.java
not javac HelloWorldApp
.javac HelloWorldApp.java
,而不是javac HelloWorldApp
。
Syntax Errors (All Platforms)语法错误(所有平台)
If you mistype part of a program, the compiler may issue a syntax error.如果您键入了程序的一部分,编译器可能会发出语法错误。The message usually displays the type of the error, the line number where the error was detected, the code on that line, and the position of the error within the code.该消息通常显示错误类型、检测到错误的行号、该行上的代码以及错误在代码中的位置。Here's an error caused by omitting a semicolon (这里有一个因省略分号(;
) at the end of a statement:;
)而导致的错误在陈述结束时:
Testing.java:8: error: ';' expected count++ ^ 1 error
If you see any compiler errors, then your program did not successfully compile, and the compiler did not create a 如果您看到任何编译器错误,那么您的程序没有成功编译,并且编译器没有创建.class
file..class
文件。Carefully verify the program, fix any errors that you detect, and try again.仔细验证程序,修复检测到的任何错误,然后重试。
Semantic Errors语义错误
In addition to verifying that your program is syntactically correct, the compiler checks for other basic correctness.除了验证程序的语法正确性外,编译器还会检查其他基本正确性。For example, the compiler warns you each time you use a variable that has not been initialized:例如,每次使用尚未初始化的变量时,编译器都会发出警告:
Testing.java:8: error: variable count might not have been initialized count++; ^ Testing.java:9: error: variable count might not have been initialized System.out.println("Input has " + count + " chars."); ^ 2 errors
Again, your program did not successfully compile, and the compiler did not create a 同样,您的程序没有成功编译,并且编译器没有创建.class
file..class
文件。Fix the error and try again.请修复错误并重试。
Error Messages on Microsoft Windows SystemsMicrosoft Windows系统上的错误消息
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp
If you receive this error, 如果收到此错误,java
cannot find your bytecode file, HelloWorldApp.class
.java
将无法找到字节码文件HelloWorldApp.class
。
One of the places java试图查找java
tries to find your .class
file is your current directory..class
文件的地方之一是您的当前目录。So if your 因此,如果.class
file is in C:\java
, you should change your current directory to that..class
文件位于C:\java
中,则应将当前目录更改为该目录。To change your directory, type the following command at the prompt and press Enter:要更改目录,请在提示符处键入以下命令,然后按Enter键:
cd c:\java
The prompt should change to 提示应更改为C:\java>
.C:\java>
。If you enter 如果在提示符处输入dir
at the prompt, you should see your .java
and .class
files.dir
,您应该会看到.java
和.class
文件。Now enter 现在再次输入java HelloWorldApp
again.java HelloWorldApp
。
If you still have problems, you might have to change your CLASSPATH variable.如果仍然存在问题,则可能必须更改CLASSPATH变量。To see if this is necessary, try clobbering the classpath with the following command.若要查看这是否必要,请尝试使用以下命令对类路径进行重击。
set CLASSPATH=
Now enter 现在再次输入java HelloWorldApp
again.java HelloWorldApp
。If the program works now, you'll have to change your CLASSPATH variable.如果该程序现在可以运行,则必须更改CLASSPATH
变量。To set this variable, consult the Updating the PATH variable section in the JDK 8 installation instructions.要设置此变量,请参阅JDK 8安装说明中的更新PATH
变量部分。The CLASSPATH variable is set in the same manner.CLASSPATH
变量的设置方式相同
Could not find or load main class HelloWorldApp.class
A common mistake made by beginner programmers is to try and run the 初学者犯的一个常见错误是尝试在编译器创建的java
launcher on the .class
file that was created by the compiler..class
文件上运行java
启动器。For example, you'll get this error if you try to run your program with 例如,如果尝试使用java HelloWorldApp.class
instead of java HelloWorldApp
.java HelloWorldApp.class
而不是java HelloWorldApp
运行程序,则会出现此错误。Remember, the argument is the name of the class that you want to use, not the filename.记住,参数是要使用的类的名称,而不是文件名。
Exception in thread "main" java.lang.NoSuchMethodError: main
The Java VM requires that the class you execute with it have a JavaVM要求使用它执行的类具有一个主方法,在该main
method at which to begin execution of your application.main
方法处开始执行应用程序。A Closer Look at the "Hello World!" Application discusses the 详细讨论了main
method in detail.main
方法。
Error Messages on UNIX SystemsUNIX系统上的错误消息
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp
If you receive this error, 如果收到此错误,java
cannot find your bytecode file, HelloWorldApp.class
.java
将无法找到字节码文件HelloWorldApp.class
。
One of the places java
tries to find your bytecode file is your current directory.java
试图查找字节码文件的地方之一是当前目录。So, for example, if your bytecode file is in 因此,例如,如果字节码文件位于/home/jdoe/java
, you should change your current directory to that./home/jdoe/java
中,则应将当前目录更改为该目录。To change your directory, type the following command at the prompt and press Return:要更改目录,请在提示符处键入以下命令,然后按Return键:
cd /home/jdoe/java
If you enter 如果在提示符处输入pwd,您应该看到pwd
at the prompt, you should see /home/jdoe/java
./home/jdoe/java
。If you enter 如果在提示符处输入ls
at the prompt, you should see your .java
and .class
files.ls
,您应该会看到.java
和.class
文件。Now enter 现在再次输入java HelloWorldApp
again.java HelloWorldApp
。
If you still have problems, you might have to change your CLASSPATH environment variable.如果仍然存在问题,则可能必须更改CLASSPATH
环境变量。To see if this is necessary, try clobbering the classpath with the following command.若要查看这是否必要,请尝试使用以下命令对类路径进行重击。
unset CLASSPATH
Now enter 现在再次输入java HelloWorldApp
again.java HelloWorldApp
。If the program works now, you'll have to change your CLASSPATH variable in the same manner as the PATH variable above.如果程序现在可以运行,您必须以与上面的PATH
变量相同的方式更改CLASSPATH
变量。
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp/class
A common mistake made by beginner programmers is to try and run the 初学者犯的一个常见错误是尝试在编译器创建的java
launcher on the .class
file that was created by the compiler..class
文件上运行java
启动器。For example, you'll get this error if you try to run your program with 例如,如果尝试使用java HelloWorldApp.class
instead of java HelloWorldApp
.java HelloWorldApp.class
而不是java HelloWorldApp
运行程序,则会出现此错误。Remember, the argument is the name of the class that you want to use, not the filename.记住,参数是要使用的类的名称,而不是文件名。
Exception in thread "main" java.lang.NoSuchMethodError: main
The Java VM requires that the class you execute with it have a JavaVM要求使用它执行的类具有一个主方法,在该main
method at which to begin execution of your application.main
方法处开始执行应用程序。A Closer Look at the "Hello World!" Application discusses the 详细讨论了main
method in detail.main
要方法。
Applet or Java Web Start Application Is Blocked小程序或Java Web Start应用程序被阻止
If you are running an application through a browser and get security warnings that say the application is blocked, check the following items:如果您正在通过浏览器运行应用程序,并且收到表明该应用程序已被阻止的安全警告,请检查以下项目:
Verify that the attributes in the JAR file manifest are set correctly for the environment in which the application is running.验证JAR文件清单中的属性是否针对运行应用程序的环境正确设置。The Permissions attribute is required. In a NetBeans project, you can open the manifest file from the Files tab of the NetBeans IDE by expanding the project folder and double-clicking manifest.mf.“权限”属性是必需的。在NetBeans项目中,可以通过展开项目文件夹并双击manifest.mf从NetBeans IDE的“文件”选项卡打开清单文件。
Verify that the application is signed by a valid certificate and that the certificate is located in the Signer CA keystore.验证应用程序是否由有效证书签名,以及证书是否位于签名者CA密钥库中。
If you are running a local applet, set up a web server to use for testing.如果正在运行本地小程序,请设置用于测试的web服务器。You can also add your application to the exception site list, which is managed in the Security tab of the Java Control Panel.您还可以将应用程序添加到异常站点列表中,该列表在Java控制面板的安全选项卡中进行管理。