7

In a Java application I'm trying to create the instance of the class Class1 by using newInstance() method on the class. Class1 belongs to my project, but it also needs Class2 which is from another project and which is located in an external jar which I have added to the project and to the build path in Eclipse. Eclipse seems to find Class2 as it compiles the project without any problem, but when I try to get the instance of Class1 it throws a ClassNotFoundException about Class2.

This is how Class1 looks like:

import other.package.Class2;

public class Class1 implements Class1Interface{
    //there's no explicit constructor

    @Override
    public void method1(String param){
        System.out.println("Loading.....");
    }   

    @Override
    public void notifyChanges(String param) throws Exception{
        Class2 class2 = Class2.getInstance(); //here it's used the Class2 from another jar
        ... 
    }
}

and this is how I try to get the instance of it:

String myParam = "blabla";
Class1Interface interf = "my.package.Class1";
try {
    interf = (Class1Interface) Class.forName(interfazws).newInstance();
    interf.method1(myParam);
} catch (InstantiationException e) {
    handle(e);
} catch (IllegalAccessException e) {
    handle(e);
} catch (ClassNotFoundException e) {
    handle(e);
}

And when I execute that I get:

Caused by: org.springframework.scheduling.quartz.JobMethodInvocationFailedException: Invocation of method 'execute' on target class [class my.package.myJobJob] failed; nested exception is java.lang.NoClassDefFoundError: other/package/Class2            
...
Caused by: java.lang.NoClassDefFoundError: other/package/Class2
...
Caused by: java.lang.ClassNotFoundException: other.package.Class2

Another thing to add about my project configuration is that under WEB-INF folder I have a folder called libs with some jars and I created another lib folder called libs_other_company and I have placed there the jar where Class2 is in. If I go to the java build path I can see the jar added in the libraries tab.

I am running the application in a Tomcat server directly from Eclipse (I'm not using anything like Ant or Maven).

How can it be possible that it finds the class at compile time but not at runtime? Any idea to make it works?

Thanks.

Javi
  • 19,387
  • 30
  • 102
  • 135

1 Answers1

6

You confuse two separate things, compile time class path resolution and run time class path access.

  1. When you compile your application, you need to have classes that your application uses/refers to in your development environment compile class path; if you don't (do that) then a compile time error will occur.

  2. When you run your compiled application, you need to have all classes that your application uses/refers to in your JVM class path or included in your application package; if you don't (do that) then a run time error will occur.

You experience the second.

spenibus
  • 4,339
  • 11
  • 26
  • 35
Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
  • 3
    I know that this is the problem. I don't understand why eclipse is not taking the jar to the runtime environment. – Javi Nov 23 '12 at 12:59
  • 1
    Because it does not do it by default, you have to "tell" it which classes/jar to include, otherwise each application would have to contain (nearly) all classes. Only **you** will know what kind of environment you intend to run your application in. For this reason I prefer to use ant build files to package the apps. – Germann Arlington Nov 23 '12 at 13:03
  • so is there any way to configure that in Eclipse or do I have to build an Ant script for that? – Javi Nov 23 '12 at 13:35
  • When you `export` a jar file in eclipse you can specify (last or one before last screen) what you want to include in the jar. It can generate an ant script for you that you can edit later. – Germann Arlington Nov 23 '12 at 13:45
  • I'm not exporting any jar. I's a dynamic web project. I can generate in any case a WAR. – Javi Nov 23 '12 at 13:56
  • Sorry, don't remember if you can export `war` file. If not, than build file is a way to go – Germann Arlington Nov 23 '12 at 14:12