老师,可否帮忙分下这个是什么原因导致的?
来源:6-5 谈谈ClassLoader
逐日
2019-06-30
老师好,我参照您课堂上的讲解实现了WaliClassLoader,在执行过程中,出现Exception in thread “main” java.lang.NoClassDefFoundError: WaliClassLoader (wrong name: Wali),已设置JDK版本,确保ide与cmd下JDK一致,调试确认可读取到class文件内容,具体堆栈信息如下,参照网上博客内容,说可能是Wali不符合binary Name命名规则,添加相关package 及相关路径后,依旧存在此问题,具体堆栈如下:
写回答
4回答
-
逐日
提问者
2019-07-02
相关Loader加载目标类
public class Wali {
public void sayHello(){
System.out.println("this is wali,hello");
}
}
00 -
逐日
提问者
2019-07-02
public class ClassLorderLearn { public static void main(String[] args) throws Exception { ClassLoaderOfWali waliClassLoader = new ClassLoaderOfWali("C:/Users/10796/Desktop/classLoadTest/","WaliClassLoader"); Class waliClazz = waliClassLoader.loadClass("Wali"); Object wali = waliClazz.newInstance(); Method sayHello = waliClazz.getMethod("sayHello"); sayHello.invoke(wali); // ClassLoader loader = ClassLorderLearn.class.getClassLoader(); System.out.println(waliClassLoader); System.out.println(waliClassLoader.getParent()); System.out.println(waliClassLoader.getParent().getParent()); System.out.println(); } }
00 -
逐日
提问者
2019-07-02
public class ClassLoaderOfWali extends ClassLoader { private String path; private String name; public ClassLoaderOfWali(String path,String name){ this.name = name; this.path = path; } @Override protected Class<?> findClass(String className) throws ClassNotFoundException { byte[] b = loadClassData(className); return defineClass(name,b,0,b.length); } protected byte[] loadClassData(String className) { className = path + className + ".class"; File file = new File(className); System.out.println(file.exists()); InputStream inputStream = null; ByteArrayOutputStream outputStream = null; try { inputStream = new FileInputStream(className); outputStream = new ByteArrayOutputStream(); int temp; while ((temp = inputStream.read()) != -1) { outputStream.write(temp); } } catch (Exception e) { e.printStackTrace(); } finally { try { inputStream.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } return outputStream.toByteArray(); } }
00 -
翔仔
2019-07-02
同学好,请问你相关的代码是怎样的?光凭这样的信息我这边没办法定位呢,不好意思这几天忙着带小孩看病,晚上照顾没太能看上
052019-07-03
相似问题