Lesson: Common Problems (and Their Solutions)

编译问题

在微软Windows系统上的常见错误

'javac' is not recognized as an internal or external command,operable program or batch file

 

如果出现这个错误,是因为你的Windows系统上不能找到javac编译。

这里有一种方法告诉Windows系统去哪里找到javac命令。假使你已经安装了JDK在C:\jdk1.8.0.在下面的提示下,你可以输入一下的命令和按回车键。

C:\jdk1.8.0\bin\javac HelloWorldApp.java

如果你选择这种方式,那你每次在编译或运行程序之前都要进入C:\jdk1.8.0\bin\ 这个目录。为了避免这个多余的操作,可以在系统path变量对JDK8的安装路径进行配置。

Class names, 'HelloWorldApp', are onlyaccepted if annotation processing is explicitly requested

如果出现以上这个错误,是因为你在编译的时候忘记加上 .java这个后缀名了。记住,这个命令是javacHelloWorldApp.java 而不是 javac HelloWorldApp。

UNIX 系统上的常见错误

javac: Command not found

如果出现这个错误,UNIX无法找到Javac 编译器。

这有一种方法去高手UNIX在哪里可以找到javac这个命令。假如你已经安装了JDK在/usr/local/jdk1.8.0目录下。你可以输入如下的命令然后回车:

/usr/local/jdk1.8.0/javac HelloWorldApp.java

Note: 如果你选择这种方式,那么你每次在编译或运行你的程序之前都要进入/usr/local/jdk1.8.0/目录。为了避免这个多余的操作,你可以加入这个信息到你的PATH环境变量中。这些步骤将改变你现在在运行的shell依赖。Classnames, 'HelloWorldApp', are only accepted if annotation processing isexplicitly requested

如果出现以上这个错误,是因为你在编译的时候忘记加上 .java这个后缀名了。记住,这个命令是javacHelloWorldApp.java 而不是 javac HelloWorldApp。

语法错误(所有平台)

如果你错误录入了部分程序,编译器可能提醒一个语法错误。这个信息一般会显示错误的类型,该行数会被发现,在这行上的错误代码,还有错误代码里的位置。这里给出一个在语句后面省略了分好而引起的错误:

testing.java:14: `;' expected.
System.out.println("Input has " + count + " chars.")
                                                     ^
1 error

有时候编译器不能猜测你的意图而打印出混乱的错误信息或多种错误信息——如果这错误覆盖了几行代码。例如,一下的代码片段从粗体行省略了一个冒号:

while (System.in.read() != -1)
    count++
System.out.println("Input has " + count + " chars."); 

当运行这个代码,编译器将提示两个错误信息:

testing.java:13: Invalid type expression.
        count++
                 ^
testing.java:14: Invalid declaration.
    System.out.println("Input has " + count + " chars.");
                      ^
2 errors

编译器之所以提示两个错误信息是因为它处理 count++ 之后,编译器的声明表明它是在一个表达式的中间。没有了分号,编译器无法知道这个语句是否完成。

如果你看见有编译错误,而你的程序无法成功编译,无法创建 .class 文件。那么认真检查程序,修改你发现的错误,再编译。

语义错误

除了验证你的程序语法上的准确性,编译器还检查其他基本的准确。例如,使用未初始化的变量时候都会提醒你:

testing.java:13: Variable count may not have been initialized.
        count++
        ^
testing.java:14: Variable count may not have been initialized.
    System.out.println("Input has " + count + " chars.");
                                       ^
2 errors

再次,你的程序不能成功编译吗,编译器不能创建 .class文件。修正这个错误再试之。

运行问题

微软Windows平台错误信息

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

如果出现这个错误,Java不能发现你的字节码文件HelloWorldApp.class。

Java视图去寻找你的 .class文件所在位置之一是你当前的目录。所以如果你的.class文件是在C:\java,那么你应该改变你当前的目录指向那。要改变当前的目录,输入如下的命令回车即可:

cd c:\java

这个提示将改变当前的目录指向 C:\java,如果你在提示中键入dir,你将可以看到你的.java和.class文件。现在再输入javaHelloWorldApp。

如果仍然有问题,你可能要改变你的CLASSPATH变量了。要看着是否必需的,通过如下命令清除classpath变量

set CLASSPATH=

现在再输入java HelloWorldApp。如果程序可以运行了,那么你就要改变你的CLASSPATH变量了。要设置这个变量,可查阅JDK8安装说明中Updating thePATH variable 章节。CLASSPATH变量的设置方法一样。

Could not find or load main classHelloWorldApp.class

这是一个初级程序员运行编译创建的 .class文件经常出现的错误。例如,如果你用javaHelloWorldApp.class代替javaHelloWorldApp。记住,这个内容是类的名称,而不是文件名。

Exception in thread "main"java.lang.NoSuchMethodError: main

Java虚拟机要求你运行的类要有一个main方法,这个在A Closer Look at the "HelloWorld!" Application 已经讨论过了。

UNIX平台错误信息

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

如果你收到这个错误,是因为Java无法找到你的.class字节码文件, HelloWorldApp.class.

Java试图去寻找你的 .class文件所在位置之一是你当前的目录。所以如果你的.class文件是在/home/jdoe/java,那么你应该改变你当前的目录指向那。要改变当前的目录,输入如下的命令回车即可:

cd /home/jdoe/java

如果你在终端输入pwd,你应该会看到 /home/jdoe/java。如果你键入ls,你将看到你的.java和.class文件。现在再键入java HelloWorldApp。

如果你仍然发现问题,你可能要改变你的CLASSPATH环境变量了。要看这是否必要,可通过以下命令清除掉classpath

unset CLASSPATH

现在再键入java HelloWorldApp。如果现在可以运行了,你将要改变你的CLASSPATH环境变量了——就像设置path变量一样。

Exception in thread "main"java.lang.NoClassDefFoundError: HelloWorldApp/class

这是一个初级程序员运行编译创建的 .class文件经常出现的错误。例如,如果你用javaHelloWorldApp.class代替javaHelloWorldApp。记住,这个内容是类的名称,而不是文件名。

 

Exception in thread "main"java.lang.NoSuchMethodError: main

Java虚拟机要求你运行的类要有一个main方法,这个在A Closer Look at the "HelloWorld!" Application 已经讨论过了。

Applet or Java Web Start Application Is Blocked

如果你在通过浏览器运行应用的时候出现说应用阻塞的安全警告,请坚持以下各项:

  • Verify that the attributes in the JAR file manifest are set correctly for the environment in which the application is running. 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.
  • Verify that the application is signed by a valid certificate and that the certificate is located in the Signer CA keystore.
  • If you are running a local applet, set up a web server to use for testing. You can also add your application to the exception site list, which is managed in the Security tab of the Java Control Panel.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值