线程“main”中的异常java.lang.NoClassDefFoundError:java / lang / Object

时间:2016-04-03 19:44:30

标签: java localhost classloader noclassdeffounderror

所以我有自己的类加载器类ServerLoader,它能够从本地服务器加载具有路径http://localhost/app/1/的类,其中存在一些.class文件。但是当我尝试定义某个类的字节代码时,我的程序崩溃了。

result = defineClass(name, ByteClass, 0, ByteClass.length);

所以我得到了Exception in thread "main" java.lang.NoClassDefFoundError: java/lang/Object

以下是全班:

package com.local;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

public class ServerLoader extends ClassLoader {

    private ClassLoader parent;
    private String urlPath;

    public ServerLoader(String urlPath) {
        parent = ServerLoader.class.getClassLoader();
        this.urlPath = urlPath;
    }

    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {

        Class<?> result = null;

        try {
            URL mainUrl = new URL(urlPath + name.substring(11);
            HttpURLConnection conn = (HttpURLConnection) mainUrl.openConnection();
            conn.setRequestMethod("GET");

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            BufferedInputStream input = new BufferedInputStream(conn.getInputStream());

            int data;
            while ((data = input.read()) != -1) {
                out.write(data);
            }

            byte[] ByteClass = out.toByteArray();
            result = defineClass(name, ByteClass, 0, ByteClass.length);
            cacheClass.put(result.getName(), result);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

}    

所以这是主要方法

package com.local;

import java.lang.reflect.Method;

public class Main {

    public static void main(String[] args) throws Exception {
        ServerLoader loader = new ServerLoader("http://localhost/app/1/");

        Class<?> remoteInstanceClass = loader.loadClass("com.server.PluginA");

        Method mainMethod = remoteInstanceClass.getMethod("main", String[].class);
        mainMethod.invoke(remoteInstanceClass, (Object) args);
    }
}

并堆叠跟踪

com.local.Main
    Connected to the target VM, address: '127.0.0.1:54553', transport: 'socket'
    java.io.FileNotFoundException: http://localhost/app/1/Object.class
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1836)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
        at com.local.ServerLoader.ConnectAndResolveClass(ServerLoader.java:43)
        at com.local.ServerLoader.loadClass(ServerLoader.java:28)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:642)
        at com.local.ServerLoader.ConnectAndResolveClass(ServerLoader.java:51)
        at com.local.ServerLoader.loadClass(ServerLoader.java:28)
        at com.local.Main.main(Main.java:10)
    Exception in thread "main" java.lang.NoClassDefFoundError: java/lang/Object
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:642)
        at com.local.ServerLoader.ConnectAndResolveClass(ServerLoader.java:51)
        at com.local.ServerLoader.loadClass(ServerLoader.java:28)
        at com.local.Main.main(Main.java:10)
    Disconnected from the target VM, address: '127.0.0.1:54553', transport: 'socket'

Process finished with exit code 1

所有的名称和名称都是正确的。那么什么可能导致问题呢?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。 LoadClass方法中的每个ClassLoader必须首先使用父ClassLoader加载类,然后尝试以另一种方式加载它。所以这是正确的LoadClass方法。

@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {

    Class<?> result = null;

    try {
        result = parent.loadClass(name);
    } catch (ClassNotFoundException e) {
        try {
            URL mainURL = new URL(urlPath + name.substring(name.lastIndexOf('.') + 1) + ".class");
            HttpURLConnection conn = (HttpURLConnection) mainURL.openConnection();
            conn.setRequestMethod("GET");

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            BufferedInputStream input = new BufferedInputStream(conn.getInputStream());

            int data;
            while ((data = input.read()) != -1) {
                out.write(data);
            }

            byte[] byteClass = out.toByteArray();
            result = defineClass(name, byteClass, 0, byteClass.length);

        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    return result;
}

您询问了示例类:

package com.server;

public class PluginE {
    public static void main(String[] args) {
        System.out.println("Executing main method of PluginE");

    }
}