aspose file tools*
The moose likes I/O and Streams and the fly likes ClassNotFoundException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "ClassNotFoundException" Watch "ClassNotFoundException" New topic
Author

ClassNotFoundException

Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Hi there,
I compiled FreezeAlien.java sccessfully keeping Alien.java also in the same directory ie. "~/rd/java/io" and created output file "X.file".

Now I moved X.file under "~/rd/java/io/xfiles". Here I compiled ThawAlien.java successfully, here when I run "java ThawAlien" I get java.lang.ClassNotFoundException: as I appended.

Please tell me what is going on here. This is example for "Finding the class"

Thanks
siva










Exception:
====================
~/rd/java/io/xfiles % java ThawAlien
Exception in thread "main" java.lang.ClassNotFoundException: Alien
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:217)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:558)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1513)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1435)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1626)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324)
at ThawAlien.main(ThawAlien.java:11)
~/rd/java/io/xfiles %





[ August 10, 2004: Message edited by: Siva kandasamy ]
[ August 11, 2004: Message edited by: Siva kandasamy ]
Petr Blahos
Ranch Hand

Joined: Apr 28, 2004
Posts: 131
Well, that's because the virtual machine must know
the object it reads from the ObjectInputStream. You
wrote Alien and now you are trying to read the object.
The object happens to be Alien and java must know how
to instantiate it.

So either copy all the class files in the same directory
or use CLASSPATH.

Petr


Get a better web browser:<br /><a href="http://www.mozilla.org/products/firefox/switch.html" target="_blank" rel="nofollow">http://www.mozilla.org/products/firefox/switch.html</a>
Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
This is example for "Finding class" from Thinking in Java, 2nd edition,

Meaning, "you serialize an object and send it as a file or through a network to another machine. The program on the other machine reconstruct the object using only the contents of the file"

My understanding is, it suppose to work the way I descriped, but not sure what is wrong.

For more details, here is the insert from the book.
Finding the class

You might wonder what�s necessary for an object to be recovered from its serialized state. For example, suppose you serialize an object and send it as a file or through a network to another machine. Could a program on the other machine reconstruct the object using only the contents of the file?

The best way to answer this question is (as usual) by performing an experiment. The following file goes in the subdirectory for this chapter:

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

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

The file that creates and serializes an Alien object goes in the same directory:

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

public class FreezeAlien {
// Throw exceptions to console:
public static void main(String[] args)
throws IOException {
ObjectOutput out =
new ObjectOutputStream(
new FileOutputStream("X.file"));
Alien zorcon = new Alien();
out.writeObject(zorcon);
}
} ///:~

Rather than catching and handling exceptions, this program takes the quick and dirty approach of passing the exceptions out of main( ), so they�ll be reported on the command line.

Once the program is compiled and run, copy the resulting X.file to a subdirectory called xfiles, where the following code goes:

//: c11:xfiles:ThawAlien.java
// Try to recover a serialized file without the
// class of object that's stored in that file.
import java.io.*;

public class ThawAlien {
public static void main(String[] args)
throws IOException, ClassNotFoundException {
ObjectInputStream in =
new ObjectInputStream(
new FileInputStream("X.file"));
Object mystery = in.readObject();
System.out.println(mystery.getClass());
}
} ///:~

This program opens the file and reads in the object mystery successfully. However, as soon as you try to find out anything about the object�which requires the Class object for Alien�the Java Virtual Machine (JVM) cannot find Alien.class (unless it happens to be in the Classpath, which it shouldn�t be in this example). You�ll get a ClassNotFoundException. (Once again, all evidence of alien life vanishes before proof of its existence can be verified!)

If you expect to do much after you�ve recovered an object that has been serialized, you must make sure that the JVM can find the associated .class file either in the local class path or somewhere on the Internet.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the I/O and Streams forum..


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

You should update your copy of Thinking In Java:

Even opening the file and reading in the object mystery requires the Class object for Alien; the JVM cannot find Alien.class (unless it happens to be in the Classpath, which it shouldn�t be in this example). You�ll get a ClassNotFoundException. (Once again, all evidence of alien life vanishes before proof of its existence can be verified!) The JVM must be able to find the associated .class file.

Thinking In Java, 3rd ed. Revision 4.0
[ August 12, 2004: Message edited by: Joe Ess ]

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
 
subject: ClassNotFoundException