NoClassDefFoundError when working with Hibernate - java

I am struggling for a couple hours with one problem regarding the initial setup of Hibernate for Eclipse. More precisely I am getting this error and i looked up anywhere for a solution. If anyone could help me with this one i would kindly appreciate it!
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at com.hibernate.test.Main.main(Main.java:16)
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)

You're getting the error because the hibernate libraries are not available to Tomcat. In your picture, below the hibernate library set there is an empty set called 'Web App Libraries' - this is the set your hibernate libraries need to be in.
Right click your project -> Build Path -> Configure Build Path, and remove the hibernate set from the build path. Now import the jars into the WEB-INF/lib folder. Refresh your project and now you should see them listed in the 'Web App Libraries' set (i appreciate this is somewhat annoying that you have to import them into your code base - someone else might know a better way to do this that doesn't involve copying the jars in)

there is problem your maven dependency. if you are using hibernate annotations, you should add hibernate-annotations jar

If you are using maven , then there is some diffrence in versions of hibernate jars ,
Please clean all jars from maven directory and then again let maven download the jars .
I hope this solves your problem .

Related

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/env/EnvironmentCapable

I am trying to create a simple spring boot application from the STS starter project template.
So I create using the STS wizard as below:
File->New->Spring Starter Project
* Type: Maven
* Packaging: jar
* Language: java *ver:1.8*
* Spring Boot version: 1.5.8
* Selected dependencies:
-> Actuator
-> JPA
-> H2
-> Web
And this is my project structure after it finishes:
Now I don't change anything in the project, I simply try to run the main class as a Java Application but I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/env/EnvironmentCapable
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at io.yassir.springboot.SbCdCollectionApplication.main(SbCdCollectionApplication.java:10)
Caused by: java.lang.ClassNotFoundException: org.springframework.core.env.EnvironmentCapable
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 13 more
I have found other questions with the same issue, but none of the answers worked for me because I didn't even configure the project, it's spring's pre-configuration.
I resolved the problem by deleting maven local repository at ~\.m2\repository and then re-setting eclipse configuration to default.
This issue is caused by local jars being corrupted, or invalid eclipse build settings.

Modify org.eclipse.jdt.core.dom.ASTPArser

I need to modify org.eclipse.jdt.core.dom.ASTParser class which is in jdt.core jar file. So I created a java project and I created the same package (org.eclipse.jdt.core.dom). but when I want to create a new instance of this class I get this error:
Exception in thread "main" java.lang.SecurityException: class
"org.eclipse.jdt.core.dom.ASTParser2"'s signer information does not
match signer information of other classes in the same package at
java.lang.ClassLoader.checkCerts(Unknown Source) at
java.lang.ClassLoader.preDefineClass(Unknown Source) at
java.lang.ClassLoader.defineClass(Unknown Source) at
java.security.SecureClassLoader.defineClass(Unknown Source) at
java.net.URLClassLoader.defineClass(Unknown Source) at
java.net.URLClassLoader.access$100(Unknown Source) at
java.net.URLClassLoader$1.run(Unknown Source) at
java.net.URLClassLoader$1.run(Unknown Source) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) at
sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) at
typeResolution.TypeResolution.main(TypeResolution.java:80)
When I run my code in a plugin mode project I don't have problem, but in a java stand alone project I am getting this error.
Can anyone give me insight into this problem? Thanks.
The original artifact org.eclipse.jdt.core_xyz.jar is signed. When you try to use that jar in combination with your own modified class the JVM will detect that the integrity of the signed jar is broken.
You have two options:
instead of replacing just one file, replace the entire jar with your
own version
re-package the original jar without signing
Depending on your configuration there may be more ripples, because other plug-ins like org.eclipse.jdt.compiler.tool contribute classes to packages that also exist in org.eclipse.jdt.core ("split packages"), which is OK only if both jars are signed by with the same certificate - or not signed at all.

How to add external jar

I have added an external jar(for jar Visit jarlink) on my java project but I do not have all features of that library.
I added jar following this way click left on my project and properties->Java Build Path->Libraries tabs->Add External Jar.
Is this the right way ? or I am doing it wrong?
for example link Visit mkyong.com/java/java-find-location-using-ip-address I am trying to do this example .I have added that jar on my project but some imports do not work
I have added geoip2-0.7.0.jar (import com.maxmind.*) this import is working but (import com.maxmind.geoip.regionName;) this import is not working
errors : Exception in thread "main" java.lang.NoClassDefFoundError: ServerLocation
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: ServerLocationenter code here
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more
The most natural way to do this, is to create a lib folder, put your libs in there, then add the JARs to the build path.
If you're using Maven, you just need to specify the dependency groupId and artifactId, and nothing else.
To add external jar,
if you use eclipse find out here.
If you generate a project using maven, Add a dependency in pom.xml. Find sample here.

NoClassDefFoundError: scala/ScalaObject in mixed Scala/Java project

Background: I am a C# developer, but my next project needs to be on the JVM. I have researched Scala and I think that it lets me to what I can in C# (Lambdas, inferred types, functional-style, etc). I would classify myself as a very novice Java developer... especially when it comes to using the IDE's, etc.
I have a Scala project that seems to work...
I made a Java project and referenced the Scala project
The editor seems to know about the classes in the Scala project (typeahead, etc)
When I try to run the Java project, I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at learning_scala.java.Main.main(Main.java:12)
Caused by: java.lang.ClassNotFoundException: scala.ScalaObject
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Clearly something has gone awry in the Java/Scala compilation, but I have no idea how to resolve it (my lack of Java experience being the primary culprit here)
I'm using Eclipse w/ the official Scala plugin... all of which are up to date as of 2-3 days ago.
Thanks
You need to add the scala-library.jar library to your classpath. You can do that a few different ways but the simplest is to copy the file into your project (drag and drop it into your eclipse package explorer). Next, right click on the file and select add to build path. That adds the scala jar to your classpath. That should fix the problem.

java , eclipse jar problem

i'm kind of new to java. I use eclipse and i imported some jar files with some classes by going to properties -> Add External Jars;
The problem is that when i try to use a class from the jar i get the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: nextapp/echo2/app/event/ActionListener
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at Program.main(Program.java:12)
Caused by: java.lang.ClassNotFoundException: nextapp.echo2.app.event.ActionListener
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 13 more
The class is supposed to create a window,
i guess that the jar has to import somehow other stuff or something like that,
How can i fix this?
PS. Sorry for the bad code , i don't know tags here.
thansk,
Raxvan
Most jars you download will include 0-5 other jars that are needed. They all need included in order to use the jar. Very quickly you end up with 20 jars in a simple 1000 line Java project, but thems are the breaks.
Look inside your downloaded jar zip. Should be a lib directory or such with all the libs you need.
Another way to get around this would be to use a tool like Ivy or Maven to manage downloading dependencies. Depending on what libraries you are using, you may have many transitive dependencies, and doing that all by hand will end up taking a lot of time.

Categories