1

when running project from jar.the java.lang.Noclassdeffounderror occur.

ex: 1-class contain main method:

package p;

import p1.MyClass1;

import p2.MyClass2;

public class TestClass {

public int i;
public Object obj;  //Line 5

public void aMethod(){

// Object obj=null; //Line 7

    if(i==0){
        obj=new MyClass1();
        System.out.println("if block: p1.MyClass1");
    }else{
        obj=new MyClass2();
        System.out.println("else block: p2.MyClass2");
    }
}

public static void main(String [] args){
    TestClass t=new TestClass();
    t.aMethod();
}

}

2: MyClass1 of p1 package:

package p1;

public class MyClass1 {

public MyClass1() {}

}

3- MyClass2 of p2 package:

package p2;

public class MyClass2 {

public MyClass2() {}

}


in above example 3 classes are TestClass,MyClass1 and MyClass2. when i run the above project after build(TestClass is main class of my project) using jar and manually deleating p2 package it run fine. but after chenge in TestClass making comment to global variable public Object obj(Line 5) and remove the comment local variable Object obj in method aMethod of TestClass(Line 7). then i build and run project from jar after manually deleating p2 package,console shows Exception in thread "main" java.lang.NoClassDefFoundError: p2/MyClass2

at run time no need of p2 package but making the variable "obj" in class TestClass

to global to local(in method aMethod()) exception occur. Please sort my problem why this giving error.

vikas singh
  • 131
  • 1
  • 2
  • 10

0 Answers0