简体   繁体   English

在线程“ main” java.lang.NoClassDefFoundError中使用程序包“ Exception”时出现奇怪的错误。 ”

[英]I get strange error while using package“ Exception in thread ”main“ java.lang.NoClassDefFoundError. ”

I get "Exception in thread "main" java.lang.NoClassDefFoundError" while using "package" 我在使用“包”时收到“线程“主”中的异常java.lang.NoClassDefFoundError”

  1. if I comment "//package com.kevin.corejava; " it would run successfully; 如果我注释“ // package com.kevin.corejava;”,它将成功运行;
  2. Or, I delete the "main" method in "Employee.java", and add a class "Test.java" in folder "E:\\Space\\java" which imports "Employee.java" (import com.kevin.corejava.*), and reference Employee int its main method, it would still run successfully. 或者,我删除“ Employee.java”中的“ main”方法,并在文件夹“ E:\\ Space \\ java”中添加一个类“ Test.java”,该类会导入“ Employee.java”(导入com.kevin.corejava。 *),并以其主要方法引用Employee,它仍将成功运行。

Why the "main" method in Employee could not run by itself when I add the sentence "package com.kevin.corejava" (Employee.java is put in path E:\\Space\\java\\com\\kevin\\corejava\\Employee.java) 为什么当我添加句子“ package com.kevin.corejava”(Employee.java放在路径E:\\ Space \\ java \\ com \\ kevin \\ corejava \\ Employee.java中时,Employee中的“ main”方法本身无法运行) )

package com.kevin.corejava;
import java.util.*;
import static java.lang.System.*;

public class Employee
{
    public Employee(String n, double s, int year, int month, int day)
    {
        name = n;
        salary = s;
        GregorianCalendar calendar = new GregorianCalendar(year, month-1,day);
        hireDay = calendar.getTime();
    }

    public String getName()
    {
        return name;
    }

    public double getSalary()
    {
        return salary;
    }
    public Date getHireDay()
    {
        return hireDay;
    }
    public void raiseSalary(double byPercent)
    {
        double raise = salary *byPercent/100;
        salary+=raise;
    }

    private String name;
    private double salary;
    private Date hireDay;


    public static void main(String[] args)
    {
        System.out.println("This is main ,I won't say 'Hello World'!");
    }

}

Just make sure you follow the below things : 只要确保您遵循以下事项:

  1. "package com.kevin.corejava;" “包com.kevin.corejava;” It should be a valid path. 它应该是有效路径。

  2. Your package path must be executed by JDK, and JRE must access its class file. 您的包路径必须由JDK执行,并且JRE必须访问其类文件。 (Make Sure your JDK and JRE paths are right to access this file). (确保您的JDK和JRE路径可以访问此文件)。

  3. The class name must be same as the file name as the main method is in your Employee class. 类名必须与文件名相同,因为Employee类中的主要方法是。

  4. Your project's Build path is right. 您的项目的构建路径是正确的。 If you use Eclipse or other IDE, just right click and configure your build path. 如果您使用Eclipse或其他IDE,只需右键单击并配置您的构建路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“ main”中的异常java.lang.NoClassDefFoundError。 不执行而是编译 - Exception in thread “main” java.lang.NoClassDefFoundError. not executing but compiling 为什么会出现“线程“main”中的异常 java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException”错误? - Why do I get "Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException" error? 线程“main”中的异常java.lang.NoClassDefFoundError:运行包 - Exception in thread “main” java.lang.NoClassDefFoundError : Running a package 包中的类 - 线程“main”中的异常 java.lang.NoClassDefFoundError - Class in a package - Exception in thread "main" java.lang.NoClassDefFoundError java 错误:线程“main”中的异常 java.lang.NoClassDefFoundError - java error: Exception in thread "main" java.lang.NoClassDefFoundError 在线程“main”java.lang.NoClassDefFoundError中获取错误异常: - Getting error Exception in thread “main” java.lang.NoClassDefFoundError: JNI 错误:线程“主”java.lang.NoClassDefFoundError 中的异常 - JNI error: Exception in thread “main” java.lang.NoClassDefFoundError 线程“ main”中的异常java.lang.NoClassDefFoundError启动错误 - Exception in thread “main” java.lang.NoClassDefFoundError launch error 线程“main”java.lang.NoClassDefFoundError中的异常 - Exception in thread “main” java.lang.NoClassDefFoundError 线程“ main” java.lang.NoClassDefFoundError中的异常: - exception in thread“main”java.lang.NoClassDefFoundError:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM