2

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!

webish
  • 701
  • 2
  • 9
  • 17

5 Answers5

6

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.

Mauren
  • 1,955
  • 2
  • 18
  • 28
  • What I'm concerned with is why the Class.forName() method has a thrown exception in the first place... – webish Mar 16 '14 at 04:57
  • @webish because when you use `Class.forName()`, you ask the JVM to look inside loaded classes for a name, which is not guaranteed to be loaded at all. `Class.forName()` will never fail during compilation, as the JVM can't know what's going to be loaded when the program starts. By the way, I can't understand why you're loading `ArrayList` with `Class.forName()`, since it belongs to standard library. – Mauren Mar 16 '14 at 04:59
  • 1
    I need more reputation to vote-up. I'm learning java now and in the lessons I'm taking this concept hasn't been introduced yet. I got a little ahead of myself by using a method that "declared a checked exception". Thanks for the answer. – webish Mar 16 '14 at 05:16
1

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
    }
  }
}
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
1

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 {
Hosein Masbough
  • 431
  • 1
  • 4
  • 19
1

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.

Reed Grey
  • 508
  • 6
  • 10
0

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");
      }
    }
    
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 1
    These two examples worked; however, Maureen answered first and I cannot vote-up yet. Thanks for the help! – webish Mar 16 '14 at 05:16