【问题标题】:Java NoClassDefFoundError amfJava NoClassDefFoundError amf
【发布时间】:2017-07-18 12:35:51
【问题描述】:

我正在尝试将 java sn-p 编译成 jar 文件,我遇到了经典的 java 运行时异常,但我无法解决问题。 这是从 Markus Wulftange 借来的代码:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;

import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.ActionContext;
import flex.messaging.io.amf.ActionMessage;
import flex.messaging.io.amf.AmfMessageDeserializer;
import flex.messaging.io.amf.AmfMessageSerializer;
import flex.messaging.io.amf.MessageBody;

public class Amf3ExternalizableUnicastRef {

public static void main(String[] args) throws IOException, ClassNotFoundException {
    if (args.length < 2 || (args.length == 3 && !args[0].equals("-d"))) {
        System.err.println("usage: java -jar " + Amf3ExternalizableUnicastRef.class.getSimpleName() + ".jar [-d] <host> <port>");
        return;
    }
    boolean doDeserialize = false;
    if (args.length == 3) {
        doDeserialize = true;
        args = Arrays.copyOfRange(args, 1, args.length);
    }

    // generate the UnicastRef object
    Object unicastRef = generateUnicastRef(args[0], Integer.parseInt(args[1]));

    // serialize object to AMF message
    byte[] amf = serialize(unicastRef);

    // deserialize AMF message
    if (doDeserialize) {
        deserialize(amf);
    } else {
        System.out.write(amf);
    }
}

public static Object generateUnicastRef(String host, int port) {
    java.rmi.server.ObjID objId = new java.rmi.server.ObjID();
    sun.rmi.transport.tcp.TCPEndpoint endpoint = new sun.rmi.transport.tcp.TCPEndpoint(host, port);
    sun.rmi.transport.LiveRef liveRef = new sun.rmi.transport.LiveRef(objId, endpoint, false);
    return new sun.rmi.server.UnicastRef(liveRef);
}

public static byte[] serialize(Object data) throws IOException {
    MessageBody body = new MessageBody();
    body.setData(data);

    ActionMessage message = new ActionMessage();
    message.addBody(body);

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    AmfMessageSerializer serializer = new AmfMessageSerializer();
    serializer.initialize(SerializationContext.getSerializationContext(), out, null);
    serializer.writeMessage(message);

    return out.toByteArray();
}

public static void deserialize(byte[] amf) throws ClassNotFoundException, IOException {
    ByteArrayInputStream in = new ByteArrayInputStream(amf);

    AmfMessageDeserializer deserializer = new AmfMessageDeserializer();
    deserializer.initialize(SerializationContext.getSerializationContext(), in, null);
    deserializer.readMessage(new ActionMessage(), new ActionContext());
  }
}    

使用位于同一目录中的包 flex-messaging-core,我编译成一个 jar javac -cp flex...jar sourcefile.java.

然后编译成一个jar jar -cfm myjar.jar MANIFEST.ML myclass.class.

但是,当从带有正确参数的 shell 运行时 java -jar myjar.jar -d 127.0.0.1 8000 它在 threadmain java.lang.NoClassDefFoundError : flex/messaging/io/amf/MessageBody 中引发异常。

google 了 2 天,尝试了所有的解决方案,但真的不能自己解决问题,能否请我帮忙?

【问题讨论】:

  • NoClassDef 表示编译器没有找到该类。您可以验证包是否具有此类,路径是否全部正确,是否链接了依赖项...
  • 嗨,Leonardo,谢谢你的回答,我已经检查了这个包,它有“offensor”类,即 flex.messaging.io.amf.MessageBody。类路径和依赖项似乎也是正确的
  • 你的 Manifest.ml 是什么?可能您应该使用与编译类相同的类路径来执行 jar:java -cp flex...jar -jar myjar.jar -d 127.0.0.1 8000
  • 我的 manifest.ml 包含这些头文件 Manifest-Version: 1.0 Main-Class: Amf3ExternalizableUnicastRef Class-Path: flex-messaging-core-4.7.2.jar
  • 为了清楚起见,jar 编译并执行,直到它调用 Messagebody 类,我放置了一些 System.out.println 来查明这一点

标签: java deserialization blazeds amf


【解决方案1】:

不应该是 MANIFEST.MF,而不是 MANIFEST.ML?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2021-11-15
    • 1970-01-01
    相关资源
    最近更新 更多