0

I am a beginner in java. When a try to run this program with the arguments "Media 1" (In NetBens) I have the following message. The name of the file is Media.java. Can anyone help me?

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown at Media.main(Media.java:23) Java Result: 1

import java.lang.reflect.Method;

 public class Media {

public boolean test1(String s) {
    System.out.println(s);
    return true;
}

public int test2(String s) {
    return 0;
}

public boolean test3(String s) {
    return true;
}

public static void main(String... args) {

    Class<?> c = Class.forName(args[0]);
    Object t = c.newInstance();

    Method[] allMethods = c.getDeclaredMethods();
    for (Method m : allMethods) {
        String mname = m.getName();
        if (!mname.equals("main")) {
            System.out.println("involking" + mname);
            Object o = m.invoke(t, args[1]);
            System.out.println("return value " + o.toString());
        }
    }

}

}
2
  • 1
    The Class.forName method is declared to throw ClassNotFoundException. You're not catching that or declaring that your method might throw it. See docs.oracle.com/javase/tutorial/essential/exceptions Commented Apr 28, 2014 at 14:53
  • Also, it's a good idea to get your code to compile cleanly before you try to run it. I would expect NetBeans to prompt you when you try to run this code, basically saying "Hey, this didn't compile properly - do you really want to run something that is broken?" The answer to that question should almost always be "no". Commented Apr 28, 2014 at 14:54

2 Answers 2

1

surround your code with try catch like below as methods you are calling throws exception so you have to declare or handle it

try {
    Class<?> c = Class.forName(args[0]);
    Object t = c.newInstance();

    Method[] allMethods = c.getDeclaredMethods();
    for (Method m : allMethods) {
        String mname = m.getName();
        if (!mname.equals("main")) {
            System.out.println("involking" + mname);
            Object o = m.invoke(t, args[1]);
            System.out.println("return value " + o.toString());
        }
    }
}  catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (InvocationTargetException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

Comments

0

...

public static void main(String... args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

...

or use try/catch blocks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.