Mike
Mike

Reputation: 882

Class.forName() - JDK7 - ClassNotFoundException

I can't get this to work!

I found lots of those problems on stackoverflow but 90% of the problems were related to the missing package name, but I don't have any package, it's "unnamed" therefore must not be defined within forName() parameter.

A class that is not in a named package is in an unnamed package. Thus the full class name is "Main". Such classes cannot be used from a named package, except via reflection.

The other solution was specifying classpath, however src (blue folder) IS a classpath as said here

You can right click on any directory in your IntelliJ project, select "Mark Directory As...", and choose "Source Root". That director folder will change color from yellow to blue; IntelliJ considers all those directories to be in your CLASSPATH.

enter image description here

Yet it's still not working! It slowly drives me nuts.

Upvotes: 0

Views: 146

Answers (1)

longhua
longhua

Reputation: 4262

This message means that you need to handle the checked exception, that is, java.lang.ClassNotFoundException. It doesn't mean you couldn't find/load this class.

In order to handle checked exception, you can

  1. Wrap the codes with try {...} catch (ClassNotFoundException ex) {...}.
  2. Add it to throw declaration of main method: main(String[] args) throws ClassNotFoundException.

In IDEA, you can always use alt + enter to see suggestions. enter image description here

Upvotes: 2

Related Questions