-1

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:

JAR contents

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.

apgsov
  • 794
  • 1
  • 8
  • 30
  • I guess HttpClient is not found in the classpath because it is contained in another jar file. There is a question answering that at https://stackoverflow.com/questions/12357136/reference-jars-inside-a-jar. Here is the official JAR documentation: https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html – carlosvin Dec 16 '19 at 18:35
  • @carlosvin This works with the Eclipse Class Loader. – dan1st Dec 16 '19 at 19:50

2 Answers2

0

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.

Sambit
  • 7,625
  • 7
  • 34
  • 65
  • I don't think this is the issue as I've seen similar manifest files using JarRsrcLoader which list them the way I have in my manifest file. – apgsov Dec 16 '19 at 19:33
0

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.

apgsov
  • 794
  • 1
  • 8
  • 30