Documentation

The Java™ Tutorials
Trail: Getting Started

Lesson: Common Problems (and Their Solutions)课程:常见问题(及其解决方案)

Compiler Problems编译器问题

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 (javac).如果收到此错误,Windows将找不到编译器(javac)。

Here's one way to tell Windows where to find javac.这里有一种方法告诉Windows在哪里可以找到javacSuppose 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.如果选择此选项,则每次编译或运行程序时,都必须在javacjava命令之前加上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, javac.如果收到此错误,UNIX将找不到编译器javac

Here's one way to tell UNIX where to find javac.这里有一种方法告诉UNIX在哪里可以找到javacSuppose 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/.注意:如果选择此选项,则每次编译或运行程序时,都必须在javacjava命令之前加上/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.请修复错误并重试。

Runtime Problems运行时问题

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 tries to find your .class file is your current directory.java试图查找.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 HelloWorldAppIf 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 main method at which to begin execution of your application.JavaVM要求使用它执行的类具有一个主方法,在该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 at the prompt, you should see /home/jdoe/java.如果在提示符处输入pwd,您应该看到/home/jdoe/javaIf 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 HelloWorldAppIf 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 main method at which to begin execution of your application.JavaVM要求使用它执行的类具有一个主方法,在该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:如果您正在通过浏览器运行应用程序,并且收到表明该应用程序已被阻止的安全警告,请检查以下项目:


Previous page: Previous Lesson
Next page: End of Trail