public class PropHolder {
public static Properties prop;
static {
//code for loading properties from file
}
}
// Referencing the class somewhere else:
Pr
My best bet is there is an issue here:
static {
//code for loading properties from file
}
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.
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:
static {
try {
... your init code here
} catch (Throwable t) {
LOG.error("Failure during static initialization", t);
throw t;
}
}
You are getting a java.lang.NoClassDefFoundError
which does NOT mean that your class is missing (in that case you'd get a java.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.).
I had the same problems:java.lang.NoClassDefFoundError: Could not initialize class com.xxx.HttpUtils
static {
//code for loading properties from file
}
it is the environment problem.That means the properties in application.yml is incorrect or empty!
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
mvn -U clean test -Dtest=path
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.
-Dxxx.version=xxxxx #sorry can't post more
Maybe you have already solved this problem, but still hope it will help others who meet the same problem.
I encounter the same problem. I inited a bean object in static block like below:
static {
try{
mqttConfiguration = SpringBootBeanUtils.<MqttConfiguration>getBean(MqttConfiguration.class);
}catch (Throwable e){
System.out.println(e);
}
}
Just because the process the my bean obejct inition caused a NPE, I get trouble into it. So I think you should check you static code block carefully.