java主线程和子线程区别_主线程异常– Java

java主线程和子线程区别

Being a Java Programmer, you must have seen exception in thread main sometimes while running a java program.

作为Java程序员,您有时必须在运行Java程序时exception in thread main看到exception in thread main

线程主异常 (Exception in thread main)

If you are using Java IDE like Eclipse or Netbeans to run a java program, you might not face some of these issues because IDE takes care of running the class with proper syntax and correct command.

如果使用诸如Eclipse或Netbeans之类的Java IDE来运行Java程序,则可能不会遇到这些问题,因为IDE会使用正确的语法和正确的命令来运行类。

Here I am explaining some common java exception in thread main exceptions you will see while running a java program from the terminal.

在这里,我正在解释线程主要异常中的一些常见Java异常,您将从终端运行Java程序时会看到这些异常。

1.线程主java.lang.UnsupportedClassVersionError中的异常 (1. Exception in thread main java.lang.UnsupportedClassVersionError)

This exception comes when your java class is compiled from another JDK version and you are trying to run it from another java version. Let’s understand this with a simple example.

当您的Java类是从另一个JDK版本编译而您正试图从另一个Java版本运行它时,就会出现此异常。 让我们用一个简单的例子来理解这一点。

package com.journaldev.util;

public class ExceptionInMain {

	public static void main() {
		System.out.println(10);
	}

}

When I created the project in Eclipse, I kept JRE version as Java 7 but in my terminal java version is Java 1.6. Because of Eclipse IDE JDK settings, the class file generated is compiled with Java 1.7.
Now when I try to run this class from the terminal, I get the following exception message.

在Eclipse中创建项目时,我将JRE版本保留为Java 7但在终端Java版本中保留为Java 1.6 。 由于具有Eclipse IDE JDK设置,因此生成的类文件是使用Java 1.7编译的。
现在,当我尝试从终端运行此类时,我收到以下异常消息。

pankaj@Pankaj:~/Java7Features/bin$java com/journaldev/util/ExceptionInMain
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/journaldev/util/ExceptionInMain : Unsupported major.minor version 51.0
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
	at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

If I run the class with Java 1.7, I won’t get this exception. The reason for this exception is that we can’t compile a java source file from higher version and then run it on lower version of JRE.

如果我使用Java 1.7运行该类,则不会出现此异常。 发生此异常的原因是,我们无法从较高版本编译Java源文件,然后在较低版本的JRE上运行它。

2.线程主java.lang.NoClassDefFoundError中的异常 (2. Exception in thread main java.lang.NoClassDefFoundError)

There are two variants of this exception. The first one is where you provide the class full name, remember that when running a Java Program, you just need to give the class name and not the extension.

此异常有两种变体。 第一个是提供类全名的位置,请记住,在运行Java程序时,只需提供类名而不是扩展名即可。

Notice that the .class in the command below to run the program causes NoClassDefFoundError. The reason for this error is when java is unable to find the class file to execute.

请注意,以下用于运行程序的命令中的.class导致NoClassDefFoundError 。 发生此错误的原因是java无法找到要执行的类文件。

pankaj@Pankaj:~/CODE/Java7Features/bin$java com/journaldev/util/ExceptionInMain.class
Exception in thread "main" java.lang.NoClassDefFoundError: com/journaldev/util/ExceptionInMain/class
Caused by: java.lang.ClassNotFoundException: com.journaldev.util.ExceptionInMain.class
	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

The second type of exception is thrown when Class is not found.

当找不到Class时,抛出第二种异常。

pankaj@Pankajs-MacBook-Pro:~/CODE/Java7Features/bin/com/journaldev/util$java ExceptionInMain
Exception in thread "main" java.lang.NoClassDefFoundError: ExceptionInMain (wrong name: com/journaldev/util/ExceptionInMain)
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
	at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)

Note that ExceptionInMain class is in the package com.journaldev.util, so when Eclipse compiles this class, it’s placed inside <Project_Classes_Directory>/com/journaldev/util and hence class is not found causing this error message.

请注意, ExceptionInMain类位于com.journaldev.util包中,因此,当Eclipse编译此类时,它将放置在<Project_Classes_Directory>/com/journaldev/util ,因此未找到导致此错误消息的类。

3.线程main java.lang.NoSuchMethodError中的异常:main (3. Exception in thread main java.lang.NoSuchMethodError: main)

This exception comes when you are trying to run a class that doesn’t have main method. In Java 7, the error message is changed to make it more clear.

当您尝试运行没有main方法的类时,会出现此异常。 在Java 7中,错误消息已更改为更加清晰。

pankaj@Pankaj:~/CODE/Java7Features/bin$ java com/journaldev/util/ExceptionInMain
Error: Main method not found in class com.journaldev.util.ExceptionInMain, please define the main method as:
   public static void main(String[] args)

Read more at java.lang.NoSuchMethodError

java.lang.NoSuchMethodError阅读更多

4.线程“ main”中的异常java.lang.ArithmeticException (4. Exception in thread “main” java.lang.ArithmeticException)

Whenever an exception is thrown from the main method, it prints the exception in the console. The first part explains that exception is thrown from the main method, second part prints the exception class name and then after a colon, it prints the exception message.

每当从main方法引发异常时,它都会在控制台中打印异常。 第一部分解释了从main方法抛出异常的情况,第二部分显示了异常类的名称,然后在冒号后面打印了异常消息。

For example, if I change the initial class print statement to System.out.println(10/0);, it will throw ArithmeticException.

例如,如果我将初始类print语句更改为System.out.println(10/0); ,它将抛出ArithmeticException

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at com.journaldev.util.ExceptionInMain.main(ExceptionInMain.java:6)

如何修复线程主中的Java异常? (How to fix java exception in thread main?)

Above are some of the common java exceptions in thread main, whenever you face any one of these check following:

上面是线程main中的一些常见Java异常,每当您遇到以下任何一项检查时,便会发生以下情况:

  1. Same JRE version is used to compile and run the java program

    使用相同的JRE版本来编译和运行Java程序
  2. You are running java class from the classes directory and package is provided as directory.

    您正在从classes目录运行Java类,并且软件包作为目录提供。
  3. Your java classpath is set properly to include all the dependency classes

    您的Java类路径已正确设置为包括所有依赖项类
  4. You are using only file name without .class extension while running a java program

    您在运行Java程序时仅使用不带.class扩展名的文件名
  5. Java class main method syntax is correct

    Java类主方法语法正确

Further Reading: Exception Handling in Java.

进一步阅读: Java中的异常处理

Did you noticed that in all the above exceptions, it’s mentioned as “thread main”. It’s because java main method is the first thread in java. Read more at multithreading in java.

您是否注意到上述所有例外情况都被称为“线程主”。 这是因为java main方法java中的第一个线程在Java多线程中阅读更多内容。

翻译自: https://www.journaldev.com/611/exception-in-thread-main-java

java主线程和子线程区别

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值