A Java 17 application is packaged as a JAR with a Main-Class manifest entry. The JAR is run using 'java -jar app.jar'. However, the application throws a NoClassDefFoundError for a class that is inside the JAR. What is the most likely cause?
Trap 1: The Main-Class manifest entry is incorrect
Incorrect. An incorrect Main-Class manifest entry would cause the JVM to fail to find the main class, leading to a different error or the application not starting at all, not a NoClassDefFoundError for another class.
Trap 2: The classpath is not set
Incorrect. When using 'java -jar', the classpath is ignored; the JVM reads the JAR as the sole source of classes. Therefore, not setting the classpath does not cause this error.
- A
The class is not exported from its module
Correct. If the class is in a package that is not exported by its module, classes from other modules (including the unnamed module) cannot load it, resulting in NoClassDefFoundError. This is a common issue in modular applications.
- B
The Main-Class manifest entry is incorrect
Why wrong: Incorrect. An incorrect Main-Class manifest entry would cause the JVM to fail to find the main class, leading to a different error or the application not starting at all, not a NoClassDefFoundError for another class.
- C
The JAR file is corrupt
Correct. A corrupt JAR file prevents the JVM from reading the class bytes, causing NoClassDefFoundError even though the class is listed in the JAR. This is a plausible cause when the JAR is damaged.
- D
The classpath is not set
Why wrong: Incorrect. When using 'java -jar', the classpath is ignored; the JVM reads the JAR as the sole source of classes. Therefore, not setting the classpath does not cause this error.