I've little experience with Java and JAR files. I've downloaded a JAR file provided to me which seems to contain all of the necessary dependencies, but when I try to run java -jar MyJar.jar, it throws the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:420)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:56)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.HttpClient
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:435)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 3 more
The JAR file itself seems to contain all of the necessary dependencies as outlined in the MANIFEST.MF file:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ amqp-client-5.6.0.jar jackson-core-2.9.8.jar jacks
on-databind-2.9.8.jar jackson-annotations-2.9.0.jar jackson-dataforma
t-yaml-2.9.8.jar snakeyaml-1.23.jar slf4j-api-1.7.5.jar slf4j-log4j12
-1.7.5.jar log4j-1.2.17.jar httpclient-4.5.8.jar httpcore-4.4.11.jar
commons-logging-1.2.jar commons-codec-1.11.jar
Class-Path: .
Rsrc-Main-Class: TRECISExternalClient
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
I was wondering if there's something I'm missing or if it possible that something is malformed. Happy to provide more information if needed.
You can check below the link for complete implementation of the class org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
https://github.com/U-Alberta/exemplar/blob/master/src/org/eclipse/jdt/internal/jarinjarloader/JarRsrcLoader.java
Refer to line no 47 if (rsrcPath.endsWith("/")), I think you have to separate each jar with ./. Besides I see there is a blank space in this line after slash / in Rsrc-Class-Path: ./ amqp-client-5.6.0.jar. It should be Rsrc-Class-Path: ./amqp-client-5.6.0.jar. HttpClient is already available as per the image.
Turns out the issue wasn't with the JAR file itself but rather that I had multiple versions of OpenJDK on my system. Purging other versions and reinstalling OpenJDK v1.8.0 (openjdk-8-jdk on Ubuntu repos) solved the issue.
Related
I have a problem when I run my Java application for a FIX application. I have added all the QuickFIX/J jars to build the FIX application. But when I run my jar I always get this error:
Application: TestMarketdataRequest
Exception in thread "main" java.lang.NoClassDefFoundError: quickfix/MessageStoreFactory
at com.dxtr.fastmatch.FastmatchMDRequest.<init>(FastmatchMDRequest.java:14)
at com.dxtr.fastmatch.FastmatchMDRequest.main(FastmatchMDRequest.java:19)
Caused by: java.lang.ClassNotFoundException: quickfix.MessageStoreFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 2 more
My question: How do I fix this and why do I get this error when I did add QuickFIX/J in my depedencies?
Include the dependency jar in your classpath when you run the main class using the -cp option:
java -cp quickfixj.jar MainClass
-classpath classpath
-cp classpath
Specifies a list of directories, JAR files, and ZIP archives to search for class files. Separate class path entries with semicolons (;). Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.
More on the docs.
this is closed. i have add executable jar in my pom. so it is closed right now thanks all
I'm trying to compile and run the PercolationVisualizer in https://github.com/samet/Coursera-Algorithms-I-Assignment-1. According to http://introcs.cs.princeton.edu/java/stdlib/, the required dependencies StdDraw.java and In.java should be present in the stdlib package.
After cloning the repository, I copied stdlib.jar to the src directory and then compiled PercolationVisualizer using the following command:
javac -cp .:stdlib.jar PercolationVisualizer.java
(I did the same for Percolation.java). However, when I try to run the program using java PercolationVisualizer, I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: In
at PercolationVisualizer.main(PercolationVisualizer.java:62)
Caused by: java.lang.ClassNotFoundException: In
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
I don't understand the NoClassDefFoundError for In, since it should be included in stdlib. Can anyone explain this behavior?
If you are on Windows, use the semicolon as CLASSPATH separator ..
javac -cp ".;stdlib.jar" PercolationVisualizer.java Percolation.java WeightedQuickUnionUF.java PercolationStats.java
If you are on linux/*nix, use the colon as CLASSPATH separator ...
javac -cp ".:stdlib.jar" PercolationVisualizer.java Percolation.java WeightedQuickUnionUF.java PercolationStats.java
For more information look here
If you want to debug the process of compilation, use the "-verbose" flag. It will display you CLASSPATH, the "lodaing jar_file ..." statements. If it has not recognized your jar file, it will now show the "loading" statement.
I finally unjar'd stdlib.jar into the working directory, after which the compilation worked. (This is indeed the first method to use stdlib described in http://introcs.cs.princeton.edu/java/stdlib/).
I am currently trying to make a Jar with all my libraries included. What I have done, I have created folders like this :
eancopy (which contain all my classes)
lib (containing all my libraries : mongo, jedis...)
META-INF/MANIFEST.MF
My main class is named process (within the package eancopy)
My manifest is like this:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Class-Path: lib\commons-pool-1.5.6.jar lib\jedis-2.0.0.jar lib\mongo-2.6.3.jar
Created-By: 1.6.0_24-b07 (Sun Microsystems Inc.)
Main-Class: eancopy.process
I have generated the JAR with this command :
jar cvmf META-INF/MANIFEST.MF EANcopy.jar eancopy/*.class lib/*.jar
My problem : When executing the JAR by java -jar EANcopy.jar, it works when it is executed in the place where I have generated the JAR but in another place I have the message :
Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/DBObject
Caused by: java.lang.ClassNotFoundException: com.mongodb.DBObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
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)
Could not find the main class: eancopy.process. Program will exit.
Any ideas ? Thanks
This exception shows you the cause of the problem:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/DBObject
You haven't specified these libs lib\commons-pool-1.5.6.jar lib\jedis-2.0.0.jar lib\mongo-2.6.3.jar in your CLASSPATH environment variable, hence why it fails when run in other places.
I have created my jar file in the following folder:
/usr/local/bin/niidle.jar
And I have one jarfile which is in the following folder
/Projects/EnwelibDatedOct13/Niidle/lib/hector-0.6.0-17.jar
And this file 'hector-0.6.0-17.jar' I have to include in MANIFEST.MF in jar.
And when I mention class path in MANIFEST.MF as follows:
Manifest-Version: 1.0
Main-Class: com.ensarm.niidle.web.scraper.NiidleScrapeManager
Class-Path: /Projects/EnwelibDatedOct13/Niidle/lib/hector-0.6.0-17.jar
When I run this using command:
java -jar /usr/local/bin/niidle.jar
It works properly..
But I dont want to give full Class-Path name, I have to give Class-Path as follows:
Manifest-Version: 1.0
Main-Class: com.ensarm.niidle.web.scraper.NiidleScrapeManager
Class-Path: lib/hector-0.6.0-17.jar
And when I run this using command:
java -jar /usr/local/bin/niidle.jar
It is showing error message:
Exception in thread "main" java.lang.NoClassDefFoundError: me/prettyprint/hector/api/Serializer
at com.ensarm.niidle.web.scraper.NiidleScrapeManager.main(NiidleScrapeManager.java:21)
Caused by: java.lang.ClassNotFoundException: me.prettyprint.hector.api.Serializer
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 1 more
Please tell me solution for that...
I see at least two solutions:
store hector-0.6.0-17.jar inside niidle.jar and use a relative path in Class-Path. E.g. Class-Path: lib/hector-0.6.0-17.jar (for niidle.jar/lib/hector-0.6.0-17.jar)
java -cp /Projects/EnwelibDatedOct13/Niidle/lib/hector-0.6.0-17.jar -jar /usr/local/bin/niidle.jar
Create a lib folder in the folder where you are running the command, put hector-0.6.0-17.jar in it and try running the command again.
I've run in to a strange error with the javamail 1.4.2 api and jdk/jre 1.6.0u16. I've placed the unzipped javamail-1.4.2 folder (along with the MySQL Connector-J 5.1.7 also needed) in both the jdk and jre folders as in the %classpath% below.
.;"C:\Program
Files\Java\jdk1.6.0-16\jre\lib\ext\mysql-connector-java-5.1.7-bin";"C:\Program Files\Java\jdk1.6.0-16\jre\lib\ext\javamail-1.4.2\mail.jar";"C:\Program
Files\Java\jdk1.6.0_16\bin";"C:\Program
Files\GameLabInventory\GameLabInventory-7.0.jar";"C:\Program
Files\Java\jre6\lib\ext\mysql-connector-java-5.1.7-bin";"C:\Program Files\Java\jre6\lib\ext\javamail-1.4.2\mail.jar";
But when I run my compiled jar with the command
C:\Program Files\Java\jre6\bin>java -cp %classpath% -jar "C:\Program Files\GameLabInventory\GameLabInventory_7.0.jar"
I get the following error message:
Exception in thread "main"
java.lang.NoClassDefFoundError:
javax/mail/MessagingException
at gamelab.inventory.GameLabInventory.main(GameLabInventory.java:62)
Caused by:
java.lang.ClassNotFoundException:
javax.mail.MessagingException
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)
... 1 more
I can't find anything wrong with my setup here, hopefully you guys can help me out. Thanks!
Here is the manifest file:
Manifest-Version: 1.0
Main-Class: gamelab.inventory.GameLabInventory
Here is the jar -tvf (link)
According to the documentation for the java tool:
When you use [the -jar] option, the
JAR file is the source of all user
classes, and other user class path
settings are ignored.
You can specify a Class-Path attribute in yourJAR file that "specifies the relative URLs of the extensions or libraries that [your] application or extension needs."
When using -jar, java overrides the classpath setting (-cp). In this configuration, however, classpath jars can be specified in the manifest file of your jar.
I've experienced a similar issue (not identical to yours, since mine is spring-related) and found this link was helpful:
http://forum.springsource.org/showthread.php?t=65840&page=3
It seems that implementation changes in the JDK might be causing this behaviour. Quoting from the link:
"I managed to reproduce the problem on my Mac development box using Java 6 (1.6.0_07). I then tried it on my Mac using Java 5, and could not reproduce the problem. Crucially, in the Java 5 case, the JVM would be using the javax.activation API and implementation from the 1.1.1 javax activation bundle, whereas on Java 6, it would be using them from the JRE (anything that's part of the JRE takes precedence over what's in dm Server's lib directory). I then tried the app on an Ubuntu box, this time using Java 6 1.6.0_10. I could not reproduce the problem."
and/but:-
"I've just had this same problem "No object DCH for MIME type" but on Ubuntu java version "1.6.0_13". So I don't know how you guys solved it by upgrading to 1.6.0_11.
What I did is copy the mailcab.default to my own bundle and it worked. Seems to be a classloading problem which I don't fully understand yet."