Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't load inner class? ClassNotFoundException

Why does the following code cause ClassNotFoundException?

public class App02 {

    public static class A {
    }

    public static void main(String[] args) throws ClassNotFoundException {

        try {
            System.out.println("A.class.getCanonicalName() = " + A.class.getCanonicalName());
            Class c = Class.forName("tests.App02.A"); //error on this line
            System.out.println(c.getName());
        }

        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Output:

A.class.getCanonicalName() = tests.App02.A
java.lang.ClassNotFoundException: tests.App02.A
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:190)
    at tests.App02.main(App02.java:15)
like image 267
Suzan Cioc Avatar asked Jan 27 '14 19:01

Suzan Cioc


People also ask

What is the reason for ClassNotFoundException?

ClassNotFoundException is a checked exception which occurs when an application tries to load a class through its fully-qualified name and can not find its definition on the classpath. This occurs mainly when trying to load classes using Class. forName(), ClassLoader. loadClass() or ClassLoader.

How do I add an inner class?

Creating an inner class is quite simple. You just need to write a class within a class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class.

How do you initialize an inner class?

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass outerObject = new OuterClass(); OuterClass. InnerClass innerObject = outerObject.


4 Answers

Try Class c = Class.forName("tests.App02$A"). It's not a top-level class, so use $ to locate it.

like image 188
Paul Hicks Avatar answered Oct 16 '22 16:10

Paul Hicks


You need to use $ to access the nested class:

Class c = Class.forName("tests.App02$A");

When you compile your class, you will notice that the nested class is named as: App02$A.class, under package tests. It would make more sense then.

like image 27
Rohit Jain Avatar answered Oct 16 '22 18:10

Rohit Jain


Because you are using a canonical name, but you should use name (A.class.getName()).

In your case you should use Class c = Class.forName("tests.App02$A");

like image 4
Philip Voronov Avatar answered Oct 16 '22 18:10

Philip Voronov


There is a helpful util in commons-lang which support these classes:

org.apache.commons.lang3.ClassUtils.get("tests.App02.A")
like image 2
Inshua Avatar answered Oct 16 '22 17:10

Inshua