任何想法为什么我会收到此错误? (是的,我查找了错误,但仍然没有找到解决方法)

我的错误:

Exception in thread "main" java.lang.ClassFormatError: Truncated class file
 at java.lang.ClassLoader.defineClass1(Native Method)
 at java.lang.ClassLoader.defineClass(Unknown Source)
 at java.lang.ClassLoader.defineClass(Unknown Source)
 at org.fellixombc.mysql.util.MysqlClassLoader.findClass(MysqlClassLoader.java:22)
 at org.fellixombc.mysql.util.MysqlClassLoader.loadClass(MysqlClassLoader.java:14)
 at org.fellixombc.mysql.Main.main(Main.java:9)


档案:

Main.java

package org.fellixombc.mysql;

import org.fellixombc.mysql.util.MysqlClassLoader;

public class Main {
    public static void main(String[] args) {
        MysqlClassLoader mcl = new MysqlClassLoader();
        try {
            mcl.loadClass("org.fellixombc.mysql.net.Client");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}


Client.java:

package org.fellixombc.mysql.net;

public class Client {
    public Client() {
        System.out.println("Hello!");
    }
}


MysqlClassLoder.java:

package org.fellixombc.mysql.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class MysqlClassLoader extends ClassLoader {
    public MysqlClassLoader() {
        super(MysqlClassLoader.class.getClassLoader());
    }

    @Override
    public Class<?> loadClass(String className) throws ClassNotFoundException {
        return findClass(className);
    }

    @Override
    public Class<?> findClass(String className) throws ClassNotFoundException {
        byte[] b = null;
        try {
            b = loadClassData(className);
            Class c = defineClass(className, b, 0, b.length);
            if(c != null)
                return c;
            return super.findClass(className);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private byte[] loadClassData(String className) throws IOException {
        int size = className.length();
        byte buff[] = new byte[size];

        // Open the file
        FileInputStream fis = new FileInputStream("bin/" + className.replace('.', File.separatorChar) + ".class");
        fis.available();
        fis.read(buff);
        fis.close();

        return buff;
    }
}

最佳答案

是的,您最多读取的字节数等于文件名中的字符数。相反,您需要读取整个文件。这是一种方法,按照您的建议使用readFully。

File f = new File("bin/" + className.replace('.', File.separatorChar) + ".class");
DataInputStream is = new DataInputStream(new FileInputStream(f));
int len = (int)f.length();
byte[] buff = new byte[len];
is.readFully(buff);
is.close();
return buff;




由于您不处理诸如Object之类的内置类,因此我认为您需要从findClass中的loadClassData捕获FileNotFoundException,然后调用super.findClass。例如。:

try {
  try {
    b = loadClassData(className);
  }
  catch(FileNotFoundException fnf) {
    return super.findClass(className);
  }
  Class c = defineClass(className, b, 0, b.length);
  if(c != null)
    return c;
  return super.findClass(className);
} catch (IOException e) {
  e.printStackTrace();
}
return null;

10-08 08:39