-2

Plz check following code.... class testError has been instantiated but still Class not found exception is generated... If that is true then why statement written in exception handler does not get printed??

class testError
{
  void display()
    {
      System.out.println("This is testError Class");
    }

}

class checkResult
{
  public static void main(String[] args)
     {
        testError te = new testError();
        te.display();// I hope the class has been created

        Class cls = Class.forName("testError"); // will throw ClassNotFound exception
                                                // Why??... Though the class has been 
                                                // instantiated

        // if we try to put it in trycatch block it will work...Why??

         try{ Class cls = Class.forName("testError");}

         catch(ClassNotFoundException e)
            {
              System.out.println("Error found");  //"Error found" will not be printed
                                                  // as the class has been instantiated
            }

      }
   }
Yuan Shing Kong
  • 674
  • 5
  • 15
priyank
  • 11
  • 1
  • 5
  • 1
    Guess this is Java? You need to pass the fully qualified class name. See documentation: http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#forName(java.lang.String) – raphaëλ Aug 16 '14 at 12:29
  • Are these classes really in the default package, or are they in some package you defined? – Mureinik Aug 16 '14 at 12:35
  • 1
    Hi.. This is in default package..and the problem is class has been instantiated ergo will be in class path but class not found exception is thrown away if try to execute the code.... – priyank Aug 16 '14 at 12:56
  • hii rparree....if complete class name had been the cause then error would have also surfaced in try catch block..but there its working fine – priyank Aug 16 '14 at 12:58
  • Does the first `Class.forName("testError");` really throw a CNFException? Or is Java just complaining that you should catch this exception - as you do not catch or throw any exceptions until then? For the second case you probably should read [something about exception types in Java](http://java.about.com/od/Handling-Exceptions/a/Types-Of-Exceptions.htm) and then have a look at the [`Class.forName(...)` method](http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#forName%28java.lang.String,%20boolean,%20java.lang.ClassLoader%29) – Roman Vottner Aug 16 '14 at 14:25

2 Answers2

2

I can't comment - as my reputation is too low, but your code runs and debugs fine - though I had to alter it a little bit to make it compile:

    public static void main(String[] args) throws ClassNotFoundException {
    testError te = new testError();

    Class<?> cls = Class.forName("testError"); 
    try {
        cls = Class.forName("testError");
        // If you got there then everything went fine
        te.display();
    }
    catch (ClassNotFoundException e) {
        System.out.println("Error found"); 
    }
}

How do you run your code (from the command line, in an IDE)? What is the console output? You have got to give more helpful information if you want people to investigate your issue.

Finally, Java convention specifies that classes name should begin with an uppercase character (CheckResult and TestError). Also you should avoid using classes in the default package, as those cannot be imported.

ratiaris
  • 327
  • 2
  • 10
  • hii... ur code will work fine as you have already added throws ClassNotFound exception...My question is ---> Class cls = Class.forName("testError"); is throwing ClassNotFound Exception...if ClassNotFOund fact is true then the print statement in try catch block must be printed on console... – priyank Aug 16 '14 at 13:27
  • ur code will work fine as you have already added throws ClassNotFound exception...My question is --->Class cls = Class.forName("testError"); is throwing ClassNotFound Exception...if ClassNotFOund fact is true then the print statement in try catch block must be printed on console... – priyank Aug 16 '14 at 13:35
  • Do it as..first try to comment the try catch block--> ClassNotFound excption will be thrown away....now comment //Class cls = Class.forName("testError") and uncomment try catch block..this time it will work fine..Why???..if the ClassNotFound exception is there then the control must have gone into try catch block and should have printed "Error found" – priyank Aug 16 '14 at 13:36
  • As (I) expected commenting out the try/catch block didn't change anything, I still runs fines. I invoked `te.display()` at the end of the program, and got `This is testError Class`, no exception thrown... – ratiaris Aug 16 '14 at 14:17
0

First of all Follow java naming convention

Make your main class public

Create some package(not good to create in default packag) like mypackage and put the classes inside them

and try to invoke the method this way

String name = packageName.className.class.getName();//get the name of the class
className o = (className)Class.forName(name)
                               .newInstance();
//will give an instance of type Object so cast it
o.display();// call the method
SparkOn
  • 8,806
  • 4
  • 29
  • 34