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.
Related
I'm following this sample to test purview SDK for java, it could be built successful, but when I run this java file ,it shows "Exception in thread "main" java.lang.NoClassDefFoundError: org/reactivestreams/Publisher". the class "DefaultAzureCredentialBuilder" was defined in core-identity jar and I already imported it ,not clear why it still failed and what class it required?
here is my code content
We get this error when the class is not available in the program at runtime but class was successfully compiled by the java compiler.
The reason why you are getting this error is because of jre unable to find DefaultAzureCredentialBuilder class.
As you mentioned in the post that this class is defined inside core-identity jar. whether the jar file may be missing. So, try to import it again and check.
I have this Problem. When i run my code in Intellij it works fine, but if i do an artifact and build the jar, it doesnt work. I think its caused by an extern library. Heres my output:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mindfusion/scheduling/Calendar
at GUI.<init>(GUI.java:75)
at Logfiles.main(Logfiles.java:13)
Caused by: java.lang.ClassNotFoundException: com.mindfusion.scheduling.Calendar
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 2 more
I know which Class it is but i dont know how to solve the Problem. Im really just a beginner. Could you please help me and explain it simple. Thank you
Edit:
After i build the artifact with extracted Libraries this Error comes : Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
This error simply means the class file is not present in the jar.
One possible solution is you can download jd-gui which is used to look at jars. You can use this to check if the class is present.
Another solution is you can grep search the class in the jar with this simple command.
grep -l "<class-name>" <jar-name>.jar
if the class is not present in the jar file. you can add the class using jar command.
jar -cvf <jar-absolute-location> <class-path>
eg : jar -cvf GUI.jar com.mindfusion.scheduling.Calendar
The easiest way to understand this issue, it to read the Javadoc for that class. From the Javadoc:
Thrown if the Java Virtual Machine or a ClassLoader instance tries to
load in the definition of a class (as part of a normal method call or
as part of creating a new instance using the new expression) and no
definition of the class could be found.
The searched-for class definition existed when the currently executing class was compiled,
but the definition can no longer be found.
That means that NoClassDefFoundError can be thrown when that particular class is present during compile time but somehow not available during runtime. This could be due to missing JAR file, permission issues, or incorrect classpath on runtime.
Normally I see these issues when developers neglect to define the classpath for used libraries. They forget that your IDE has its own file defining the classpath (i.e. Eclipse has the .classpath file) so running the application from the IDE works fine (class is present during compile time), but after the application is compiled and the classpath is not defined in the machine hosting the application, NoClassDefFoundError is thrown (class "missing" at runtime).
My suggestion is figured out first if the classpath is correct. More times than none, this is the issue. If the classpath is correct, make sure all permissions are set correctly.
http://docs.oracle.com/javafx/2/text/NeonSign.java.html
when I try to run the neonsign java example from oracle in intelliJ this is my error:
Exception in thread "main" java.lang.ClassNotFoundException: NeonSign
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)
https://docs.oracle.com/javase/7/docs/api/java/lang/ClassNotFoundException.html
public class ClassNotFoundException
extends ReflectiveOperationException
Thrown when an application tries to load in a class through its string name using:
The forName method in class Class.
The findSystemClass method in class ClassLoader .
The loadClass method in class ClassLoader.
but no definition for the class with the specified name could be found.
As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism. The "optional exception that was raised while loading the class" that may be provided at construction time and accessed via the getException() method is now known as the cause, and may be accessed via the Throwable.getCause() method, as well as the aforementioned "legacy method."
I believe this usually happens when you do not have the Library/jar in question added, and have to add it to your current project.
Please make sure you are Using Java 8 (1.8) or you have JavaFX added to your project.
In this case, after looking through the example I found this line of code
scene.getStylesheets().add(NeonSign.class.getResource("brickStyle.css").toExternalForm());
I wonder if the issue is because it's trying to get a resource that doesn't exist (brickStyle.css). If you remove this line, does the error persist?
This might also have to do with you not having JavaFX in your project.
Also, additional reading.
How do I resolve this Java Class not found exception?
Just to add, depending on if IDEA gives you a sample of an FX application, but Netbeans has a FXApplication project with "FXMainClass" that has a built in small example.
Good luck.
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...
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