Question

I've just refactored a project in Eclipse to use packages instead of the default package to better organise my code. I have a test program which creates a number of Runnable objects, each of which are run sequentially to each other, but in parallel with the main program.

Before refactoring this worked fine, with each thread dutifully performing its task. However, since moving things into packages I'm getting a ClassNotFound exception as soon as one of the Runnable classes attempts to use a class from another package. Stacktrace follows:

java.lang.ClassNotFoundException: Tweet
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at indexing.TweetCleanup.run(TweetCleanup.java:84)
at java.lang.Thread.run(Unknown Source)

In this trace 'TweetCleanup' is the Runnable class, and 'Tweet' is the class not found. I've included it in TweetCleanup as 'common.Tweet' (where common is the package). I've used a separate test program to see if it can see the class in the main thread, which runs successfully.

All I can think of is that the Thread needs to be supplied some classpath to 'see' the Tweet class, however this wasn't the case before refactoring as packages. I believe the default behaviour of a child Thread is to use the classpath of its parent, which includes 'common.Tweet'.

I'm using Eclipse Helios as my IDE.

Any tips would be appreciated!

Cheers,

P

Was it helpful?

Solution

Not a Threading problem at all, but you're using Java Serialization somehow and you try to read an Object from a stream which has "older" objects stored within it.

I suspect you're using some kind of persistence by Java Serialization, and your TweetCleanup-Thread is reading in the old "database". Delete this file and your program will work again.

Note that renaming packages or moving classes is a binary incompatible change - you cannot deserialize such objects anymore.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top