Find a class

Source: Internet
Author: User

Readers may wonder why an object is needed to recover from its serialized state. For example, suppose we serialize an object and pass it over the network as a file to another machine. At this point, can a program located on another machine use only the file directory to reconstruct the object?
The best way to answer this question is to do an experiment. The following document is located in the subdirectory of this chapter:

: Alien.java
//A serializable class
import java.io.*;

public class Alien implements Serializable {
}///:~


The files used to create and serialize a alien object are located in the same directory:

: Freezealien.java
//Create a serialized output file
import java.io.*;

public class Freezealien {public
  static void Main (string[] args) 
      throws Exception {
    objectoutput out =
   new ObjectOutputStream (
        new FileOutputStream ("file.x"));
    Alien Zorcon = new Alien ();
    Out.writeobject (Zorcon); 
  }
///:~


Instead of capturing and controlling the violation, the program is simply and directly passed to main () outside, so that they can be reported at the command line.
After the program compiles and runs, copies the resulting file.x to the subdirectory named Xfiles, which is as follows:

: Thawalien.java
//Try to recover a serialized file without the 
"/Class of Object" ' s stored in that file.< C4/>package C10.xfiles;
Import java.io.*;

public class Thawalien {public
  static void Main (string[] args) 
      throws Exception {
    ObjectInputStream in =
   new ObjectInputStream (
        new FileInputStream ("file.x"));
    Object mystery = In.readobject ();
    System.out.println (
      mystery.getclass (). toString ());
  }
///:~

The program can open the file and read the contents of the mystery object successfully. However, once you try to find any information about the object-this requires that the Alien class object--java the virtual machine (the JVM) cannot find Alien.class (unless it happens to be in the classpath, and this example should be the opposite). This would have been a violation of the name ClassNotFoundException (similarly, it would have disappeared if it had not been possible to verify evidence of alien existence).
Once you have restored a serialized object, if you want to do more with it, you must ensure that the JVM can find the relevant. class file on the local classpath or somewhere else on the internet.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.