Shared jar and noclassdeffound java - java

Im running a debian java server that needs to send and receive objects of type EventObject and PostObject (e.g serializable). These have been placed in a .jar file SharedModels.jar and are used both in client and server.
On the windows installation (Eclipse), using
import Models.EventObject;
import Models.PostObject;
works fine (including external Jar through Eclipse).
It compiles fine, but when the method is called on the server , the server doesn't recognize the class anymore. This is the output:
Exception in thread "Thread-0" java.lang.NoClassDefFoundError: Models/PostObject
at server.Database.getPosts(Database.java:101)
at server.ServerThread.run(ServerThread.java:47)
Caused by: java.lang.ClassNotFoundException: Models.PostObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
I've understood this is because JVM does recognize the class at compiletime but not during runtime.. OR something is wrong with classpath. Does the name of the actual .jar need to have any naming to fid the package contained within? What do I need to do to fix this?

If your Models/PostObject is a class from some jar, then make sure that the class is included in exported jar/war (simply open it with tool like 7zip and manually confirm that the needed class is there).
How to create a jar with external libraries included in Eclipse?

This is thrown when at compile time the required classes are present , but at run time the classes are changed or removed or class's static initializes threw exceptions. It means the class which is getting loaded is present in classpath , but one of the classes which are required by this class , are either removed or failed to load by compiler .So you should see the classes which are dependent on this class

Related

ClassNotFoundException on runtime in a library module of my project

I have an Android project in Android Studio;
And I create a java-library module (the steps is: File -> New -> New Module -> Java library);
I add a third library by gradle, such as: "compile 'com.squareup.okhttp3:okhttp:3.9.1'";
then, I use the OkHttpClient in my Main class's main method, It ok, compile successfully.
But I run the Main method, the console print errors:
```
Exception in thread "main" java.lang.NoClassDefFoundError: okhttp3/OkHttpClient
at com.zhang.anan.mylib.myClass.main(myClass.java:12)
Caused by: java.lang.ClassNotFoundException: okhttp3.OkHttpClient
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
```
As per the Oracle documentation, ClassNotFoundException is thrown following the failure of a class loading call, using its string name, as per below:
The Class.forName method
The ClassLoader.findSystemClass method
The ClassLoader.loadClass method
In other words, it means that one particular Java class was not found or could not be loaded at runtime from your application current context class loader.
How to resolve
Identify the caller :From the stack trace just before the Class.forName() or ClassLoader.loadClass() calls. This will help you understand if your application code is at fault vs. a third party API.
Determine if your application code is not packaged properly e.g. missing JAR file(s) from your classpath.
If the missing Java class is not from your application code, then identify if it belongs to a third party API you are using as per of your Java application. Once you identify it, you will need to add the missing JAR file(s) to your runtime classpath or web application WAR/EAR file.

In java runtime, class not found exception

I am building the jar and I'm using this jar in one the my .war. When I run the program I am getting the below exception. But in that jar file, that particular class is there.
Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class com.itc.zeas.custominputformat.CustomTextInputFormat not found
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2195)
at org.apache.hadoop.mapreduce.task.JobContextImpl.getInputFormatClass(JobContextImpl.java:174)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:749)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:168)
at java.security.AccessController.doPrivileged(Native Method)
at
A quick search turned this up.
Your classpath is broken (which is a very common problem in the Java world).
Depending on how you start your application, you need to revise the argument to -cp, your Class-Path entry in MANIFEST.MF or your disk layout.
Maybe you should post more information? Which tools are you using to develop the program, which parameters when compiling, etc..

Class not found even when in Jar in the classpath

I am trying to deploy an application that is using Jackson, JUnit, and Commons-IO. I have the following Jars in my application's classpath:
commons-io-2.4.jar
jackson-databind-2.7.0.jar
jackson-annotations-2.7.0.jar
log4j-api-2.4.1.jar
wsdiscovery-0.2.jar
jackson-core-2.7.0.jar
log4j-core-2.4.1.jar
This application works within my development environment, and I have deployed all of the above Jars with the main application jar. I can run the application without problems, but every time I try to use it I get the following failure:
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
at com.oncam.hware.app.OnvifApp.formatOutput(OnvifApp.java:356)
at com.oncam.hware.app.OnvifApp.dispatchCommand(OnvifApp.java:271)
at com.oncam.hware.app.OnvifApp.loopSocket(OnvifApp.java:130)
at com.oncam.hware.app.OnvifApp.useSocket(OnvifApp.java:216)
at com.oncam.hware.app.OnvifApp.main(OnvifApp.java:473)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 5 more
The ObjectMapper class is in the jackson-databind-2.7.0.jar file. Furthermore, I have no problems accessing the classes in the other jar files (including the JUnit jars!). For some reason, it is as if the classloader is loading every Jar except jackson-databind-2.7.0.jar.
Does anyone know what is causing this and how I can fix it?
Someone please advise...
I figured out what was wrong.
It turns out that the environment I am using (Eclipse!) does not properly update the manifest file when you export your code to a JAR file. Without the proper manifest entries, the application cannot "find" the dependent jar files.
This is, in my opinion, a serious oversight on the part of the Eclipse folks -- especially when you have an application that depends on a lot of jar files. In order to make my application run, I had the following choices:
Create a script that runs the jvm and has a list of parameters pointing to every needed jar file, or:
2: Manually enter each required jar file into the Manifest file
To my knowledge, there is no way to automatically update the manifest file. This is a serious PITA (Pain In The A**)...
Anyway, sorry for bothering people about this problem. Hopefully, posting this answer will help others avoid similar problems...

NoClassDefFoundError Java (wrong name)

I've seen questions like this on here, but I can't find one that is specific to my situation, so sorry if this is a worn out question.
I've got a class P that is part of a package myname.utils located in ~/JavaClasses/myname/utils. I've got another class Printing that is not declared as a part of any package but is located in ~/JavaClasses/myname/practice. Class Printing imports class P. Both of these classes compile fine from ~/JavaClasses directory. However when I attempt to run the compiled class Printing like this
~/JavaClasses$ java myname/practice/Printing
I get the following errors:
Exception in thread "main" java.lang.NoClassDefFoundError: myname/practice/Printing (wrong name: Printing)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
The error is not related to myname.utils.P class but it happens because Printing class has no package definition. This means that the real full name of the class is Printing, but if you launch the java command from ~/JavaClasses folder, you are exactly stating that the full name of Printing class is myname.practice.Printing.
You have 2 options to fix this problem:
1) Declare the myname.practice package inside Printing class (I suggest this one)
2) Add the myname.practice folder to the classpath, in this way:
~/JavaClasses$ java -cp myname.practice:. Printing
The . represents the current directory, and it is necessary so you can still reference the myname.utils.P class from ~/JavaClasses folder.
Side note. At compile time you have no errors because probably you don't specify a classpath when you launch javac and so the current folder it is used. This way, myname.utils.P can be referenced without problem by every class, even by Printing.
This is because you are not using the package name. Yes when java gives the error and the package name for class not found, it uses /. However those should be replaced with .. since parts of the package name are always separated with a period and never with a slash. So if I had a package foo.bee. Then when I tried to run
java foo.bee
It would run currently and all, however if I tried
java foo/bee
it would complain about a class not def error.

"ClassFormatError: Incompatible magic value" when trying to run Java jar file

I typed in "java -jar ShowTime.jar", and got this error message:
Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 1347093252 in class file ShowTime
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
How should I fix this?
P.S. I have a mac.
A Java class should start with the magic (hex) value 0xCAFEBABE (neat huh). Your value 1347093252 is 0x504B0304 in hex, which happens to be the magic value for a ZIP file (first 2 bytes in ASCII are PK for Phil Katz, creator of the ZIP format). A jar is also a zipfile btw, so probably your jar is pretty much corrupt. Try rebuilding the entire project.
That usually means that you compiled the jar for a newer version of java than you ran it with. Check to see if you are using the same version of java to compile and run. If that doesn't fix the problem, please provide more info such as the compiler command and the output of java -version.
It means your application is using a jar file which is complied in a specific format in some other system. Now when you are trying to use the project in other system, the other system is not able to decode the jar, as each system generated a specific key to bundle the project.
Remote debug id the best way to find the issue that which jar is not in the proper format. Run the command in comment prompt for remote debug
java -agentlib:jdwp=transport=dt_socket,server=y,address=8001,suspend=n (command and parameter to run you jar)
what you need is, find the jar file and replace with another jar (may be a freshly downloaded one or if you teammate has the jar. Use it.)
The error i was getting because my project way having dependency with another project ABC, and ABC was using a generic jar. But project ABC was bundled with the magic key. I have replaced the jar used by ABC with a downloaded jar.
I got this error when the main class name of my jar was not identical to its filename.
For example, the main class of the jar was named Foo.java, and inside it I had public class Bar { ... } instead of public class Foo { ... }
It is probably not the most general answer, but making the file name and the class name identical solved this issue for me.

Categories