在 Java 中使用包启动线程时 Eclipse 中出现 ClassNotFound 异常

发布于 2024-10-07 11:06:21 字数 1470 浏览 0 评论 0原文

我刚刚在 Eclipse 中重构了一个项目,以使用包而不是默认包来更好地组织我的代码。我有一个测试程序,它创建了许多 Runnable 对象,每个对象都按顺序运行,但与主程序并行运行。

在重构之前,这工作得很好,每个线程都尽职尽责地执行其任务。但是,由于将内容移入包中,只要其中一个 Runnable 类尝试使用另一个包中的类,我就会收到 ClassNotFound 异常。堆栈跟踪如下:

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)

在此跟踪中,“TweetCleanup”是 Runnable 类,“Tweet”是未找到的类。我已将其作为“common.Tweet”包含在 TweetCleanup 中(其中 common 是包)。我使用了一个单独的测试程序来查看它是否可以在主线程中看到该类,并且该程序运行成功。

我能想到的是,需要为 Thread 提供一些类路径来“查看”Tweet 类,但是在重构为包之前情况并非如此。我相信子线程的默认行为是使用其父线程的类路径,其中包括“common.Tweet”。

我使用 Eclipse Helios 作为我的 IDE。

任何提示将不胜感激!

干杯,

P

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九命猫 2024-10-14 11:06:21

根本不是线程问题,但您以某种方式使用 Java 序列化,并且尝试从其中存储了“较旧”对象的流中读取对象。

我怀疑您正在使用 Java 序列化的某种持久性,并且您的 TweetCleanup-Thread 正在旧的“数据库”中读取。删除该文件,您的程序将再次运行。

请注意,重命名包或移动类是二进制不兼容的更改 - 您无法再反序列化此类对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文