226
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?

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
Leon
  • 8,151
  • 11
  • 45
  • 51

10 Answers10

279

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.

buræquete
  • 14,226
  • 4
  • 44
  • 89
John Vint
  • 39,695
  • 7
  • 78
  • 108
  • I have been facing the same problem time and again. I am sure that it is because of the `static` issue. What needs to be done to resolve the problem? – viper Aug 09 '16 at 04:18
  • 3
    You will need to identify what exception is being thrown from the `static` block. To debug it put a `try/catch(Exception e)` around the entire block and log the exception. You'll have to fix that exception. Typically the exception will be logged but may be hard to find since it's being logged during classloading which may happen very early – John Vint Aug 09 '16 at 11:30
  • Yes I kept the code in `try catch` block and it said `Failed to initialize ClassA`. I think it is the problem of `JVM`. I restarted my system and then everything worked fine. How do I resolve this problem in future without restarting my system and solve the problem with a simple solu4tion. – viper Aug 09 '16 at 11:57
  • Failed to initialize ClassA is a side effect from something else. You will want to look at the `cause` if available. A `NoClassDefFoundError` is always associated to another error, you will need to look for it in the logs or try to log it more appropriately (like force the logging into a new file on the file system) – John Vint Aug 09 '16 at 12:08
  • It would be more easily traceable if Java would throw an error CouldNotInitializeStaticPartOfClassError or something. Then we as developer would know where to look. – Maarten Nov 14 '18 at 11:12
  • @Maarten Good point. This is what I've noticed of error logging as a whole that it could be more specific and point the developer at what the actual issue is – committedandroider Oct 03 '19 at 18:02
  • I have the same issue. Some properties are read in the static initializer block. One property that is newly added into the properties file causes this problem. After restart the application server the problem is disappeared. I think the properties file is loaded only during the start of the application server. – chance Nov 13 '20 at 11:06
  • 1
    @chance That's right. The static tantalizer is only executed once per Java classloading which for most applications will be once per application run. You would need something like a [FileChangedReloadingStrategy](https://commons.apache.org/proper/commons-configuration/javadocs/v1.10/apidocs/org/apache/commons/configuration/reloading/FileChangedReloadingStrategy.html). Java also introduced a [WatchService](https://docs.oracle.com/javase/7/docs/api/java/nio/file/WatchService.html) – John Vint Nov 13 '20 at 12:10
  • 2
    I was getting the same issue due to mismatch with the properties file key name and used in my code. After fixing the properties file problem got resolved – Nagaraj Vittal Oct 27 '21 at 14:48
  • If the code in the `static` block doesn't swallow the exception (if it did, then there should be no problem loading the class), shouldn't you get an `ExceptionInInitializerError`? What if you're getting this `NoClassDefFoundError` but there's no `ExceptionInInitializerError` nor `ClassNotFoundException` in the stack trace? – Slaw Jul 10 '22 at 21:18
  • I had the same problem. Inside the static block I was trying to add elements to a null map – D.Raluca May 23 '23 at 06:03
161

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.).

jeha
  • 10,562
  • 5
  • 50
  • 69
  • 1
    one clarifaction is that even though NoClassDefFoundError does not imply a ClassNotFoundException, it is still a possible cause of the NoClassDefFoundError. – John Vint Sep 06 '11 at 20:47
  • 1
    buf if you had a ClassNotFoundException then the ClassLoader would / could never try load the class, right? – jeha Sep 06 '11 at 20:54
  • 4
    A class can be loading another class that was not found. The cause in that instance is still a ClassNotFoundException – John Vint Sep 06 '11 at 20:56
  • 1
    I should've clarified, I just meant an exception.getCause() – John Vint Sep 06 '11 at 20:57
  • @nterry what was your issue??!! – Robert Apr 13 '16 at 23:17
  • @Robert i think it was a set of conflicting dependencies. Using a maven exclusion, i was able to get the right version loaded – Nicholas Terry Apr 14 '16 at 00:21
  • 1
    This one was really helpful here, since I've spent about 20 mins verifying the JAR (the reported class belongs to) is included. Once I realized I'm looking into wrong direction, I easily understood that likely some annotation class is missing and voila, the reported one was declared as `@Stateless`, so I just added the corresponding dependency and was able to proceed further. Thanks for the tip! – RAM237 Apr 13 '20 at 22:25
41

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;
  }
}
Mark Hansen
  • 1,044
  • 10
  • 15
5

I had the same exception, this is how I solved the problem:

Preconditions:

  1. Junit class (and test), that extended another class.

  2. ApplicationContext initialized using spring, that init the project.

  3. 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.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
KerenSi
  • 389
  • 7
  • 20
3

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.

TriMix
  • 231
  • 3
  • 4
2

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.

MonkeyKing
  • 53
  • 10
  • What's more, when the code runs on the linux machine, follow the error above, there is another problem : `java.lang.ExceptionInInitializerError: null`, it means that can not find the class in the classloader, or don't know to load which one(I guess). Did you meet that? – MonkeyKing Oct 28 '15 at 03:55
1

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 to null instead and everything went back to normal.

lifeson106
  • 503
  • 4
  • 11
1

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.

        defaultConfig {
    applicationId "com.xxx.yyy"
    minSdkVersion 15
    targetSdkVersion 28
    versionCode 5123
    versionName "5123"
    // Enabling multidex support.
    multiDexEnabled true
}

but apparently this wasn't enough. but when i changed:

public class MyAppClass  extends Application 

to:

public class MyAppClass  extends MultiDexApplication 

this solved it. hope this will help someone

Elad
  • 1,523
  • 13
  • 10
0

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 but DateTimeFormatter cannot be created at class init time.

private static final String PATTERN = "yyyy-MM-yyThh:mm:ss.SSSSSSV"; // OK
private static DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(PATTERN); // WRONG; as it depends on another field to be created first

The solutions are:

  • initialize the DateTimeFormatter in a @PostConstruct method
  • or in the method where it is needed.
WesternGun
  • 11,303
  • 6
  • 88
  • 157
-1

Adding these import statements resolved the issue:

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
Laurel
  • 5,965
  • 14
  • 31
  • 57
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 10 '22 at 17:53