Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » NoClassDefFoundError when calling Class.forName(Seeing a NoClassDefFoundError when I call Class.forName in code when running in eclipse only (i.e don't see it when run from command line).)
NoClassDefFoundError when calling Class.forName [message #1791702] Wed, 04 July 2018 11:21 Go to next message
Alex Clayton is currently offline Alex ClaytonFriend
Messages: 8
Registered: February 2015
Junior Member
Got a series of projects where code in one project uses Class.forName to load a class from another Project, which in turn makes use of a class in a third project. When I run the code in eclipse using a Java 10 JRE I get a NoClassDefFoundError but I do not see this when running (with the same JRE) at the command line. Neither do I see it when running against java 8 in eclipse.

The smallest set up I could do to replicate it was with 3 project, Project 1, Project 2 and Project 3. I've set eclipses compiler compliance level to 1.8 but did see the problem when this was 10 too.

Project 1 contains the following class:


public class Foo {
	public static void setFooInterface(FooInterface newInteface) {
		// Do something
	public interface FooInterface {
		public void callback();

Then Project 2 has Project 1 on its class path, and contains the following class:


public class Bar {
	public static void doAFoo() {
		Foo.setFooInterface(new FooInterface() {
			public void callback() {
				// Some Code

Finally Project 3 contains Project 2 only (i.e it does not include Project 1) on it's classpath. It has the following code in it:


public class BuzzApp {
	public static void main(String[] args) {
               try {
		catch (ClassNotFoundException | SecurityException 
				| IllegalArgumentException | NoClassDefFoundError e) {
			System.out.println("ERROR "+e.getLocalizedMessage());

When I run this code in eclipse with the oracle java 10.0.1 JRE I get the following error:

ERROR clayton/alex/foo/Foo$FooInterface
java.lang.NoClassDefFoundError: clayton/alex/foo/Foo$FooInterface
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(
Caused by: java.lang.ClassNotFoundException:$FooInterface
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(
	at java.base/java.lang.ClassLoader.loadClass(
	... 3 more

If instead I run using Java 1.8.0_52 then I see not such error. The program just runs to completion.

Also if Instead compile and generate jar files using ant; using similar project hierachy (i.e project 2 has project 1 on it's class path and project 3 has project 2 only on its class path) then again then the code completes successfully, even with the same Java 10 JRE.

Any idea why this would be happening? Why do I only see the error when running in eclipse, and when running with Java 10 (I think it can be seen with Java 9 too, but have not tested that fully)?
Re: NoClassDefFoundError when calling Class.forName [message #1791713 is a reply to message #1791702] Wed, 04 July 2018 12:45 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 29650
Registered: July 2009
Senior Member
Likely it's better to post on the JDT forum:
Re: NoClassDefFoundError when calling Class.forName [message #1791752 is a reply to message #1791713] Wed, 04 July 2018 19:42 Go to previous message
Nitin Dahyabhai is currently offline Nitin DahyabhaiFriend
Messages: 3297
Registered: July 2009
Senior Member

Project 2 needs to "export" the Project 1 entry of its Build Path from the Order and Export tab.

Nitin Dahyabhai
Eclipse Web Tools Platform
Previous Topic:sdk layoutlib version is not supported in eclipse
Next Topic:Eclipse not running Activator from latest installed plugin.
Goto Forum:

Current Time: Wed Nov 14 13:03:18 GMT 2018

Powered by FUDForum. Page generated in 0.03866 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top