Another odd NoClassDefFoundError on WhiteSpaceProcessor - java

I'm strugelling with an odd problem for days now.
Only one of the users of my webapp get an NoClassDefFoundError when trying to use some functionallity. This is the stacktrace:
java.lang.NoClassDefFoundError: com/sun/xml/bind/WhiteSpaceProcessor
at com.sun.xml.bind.DatatypeConverterImpl._parseInt(DatatypeConverterImpl.java:105)
at com.foo.bar.webservice.generated.GetLoginsRequest_JaxbXducedAccessor_panelId.parse(TransducedAccessor_field_Integer.java:32)
at com.sun.xml.bind.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:166)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:406)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:384)
at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:35)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:101)
at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:224)
at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:107)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:289)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:272)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:106)
at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:424)
On a strange way WhiteSpaceProcessor can't be found while it is on the classpath.
I used tattletale to look at the possitions of the usage of the classes:
WhiteSpaceProcessor only exist once on the classpath:
DatatypeConverterImpl only exist once on the classpath
I'm stuck on the fact that the exact war on a different environment is working perfect.
working environment:
Windows machine
Tomcat 5.5.28
Java 5 (jdk1.5.0.22)
none working environment:
Linux machine
Tomcat 5.5.??
Java 5 (jdk1.5.0.22)
I hope somebody can sent me in the right direction.
tomcat server is already restarted

Did you use tattletale on the working or non-working machine?
Perhaps the failing environment contains some jar file in jre/lib/ext (or a similar extensions directory), and that's being used in preference to a "lower down" version?
EDIT: Just to go into a bit more detail about the situations in which NoClassDefFoundError can be thrown, it's worth reading the JVM spec, chapter 5. It talks about three situations:
The resource corresponding to the class can't be found at all
The resource is found, but doesn't correspond to the right class (although in that case I'd expect a message including "wrong name")
You're using a version of Java earlier than 1.2, and the class file has an unsupported major/minor version number. (This situation now throws UnsupportedClassVersionError.)
Also read section 2.17.5: it states that if the class is in an "erroneous state" (e.g. previously initialization failed, or there was a bytecode verification failure) then NoClassDefFoundError will be thrown.
Now, if the static initializer of the class fails then the first caller sees an ExceptionInInitializerError - but the second caller sees NoClassDefFoundError. Here's a short but complete program to demontrate this:
class Foo {
static {
if (true) {
throw new RuntimeException();
}
}
static void foo() {
}
}
public class Test {
public static void main(String[] args) {
try {
Foo.foo();
} catch (Throwable t) {
System.out.println("First exception: " + t);
}
try {
Foo.foo();
} catch (Throwable t) {
System.out.println("Second exception: " + t);
}
}
}
Now unless something in your system is suppressing the ExceptionInInitializerError, I'd expect to see that in the log before NoClassDefFoundError if that were the problem. I still think it's more likely that your failing system is loading one class in an extension classloader which then can't find the ShiteSpaceProcessor class.

NoClassDefFoundError does not mean that the class file cannot be found in the classpath. It means that the class cannot be loaded. This is generally due to an error during initialization, or, more often, a version mismatch in JAR files on which the class depends.
Eg, you probably compiled against XYZ package version 1.2 and your user has XYZ version 1.1 installed.

Related

NoSuchMethodError Exception causing service to not receive subsequent REST calls

I'm facing this issue for the first time, here's the exception that gets thrown in the service
Exception in thread "Thread-1" java.lang.NoSuchMethodError: org.codehaus.jackson.type.JavaType.<init>(Ljava/lang/Class;)V
at org.codehaus.jackson.map.type.TypeBase.<init>(TypeBase.java:13)
at org.codehaus.jackson.map.type.SimpleType.<init>(SimpleType.java:45)
at org.codehaus.jackson.map.type.SimpleType.<init>(SimpleType.java:40)
at org.codehaus.jackson.map.type.TypeFactory._fromClass(TypeFactory.java:374)
at org.codehaus.jackson.map.type.TypeFactory._fromType(TypeFactory.java:434)
at org.codehaus.jackson.map.type.TypeFactory.type(TypeFactory.java:61)
at org.codehaus.jackson.map.ObjectMapper.<clinit>(ObjectMapper.java:174)
at com.kx.proto.validation.util.Common.<clinit>(Common.java:41)
and once this exception is thrown, the service stops receiving the subsequent api calls made to the service ( even though i have caught the exception ) , pod is still in running state.
I'm curious about two things here
The exception that gets thrown, how to fix that ? Change in POM file?
Why would the exception that has been caught would stop service from receiving further REST api calls. Basically after the exception , all the apis in service start timing out.
Below is the piece of code where exception gets thrown
at this line
GeneratedMessageV3 messageV3 = Common.loadDefaultObjectFromMessage("com.kx.proto.", recordId.toUpperCase() + "Record");
and here's the loadDefaultObject from message method
public static GeneratedMessageV3 loadDefaultObjectFromMessage(String pkg, String messageName) {
try {
Class<?> record = Class.forName(pkg + messageName);
Optional<Method> getDefaultInstance = Arrays.asList(record.getDeclaredMethods()).stream().filter((method) -> {
return method.getName().equals("getDefaultInstance");
}).findFirst();
if (getDefaultInstance.isPresent()) {
Object obj = ((Method)getDefaultInstance.get()).invoke((Object)null, (Object[])null);
return (GeneratedMessageV3)obj;
}
} catch (ClassNotFoundException var5) {
System.out.println("class not found " + var5.getMessage());
} catch (Exception var6) {
var6.printStackTrace();
}
return null;
}
NoSuchMethodError is an Error (not an Exception) that occurs when the classpath during compilation differs from the classpath at runtime.
It says: I could find that method during compilation, but I can't find that method now at runtime.
It typically occurs when the runtime classpath differs from the compilation classpath. This gets more complex if your project A depends on jar B which depends on jar C. Presume all three projects (A, B and C) are compiled/released at a different time.
Start with doing mvn dependency:tree on your project. Ask the verbose version of that. One typical cause is dependency versions overrides with a lower version. For example your project A depends on C 1.2 and B 3.0 but B 3.0 depends on C 1.4. What if B was compiled using a method of C that only exists since C 1.3? When B is run in project A with C 1.2 instead of C 1.4, it doesn't find that method and NoSuchMethodError is thrown.
Note: there is an enforcer rule to enforce not downgrading transitive dependencies. It's very useful to avoid such surprises at runtime.

NoSuchMethodError: com.google.common.cache.CacheBuilder.maximumSize(J)

I seem to be having a problem which only applied to 1 fellow user of a minecraft plugin of mine.
[15:54:14 ERROR]: Error occurred while enabling <Plugin> v1.0.8 (Is it up to date?)
java.lang.NoSuchMethodError: com.google.common.cache.CacheBuilder.maximumSize(J)Lcom/google/common/cache/CacheBuilder;
Is there any reason as to why this is happening or if I can over come it some way?
My code:
private LoadingCache<String, String> profileCache = CacheBuilder.newBuilder().
maximumSize(500).
expireAfterWrite(4, TimeUnit.HOURS).
build(new CacheLoader<String, String>() {
public String load(String name) {
try {
return getProfileJson(name);
} catch (IOException e) {
Bukkit.getLogger().info("Error, " + e.getLocalizedMessage() + ".");
}
return null;
}
});
You need to include the libraries into the exported jar. By default they are not included in the exported Jar as the IDE assumes they are present at runtime, which they are not.
See here for Gradle and here for Maven.
java.lang.NoSuchMethodError is thrown at runtime because the JVM does not find the method in the referenced class. This typically happens because you are using different versions of a third party library for compiling and running the application.
Check the version of the library used for compiling and the version used for running the code and make sure they match or are at least compatible.

Stop Eclipse from generating bytecode for code that doesn't compile

Eclipse happily generates bytecode for Java classes that have compilation errors. The offending methods just throw exceptions when invoked at runtime.
Does anyone know how I can stop Eclipse from generating bytecode when compilation errors are present? I'd rather not have runnable code at all in the presence of errors.
As an example, consider the following code:
public class Err {
public static void main(String[] args) {
methodWithoutCompilationError();
// methodWithCompilationError();
}
private static void methodWithoutCompilationError() {
System.out.println("No error here, folks.");
}
private static void methodWithCompilationError() {
System.out.println("This method doesn't compile." + );
}
}
It runs fine, even with the compilation error. Only when I uncomment the second line of main do I see there was a problem compiling.
The reason Eclipse does this is that the compile errors might be resolved by editing another java source file. For example, if a reference to a method name in another class is misspelled, the fix could be that you correct the spelling of the reference or the fix could be that you change the name of the method. Eclipse doesn't know which you'll choose to do so it compiles the referencing class anyway in case you decide to change to other file. I'm not even sure it could otherwise know when to compile all of the classes again.
As a result, Eclipse will always compile the edited java source to the extent possible whenever you change the source file.
I'm guessing at some point you enabled the 'Continue launch if project contains errors' option. Try setting it to 'Prompt':
Try checking the "Treat above errors like fatal compile errors (make compiled code not executable)" checkbox.

Can't open java application when ran as executable jar

I had a strange problem today... I'm going to make a simplified example since it "worth a thousands words" :D
public class Application() {
public static void main(String[] args) {
try {
A a = new A(); // this may throw exceptions
// (which will cause an ExceptionInInitializerError)
} catch (Throwable t) {
JOptionPane.showMessageDialog(null, "Oooops!");
System.exit(1);
}
}
}
Since it's a stand-alone application with a Swing GUI, my goal is to give a message to the user in case of any problems (in this case at startup)... the code above works in Eclipse IDE but when I export the project as executable jar by double-clicking on it, well, it just won't open.
So I try to execute it in cmd with java -jar application.jar and it prints in the shell that there was an ExceptionInInitializerError.
Why the error was not caught?
It doesn't work even if I specify catch (ExceptionInInitializerError e).
EDIT:
After more indepth debugging, I found out that this problem only happens when two particular exceptions occur and the latter occurs in the catch block of the former.
I corrected the bug by changing the order of some checks that I do on startup.
The problem btw should never happen since it was originated by a volountary mistake of the JDBC driver class name to load in a static block.
Well, at least it made me clearly understand why constructors and static initialization blocks should not throw exceptions: it makes debugging almost impossible in the case in which the class that throws the exception is used by many classes, since it may become very hard to find out when the class is loaded.
I can think of three possible explanations for an ExceptionInInitializerError not being caught in your example:
It could be being thrown by JOptionPane.showMessageDialog(null, "Oooops!");
It could be thrown before main is called.
It could be thrown on a different stack.
In fact, I think that the 2nd one is the most likely, as ExceptionInInitializerError is thrown when some unchecked exception is thrown (and not caught) during the initialization of a class. That could well be happening before you enter the try block.

Why do I get NoClassDefFoundError: java/awt/Desktop?

I'm trying to open an URI with Swing that I get above error.
What is the reason and how can I fix it?
When I do it in console everything is OK but when I do in GUI I get this error.
I should say that I use Weblogic as server.
Code
private static void open(URI uri) {
if (Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(uri);
} catch (IOException e) { /* TODO: error handling */ }
} else { /* TODO: error handling */ }
}
Stack trace:
Exception in thread "AWT-EventQueue-1" java.lang.NoClassDefFoundError: java/awt/Desktop
at be.azvub.ext.bcfidownloder.BcfiDownloadPanel.open(BcfiDownloadPanel.java:230)
at be.azvub.ext.bcfidownloder.BcfiDownloadPanel.access$000(BcfiDownloadPanel.java:37)
at be.azvub.ext.bcfidownloder.BcfiDownloadPanel$7.actionPerformed(BcfiDownloadPanel.java:147)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:5517)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3129)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
Doc on NoClassDefFoundError
The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.
You do have some incorrect classloading happening. Mostly due to wrong class loader chaining.
NoClassDefFoundError can only be caused by a classpath problem.
Because Desktop is part of jre, make sure that your classpath contains a reference to the jre library.
In Eclipse, you can go to run configurations --> Classpath and check there
UPDATE:
As Andrew suggested, you can also check you have at least java 1.6
java.awt.Desktop has been introduced in Java 6. Chances are high you're running your code on different JRE versions.

Categories

Resources