java编译错误和运行错误_常见的Java运行时错误

java编译错误和运行错误

Consider the following segment of Java code, stored in a file called JollyMessage.java:

考虑以下Java代码段,这些段存储在名为JollyMessage.java的文件中:

 // A jolly message is written to the screen!
class Jollymessage
{
   public static void main(String[] args) {
     //Write the message to the terminal window
     System.out.println("Ho Ho Ho!");
   }
}

At program execution, this code will produce a runtime error message. In other words, a mistake has been made somewhere, but the error won’t be identified when the program is compiled, only when it is run.

在程序执行时,此代码将产生运行时错误消息。 换句话说,某个地方犯了一个错误,但是只有在程序运行时,才可以在编译该程序时识别该错误。

调试 ( Debugging )

In the example above, notice that the class is called “Jollymessage” whereas the filename is called JollyMessage.java.

在上面的示例中,请注意该类称为“ Jollymessage”,而文件名称为JollyMessage.java

Java is case sensitive. The compiler won’t complain because technically there is nothing wrong with the code. It will create a class file that matches the class name exactly (i.e., Jollymessage.class). When you run the program called JollyMessage, you'll receive an error message because there is no file called JollyMessage.class.

Java区分大小写。 编译器不会抱怨,因为从技术上讲代码没有错。 它将创建一个与类名完全匹配的类文件(即Jollymessage.class)。 当您运行名为JollyMessage的程序时,您会收到一条错误消息,因为没有名为JollyMessage.class的文件。

The error you receive when you run a program with the wrong name is:

使用错误的名称运行程序时收到的错误是:

Exception in thread “main” java.lang.NoClassDefFoundError: JollyMessage (wrong name: JollyMessage)..

常见的运行时错误解决方案 ( Common Runtime-Error Solutions )

If your program compiles successfully but fails at execution, review your code for common mistakes:

如果您的程序编译成功,但执行失败,请检查您的代码是否存在常见错误:

  • Mismatched single and double quotes

    单引号和双引号不匹配
  • Missing quotes for strings

    字符串缺少引号
  • Incorrect comparison operators (e.g., not using double equal signs to indicate assignment)

    不正确的比较运算符(例如,不使用双等号表示分配)
  • Referencing objects that don't exist, or don't exist using the capitalization supplied in the code

    使用代码中提供的大写字母引用不存在或不存在的对象
  • Referencing an object that has no properties

    引用没有属性的对象

Working within integrated development environments like Eclipse can help you avoid "typo"-style errors.

在诸如Eclipse之类的集成开发环境中工作可以帮助您避免“错别字”式的错误。

To debug productionalized Java programs, run your Web browser's debugger—you should see a hexadecimal error message that can assist in isolating the specific cause of the problem.

要调试正式生产的Java程序,请运行Web浏览器的调试器-您应该看到一个十六进制错误消息,该消息可以帮助隔离问题的特定原因。

In some situations, the problem may lie not in your code, but in your Java Virtual Machine. If the JVM is choking, it may kick out a runtime error despite the lack of a deficiency in the program's codebase. A browser debugger message will help isolate code-caused from JVM-caused errors.

在某些情况下,问题可能不在于您的代码,而在于您的Java虚拟机。 如果JVM令人窒息,尽管程序的代码库缺乏缺陷,它仍可能引发运行时错误。 浏览器调试器消息将帮助将代码引起的代码与JVM引起的错误隔离。

翻译自: https://www.thoughtco.com/common-runtime-error-2034021

java编译错误和运行错误

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值