jayjaypg22
jayjaypg22

Reputation: 1751

class.forName, looking for a class in a different project, throws ClassNotFoundException

In eclipse I've got 2 different project : ProjectX, generic and projectA specific.

In projectX :

 public static final Plugin getPlugin(String sIdentifier ) {
     String sPluginClassName = "packageName.blabla.ClassName";
     try {
                pluginClass = Class.forName(sClassName);
    ...
    } catch (ClassNotFoundException e) {
                throw new BusinessException(sIdentifier, e);
    }
}

in projectA :

package packageName.blabla;

public class ClassName () {
 ....
}

During execution I get a BusinessException due to ClassNotFoundException. The class path is ok (copy/pasted not written). In eclipse projectA java build path / libraries contains projectX folder, projectX build path hasn't got any reference to projectA (seems legit).

This works fine in a projectB specific interaction with ProjectX (I don't know where are the differences : I've checked both build path, it's done the same way).

Any idea?

Upvotes: 3

Views: 1624

Answers (1)

Stephen C
Stephen C

Reputation: 719249

projectX build path hasn't got any reference to projectA (seems legit).

While you don't need a build-time dependency of projectX on projectA, the "bin" directory for projectA needs to be on the execution classpath for projectX. If it isn't, then the call to Class.forName in getPlugin won't look at the "projectA/bin" directory, and won't find the class that it is trying to load.


The execution classpath can be configured via the "Run > Run Configurations" window.

  • Select the run config for your application.
  • Go to the Classpath panel
  • Select User Classpath
  • Click on "Add Projects" and select the project you want to add.
  • Apply the configuration change.

Upvotes: 4

Related Questions