java.lang.NoClassDefFoundError:无法初始化类 XXX
public class PropHolder {
public static Properties prop;
static {
//code for loading properties from file
}
}
// Referencing the class somewhere else:
Properties prop = PropHolder.prop;
class PropHolder
是我自己的一个类。该类驻留在主类的同一个 JAR 文件中。所以这不应该是因为类路径中缺少任何 JAR。
当我通过 jar tf myjarfile
查看 JAR 文件时,我可以看到其中列出的 PropHolder.class
。
顺便说一句:代码在我的本地计算机上运行良好。但是当我用一些脚本将它部署到 Linux 服务器上时无法工作。所以我认为不是代码的问题。 但由于某种原因。部署过程很难跟踪。
可能是什么问题?
public class PropHolder {
public static Properties prop;
static {
//code for loading properties from file
}
}
// Referencing the class somewhere else:
Properties prop = PropHolder.prop;
class PropHolder
is a class of my own. The class resides in the same JAR file of the main class. So that should not because any JAR is missing from classpath.
When I look in to the JAR file by jar tf myjarfile
, I can see the PropHolder.class
listed there.
Btw: the code is running fine on my local machine. But couldn't work when I deploy it with some script onto a Linux server. So I think it is not the problem of the code.
But for some reason. the deploy process is very hard to track.
What could be the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我最好的选择是这里存在一个问题:
似乎发生了一些未捕获的异常,并传播到尝试加载该类的实际类加载器。不过,我们需要堆栈跟踪来确认这一点。
或者是在创建
PropHolder.prop
静态变量时发生的。My best bet is there is an issue here:
It would appear some uncaught exception occurred and propagated up to the actual ClassLoader attempting to load the class. We would need a stacktrace to confirm this though.
Either that or it occurred when creating
PropHolder.prop
static variable.您收到一个
java.lang.NoClassDefFoundError
,这并不意味着您的类丢失了(在这种情况下您会收到一个java.lang.ClassNotFoundException
)。尝试读取类时,类加载器在读取类定义时遇到错误。将 try/catch 放入静态初始化程序中并查看异常。如果您在那里读取了一些文件,并且它与您的本地环境不同,则很可能是问题的原因(可能找不到文件,没有权限等)。
You are getting a
java.lang.NoClassDefFoundError
which does NOT mean that your class is missing (in that case you'd get ajava.lang.ClassNotFoundException
). The ClassLoader ran into an error while reading the class definition when trying to read the class.Put a try/catch inside your static initializer and look at the exception. If you read some files there and it differs from your local environment it's very likely the cause of the problem (maybe file can't be found, no permissions etc.).
NoClassDefFoundError 没有提供太多关于静态块内部出了什么问题的线索。最好在 static { ... } 初始化代码中始终包含这样的块:
NoClassDefFoundError doesn't give much of a clue as to what went wrong inside the static block. It is good practice to always have a block like this inside of static { ... } initialization code:
我遇到了同样的异常,这就是我解决问题的方法:
先决条件:
Junit 类(和测试),扩展了另一个类。
ApplicationContext 使用 spring 初始化,初始化项目。
应用程序上下文在@Before方法中初始化
解决方案:
从@BeforeClass方法初始化应用程序上下文,因为父类还需要一些从应用程序上下文中初始化的类。
希望这会有所帮助。
I had the same exception, this is how I solved the problem:
Preconditions:
Junit class (and test), that extended another class.
ApplicationContext initialized using spring, that init the project.
The Application context was initialized in @Before method
Solution:
Init the application context from @BeforeClass method, since the parent class also required some classes that were initialized from within the application context.
Hope this will help.
如上所述,这可能有很多原因。就我而言,我有一个静态初始化的变量,它依赖于属性文件中缺少的条目。将缺少的条目添加到属性文件中,问题得到解决。
As mentioned above, this could be a number of things. In my case I had a statically initialized variable which relied on a missing entry in my properties file. Added the missing entry to the properties file and the problem was solved.
就在几天前,我也遇到了和你一样的问题。所有代码在我的本地计算机上运行良好,但结果出现错误(noclassdeffound&initialize)。
所以我发布了我的解决方案,但我不知道为什么,我只是提出一种可能性。我希望有人知道可以解释一下。@John Vint
首先,我将向您展示我的问题。我的代码既有静态变量又有静态块。当我第一次遇到这个问题时,我尝试了John Vint的解决方案,并尝试捕获异常。然而我什么也没抓到。所以我认为这是因为静态变量(但现在我知道它们是同一件事),但仍然没有发现任何东西。
所以,我尝试找出linux机器和我的电脑之间的区别。然后我发现只有当多个线程在一个进程中运行时才会出现这个问题(顺便说一下,linux机器是双核双进程的)。这意味着如果有两个任务(都使用具有静态块或变量的代码)在同一个进程中运行,则会出错,但如果它们在不同的进程中运行,则两者都可以。
在Linux机器中,我用来
运行一个任务,因为我的静态变量是启动一个容器(或者可能你初始化一个新的类加载器),所以它会一直停留到jvm停止,并且jvm仅在所有任务都停止时停止在一个过程中停止。每个任务都会启动一个新的容器(或类加载器),这会让 jvm 感到困惑。结果,发生了错误。
那么,如何解决呢?我的解决方案是在maven命令中添加一个新命令,并使每个任务都进入同一个容器。
也许您已经解决了这个问题,但仍然希望它能帮助遇到同样问题的其他人。
Just several days ago, I met the same question just like yours. All code runs well on my local machine, but turns out error(noclassdeffound&initialize).
So I post my solution, but I don't know why, I merely advance a possibility. I hope someone know will explain this.@John Vint
Firstly, I'll show you my problem. My code has static variable and static block both. When I first met this problem, I tried John Vint's solution, and tried to catch the exception. However, I caught nothing. So I thought it is because the static variable(but now I know they are the same thing) and still found nothing.
So, I try to find the difference between the linux machine and my computer. Then I found that this problem happens only when several threads run in one process(By the way, the linux machine has double cores and double processes). That means if there are two tasks(both uses the code which has static block or variables) run in the same process, it goes wrong, but if they run in different processes, both of them are ok.
In the Linux machine, I use
to run a task, and because my static variable is to start a container(or maybe you initialize a new classloader), so it will stay until the jvm stop, and the jvm stops only when all the tasks in one process stop. Every task will start a new container(or classloader) and it makes the jvm confused. As a result, the error happens.
So, how to solve it? My solution is to add a new command to the maven command, and make every task go to the same container.
Maybe you have already solved this problem, but still hope it will help others who meet the same problem.
如果您正在开发 Android 项目,请确保您没有调用任何 Android 类上的任何静态方法。我只使用 JUnit + Mockito,所以也许其他一些框架可以帮助您完全避免这个问题,我不确定。
我的问题是调用
Uri.parse(uriString)
作为单元测试的静态初始化程序的一部分。 Uri 类是一个 Android API,这就是单元测试构建找不到它的原因。我将此值更改为null
,一切都恢复正常。If you're working on an Android project, make sure you aren't calling any static methods on any Android classes. I'm only using JUnit + Mockito, so maybe some other frameworks might help you avoid the problem altogether, I'm not sure.
My problem was calling
Uri.parse(uriString)
as part of a static initializer for a unit test. The Uri class is an Android API, which is why the unit test build couldn't find it. I changed this value tonull
instead and everything went back to normal.我有同样的异常 - 但只有在调试模式下运行时,
这就是我解决问题的方法(三天后):
在 build.gradle 中我有:
在 defaultConfig 部分中设置“multiDexEnabled true”。
但显然这还不够。
但当我改变:
到:
这解决了它。
希望这会帮助某人
I had the same exception - but only while running in debug mode,
this is how I solved the problem (after 3 whole days):
in the build.gradle i had :
"multiDexEnabled true" set in the defaultConfig section.
but apparently this wasn't enough.
but when i changed:
to:
this solved it.
hope this will help someone
感谢您提出的问题以及来自投票最高答案的提示。这让我重新思考静态字段,最后,我发现我定义了一个 DateTimeFormatter ,它需要创建另一个静态常量;看起来 String 是常量很好,但是
DateTimeFormatter
不能在类初始化时创建。解决方案是:
@PostConstruct
方法中DateTimeFormatter
。Thanks for the question and hints from the top voted answers. It makes me rethink about static fields and at last, I found that I defined a
DateTimeFormatter
which needs another static constant to be created; seems that String being constant is fine butDateTimeFormatter
cannot be created at class init time.The solutions are:
DateTimeFormatter
in a@PostConstruct
method添加这些导入语句解决了该问题:
Adding these import statements resolved the issue: