我是java的初学者。当尝试使用参数“Media 1”(在NetBens中)运行此程序时,我有以下消息。该文件的名称是Media.java。谁能帮我?
线程“main”中的异常java.lang.RuntimeException:无法编译的源代码 - 未报告的异常java.lang.ClassNotFoundException;必须被抓住或宣布被抛出 在Media.main(Media.java:23) Java结果: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());
}
}
}
}
答案 0 :(得分:1)
使用下面的try catch包围您的代码作为您调用的方法抛出异常,因此您必须声明或处理它
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();
}
答案 1 :(得分:0)
...
public static void main(String ... args)抛出ClassNotFoundException,InstantiationException,IllegalAccessException,IllegalArgumentException,InvocationTargetException {
...
或使用try / catch块