当反序列化二进制类文件的内容时,ClassNotFoundException [英] ClassNotFoundException when deserializing a binary class file's contents

查看:938
本文介绍了当反序列化二进制类文件的内容时,ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解Java。我试图读取一个文件包含一个int和类的名为汽车的各种实例。当我反序列化它,虽然,程序抛出一个ClassNotFoundException,我似乎不明白为什么。

I don't know much about Java. I'm trying to read a file containing an int and various instances of a class called "Automobile". When I deserialize it, though, the program throws a ClassNotFoundException and I can't seem to understand why.

这里的代码:

        try {
        FileInputStream fin = new FileInputStream(inputFile);
        ObjectInputStream input = new ObjectInputStream(fin);

        conto = input.readInt();

        Automobile[] macchine = new Automobile[conto];

        for(int i = 0; i < conto; i++) {
            macchine[i] = (Automobile)input.readObject();
        }

        String targa;
        System.out.print("\nInserire le cifre di una targa per rintracciare l'automobile: ");
        targa = sc1.nextLine();

        for(int i = 0; i < conto; i++) {
            if(macchine[i].getTarga().equals(targa))
                System.out.println(macchine[i]);
        }

    } catch(IOException e) {
        System.out.println("Errore nella lettura del file "+inputFile);
    } catch(java.lang.ClassNotFoundException e) {
        System.out.println("Class not found");
    }

提前感谢。

编辑:这里是stacktrace

here's the stacktrace

java.lang.ClassNotFoundException: es4.Automobile
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:604)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1575)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
    at es4p2.Main.main(Main.java:35)


推荐答案

当你反序列化一个序列化对象树时,所有对象的类必须在类路径上。在这种情况下, ClassNotFoundException 最有可能意味着所需的类之一不在类路径上。

When you deserialize a serialized object tree, the classes of all the objects have to be on the classpath. In this context, a ClassNotFoundException most likely means that one of the classes required is not on the classpath. You have to address this for deserialization to work.

在这种情况下, es4.Automobile 缺失。


这个问题可能是由汽车引发的自定义异常引起的吗? / p>

Could the problem be caused by a custom exception I made which is fired by Automobile?

我可以想到的唯一其他可能是:

The only other possibilities I can think of are:


  • es4.Automobile 对缺少的其他类有直接或间接的依赖

  • es4.Automobile 或一个依赖类已抛出异常,但未在类中捕获。

  • es4.Automobile has a direct or indirect dependency on some other class that is missing
  • the static initialization of es4.Automobile or a dependent class has thrown an exception that has not been caught internally to the class.

但是两者都应该(我认为)导致不同的堆栈跟踪。

But both of those should (I think) have resulted in a different stack trace.


我注意到包名是es4p2,而不是es4。为什么说es4?可能是因为保存文件的程序使用了另一个包名?

I just noticed the package name is es4p2, not es4. Why does it say es4? Could it be because the program which saves the file uses another package name?

我不知道为什么它们不同。你需要跟谁编写代码/产生序列化对象。但是,这是最有可能您的问题的原因。具有不同包名称的类是不同的类。

I've no idea why they are different. You'd need to talk to whoever wrote the code / produced the serialized objects. However, this is most likely the cause of your problem. A class with a different package name is a different class. Period.

当捕获意外异常时,应始终输出(或更好地记录)stacktrace。这将告诉你(和我们)更多的是什么错了,在这种情况下,类的名称丢失。

You should always output (or better, log) the stacktrace when an unexpected exception is caught. That will tell you (and us) more about what has gone wrong, and in this case the name of the class that is missing.

这篇关于当反序列化二进制类文件的内容时,ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆