package Swing;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface Testable{}
class MyTest
{
@Testable
public static void m1(){}
public static void m2(){}
@Testable
public static void m3()
{
throw new RuntimeException("Boom");
}
public static void m4(){}
@Testable
public static void m5(){}
public static void m6(){}
@Testable
public static void m7()
{
throw new RuntimeException("Crash");
}
public static void m8(){}
}
class TestProcessor
{
public static void process(String clazz)throws ClassNotFoundException
{
int passed=0;
int failed=0;
for(Method m:Class.forName(clazz).getMethods())
{
if(m.isAnnotationPresent(Testable.class))
{
try
{
m.invoke(null);
passed++;
}
catch(Exception ex)
{
System.out.println("方法"+m+ "运行失败,异常为:"+ex.getCause()+"\n");
failed++;
}
}
}
System.out.println("一共运行了:"+(failed+passed)+"个方法,其中\n"+
"失败了:"+failed+"个\n"+
"成功了:"+passed+"个\n");
}
}
public class RunTests
{
public static void main(String[] args)throws Exception
{
TestProcessor.process("MyTest");
}
}
产生的异常如下
Exception in thread "main" java.lang.ClassNotFoundException: MyTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at Swing.TestProcessor.process(RunTests.java:46)
at Swing.RunTests.main(RunTests.java:72)
已经定义了MyTest类为啥还发生java.lang.ClassNotFoundException :MyTest。
答案:1 悬赏:20 手机版
解决时间 2021-02-04 08:42
- 提问者网友:原来太熟悉了会陌生
- 2021-02-03 23:27
最佳答案
- 五星知识达人网友:旧脸谱
- 2021-02-04 00:28
TestProcessor.process("MyTest");
应该是路径问题
改成TestProcessor.process("Swing.MyTest");试试
应该是路径问题
改成TestProcessor.process("Swing.MyTest");试试
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯