7 个解决方案
#1
#2
如果是.class文件,那么该文件可能损坏了。也可能读取的时候出问题了,正常的魔数是OXCAFEBABE
#3
class MyCLassLoader extends ClassLoader{
private int key;
public MyCLassLoader(int key){
this.key = key;
}
protected Class<?> findClass(String name){
byte[] classBytes = null;
try {
classBytes = loadClassBytes(name);
} catch (Exception e) {
e.printStackTrace();
}
Class<?> c = defineClass(name, classBytes, 0, classBytes.length);
if(c == null)
try {
throw new ClassNotFoundException(name);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return c;
}
private byte[] loadClassBytes(String name) throws Exception {
// TODO Auto-generated method stub
String cname = name.replace('.', '/') + ".MY";
FileInputStream in = null;
in = new FileInputStream(cname);
ByteArrayOutputStream butter = new ByteArrayOutputStream();
int ch;
while ( (ch = in.read()) != -1 ) {
ch = (byte)(ch-key);
butter.write(ch);
}
return butter.toByteArray();
}
}
public class ClassLoaderTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyCLassLoader mc = new MyCLassLoader(3);
Class<?> c = mc.findClass("src.ClassLoader.ClassTestEx");
}
}
这个.MY后缀是一个.class文件通过key转换过来的,为什么还是会抛出java.lang.NoClassDefFoundError: src/ClassLoader/ClassTestEx (wrong name: ClassLoader/ClassTestEx)异常
#4
有人能帮我解决下吗。。。
#5
确定package名真的是以src开头的么
#6
我改了点,现在可以获取类了,但是拿到的类有点奇怪。。。不能调用任何东西,连newInstance()也报错。。
java.lang.IllegalAccessException: Class ClassLoader.ClassLoaderTest can not access a member of class ClassLoader.ClassTestEx with modifiers ""
#7
我调试了下,获得的class里面Methods,Field属性都是null,这是什么情况。。求指点
#1
#2
如果是.class文件,那么该文件可能损坏了。也可能读取的时候出问题了,正常的魔数是OXCAFEBABE
#3
class MyCLassLoader extends ClassLoader{
private int key;
public MyCLassLoader(int key){
this.key = key;
}
protected Class<?> findClass(String name){
byte[] classBytes = null;
try {
classBytes = loadClassBytes(name);
} catch (Exception e) {
e.printStackTrace();
}
Class<?> c = defineClass(name, classBytes, 0, classBytes.length);
if(c == null)
try {
throw new ClassNotFoundException(name);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return c;
}
private byte[] loadClassBytes(String name) throws Exception {
// TODO Auto-generated method stub
String cname = name.replace('.', '/') + ".MY";
FileInputStream in = null;
in = new FileInputStream(cname);
ByteArrayOutputStream butter = new ByteArrayOutputStream();
int ch;
while ( (ch = in.read()) != -1 ) {
ch = (byte)(ch-key);
butter.write(ch);
}
return butter.toByteArray();
}
}
public class ClassLoaderTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyCLassLoader mc = new MyCLassLoader(3);
Class<?> c = mc.findClass("src.ClassLoader.ClassTestEx");
}
}
这个.MY后缀是一个.class文件通过key转换过来的,为什么还是会抛出java.lang.NoClassDefFoundError: src/ClassLoader/ClassTestEx (wrong name: ClassLoader/ClassTestEx)异常
#4
有人能帮我解决下吗。。。
#5
确定package名真的是以src开头的么
#6
我改了点,现在可以获取类了,但是拿到的类有点奇怪。。。不能调用任何东西,连newInstance()也报错。。
java.lang.IllegalAccessException: Class ClassLoader.ClassLoaderTest can not access a member of class ClassLoader.ClassTestEx with modifiers ""
#7
我调试了下,获得的class里面Methods,Field属性都是null,这是什么情况。。求指点