Question

I've found other thread where people had and solved this error; however, all of were NOT using fully qualified class paths. I cannot seem to get Class.forName to work and I am using fully qualified paths.

I've tested compiling from CLI and using ItelliJ Idea. Both fail with the same error.

Code (in test directory):

package test;
public class Test {
  public static void main(String[] args) {
    Class cls = Class.forName("java.util.ArrayList");
  }
}

Error:

java.lang.ClassNotFoundException; must be caught or declared to be thrown

The example above does NOT work. Thanks in advance!

Was it helpful?

Solution

You're getting this message because ClassNotFoundException is a checked exception. This means that this exception can not be ignored. You need to either surround it with a try/catch construct and provide exception handling or add a throws clause to your method and handle it in a callee.

EDIT: Please note that Class.forName() construct is not resolved during compilation. When you write such a statement, you're telling the JVM to look during program execution for a class which may not have been loaded. Java libraries are dynamically linked instead of statically linked, meaning that their code is not incorporated in your program code, being loaded only when requested. This is why it throws ClassNotFoundException.

OTHER TIPS

Class.forName("java.util.ArrayList") method declared to throw a checked exception ClassNotFoundException, so you must handle it inside a try/catch block like following

package test;
public class Test {
  public static void main(String[] args) {
    try {
        Class cls = Class.forName("java.util.ArrayList");
    } catch (ClassNotFoundException e) {
    // Handle it here
    }
  }
}

Try this:

try{
  Class cls = Class.forName("java.util.ArrayList");
}catch(ClassNotFoundException e){
  ... do messaging or logging
}

or throw ClassNotFoundException in the methods signature:

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

I don't know why none of the answers above can explain why this error happened. Neither can I, but I once encountered this issue and solved it by using

Thread.currentThread().getContextClassLoader().loadClass($YOUR_CLASS_NAME)

Hope it can solve your problem and hope someone can give us an explanation.

There are two options:

  1. Surround with a try-catch block and do some appropriate handling:

    package test;
    public class Test {
      public static void main(String[] args) {
        try {
            Class cls = Class.forName("java.util.ArrayList");
        } catch(ClassNotFoundException e)  {
            System.out.println("This is not a very serious exception, or at least Test knows how to handle it");
        }
      }
    }
    
  2. Throw it outside the method in the hope that some calling method will know what to do.

    package test;
    public class Test {
      public static void main(String[] args) throws ClassNotFoundException {
        Class cls = Class.forName("java.util.ArrayList");
      }
    }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top