java , eclipse jar problem - java

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.

Related

NoClassDefFoundError when working with Hibernate

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 .

Java - JOGL deploy: NoClassDefFoundError

Dev env: ItelliJ 14 -> compiles and runs fine.
When executing my custom created .jar from command line this appears
C:\Java\Projects\OpenGLES\out\artifacts\Test1>java -jar Test1.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/jogamp/opengl/G
ventListener
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 sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.jogamp.opengl.GLEventListener
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)
... 13 more
I'm trying to create .jar artifact, that I could just double click start.
I think that the problem means that this class cannot find some resources. The JOGL/JogAmp official sites gives explanation on deployment of .jars with JOGL, but I think that I missed something.
The Test1.jar has following structure:
Test1.jar/
--com/
----company/
------Main.class
--jar/
----gluegen-rt.jar
----gluegen-rt-natives-windows-amd64.jar
----gluegen-rt-natives-windows-i586.jar
----jogl-all.jar
----jogl-all-natives-windows-amd64.jar
----jogl-all-natives-windows-i586.jar
--lib/
----windows-amd64/
-------gluegen-rt.dll
-------joal.dll
-------jocl.dll
-------jogl_cg.dll
-------jogl_deskop.dll
-------jogl_mobile.dll
-------nativewindow_awt.dll
-------nativewindow_win32.dll
-------newt.dll
-------soft_oal.dll
--META-INF/
----MANIFEST.MF
--com
--jar
--lib
--META-INF
manifest.mf contains -
Manifest-Version: 1.0
Main-Class: com.company.Main
By the stack trace it seems that the JVM cannot find the class com.jogamp.opengl.GLEventListener that means it can't resolve the path to the included jar files. The path of the jar files is not correct. Follow the link to see how to package jars correctly.
https://docs.oracle.com/javase/tutorial/deployment/jar/

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.

Java programs cannot run after downloading opencsv

I downloaded the opencsv (from http://sourceforge.net/projects/javacsv/files/latest/download) and since then I have been getting this error with all my Java programs
java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
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)
Exception in thread "main"
How can I fix this? I realized I didn't need the opencsv anyway...how can I remove it so that my programs can compile and run as usual? I'm using Eclipse Version: 1.4.2.20120213-0813
You can remove a plugin by going to Help->About (or Eclipse->About on a Mac), choosing installations details, selecting the plugin you wish to remove, and choosing 'Uninstall'.

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.

Categories