2

Trying to use Maven to organize my project and I keep running into the following error. I know that this error means the file is present at compile time but for some reason it can't be found at runtime.

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException

So I'm working on a project in Java that will take a user query, search Google Images and then download some of the results onto my computer. To that end I've had to use some third party libraries like JSoup, Json-Simple, and Gson.

I initially added Jsoup to my classpath manually and it worked, but then I heard about Maven and started using it instead. My issue is that when I try to run my code I get the error above.

I'm just not sure how to resolve this. I've seen a bunch of other posts about similar errors and I've tried to modify my pom.xml accordingly but I just can't get it to work. I've tried removing the ~/.m2 file, ran mvn clean, mvn install, mvn package, mvn compile, and it all works fine. But when it comes time to run, I keep getting that error.

Here's most of my pom.xml file.

<repositories>
    <repository>
    <id>central</id>
   <name>Maven repository</name>
   <url>http://repo1.maven.org/maven2</url>
 </repository>

<dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>

<dependency>
  <!-- jsoup HTML parser library @ https://jsoup.org/ -->
  <groupId>org.jsoup</groupId>
  <artifactId>jsoup</artifactId>
  <version>1.11.3</version>
  <scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>

<build>
<plugins>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <mainClass>fully.qualified.MainClass</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
  </plugin>
</plugins>
</build>

FOUND SOLUTION: So I left out some parts of the pom.xml file to make it easier to read, and because all the other parts were generated by Maven itself so I figured there couldn't be an issue with anything there. But it adds a tag called "pluginManagement" that encloses all other plugins and apparently this does not allow the Shade plugin to run.

Between ngueno's guidance and this post Maven shade plugin is not called automatically for goal "package" I was able to figure out my issue, though I'm still not entirely sure why it is an issue. Anyways, I figured I'd update this post in case someone else with a similar problem stumbles across it. This was on Mac OS btw, in case it makes a difference. Thanks for your help everyone.

skippy130
  • 303
  • 3
  • 12
  • Are you able to run it on your IDE and the issue happens only when you export the JAR? – nortontgueno Jan 15 '19 at 18:32
  • I'm running this on a macOS terminal. I do have NetBeans installed but I'm not using it for this. – skippy130 Jan 15 '19 at 18:35
  • So you are running the generated JAR only? – nortontgueno Jan 15 '19 at 18:39
  • Misunderstood, yes, I'm running the JAR generated by Maven. I go into the target/classes directory and type in the command "java com.mycompany.App" to run the program. This has worked fine for a simpler program I made using Maven as practice a little earlier. – skippy130 Jan 15 '19 at 18:44
  • THANKS! The tag was the issue in my case. For background: Eclipse automatically updated to 2022-09 and then I started seeing this NoClassDef error, when I previously didn't have it. The strange thing is that before that Eclipse updated I was already using such tag... somehow the 2022-09 broke something in the way... thanks a lot again :) – DarkCygnus Oct 24 '22 at 22:26

2 Answers2

1

Usually NoClassDefFound errors are related to missing libraries at runtime.

Since you are running using the terminal I supose you are building your project using Maven, and running the generated JAR,

I would recommend to you to use the maven-shade-plugin and generate an uber-jar as I explained on this question.

The purpose generating a uber-jar is to carry all the needed dependencies inside of it (available on the application classpath).

Implement the plugin and try to run using the new JAR.

PS: Remember to check this section related to Executable Jars

UPDATE: Remove the <scope>provided</scope> of your jsoup dependency, to enforce Maven to package it along your app, with the provided scope you are saying that this dependency will be provided by the JDK at runtime.

nortontgueno
  • 2,553
  • 1
  • 24
  • 36
  • Thank you. I tried it just now, but unfortunately I get the same exact error. After adding the plugin, I did rm ~/.m2, mvn install, mvn clean, mvn compile, and then ran the JAR it generated. – skippy130 Jan 15 '19 at 18:58
  • Also remove the `provided` of the `jsoup` dependency to enforce the inclusion in the JAR – nortontgueno Jan 15 '19 at 18:59
  • No luck unfortunately – skippy130 Jan 15 '19 at 19:04
  • Would you mind decompile the JAR and check if the dependencies were generated accordingly inside of it? – nortontgueno Jan 15 '19 at 19:05
  • I can try but honestly I’m not sure how I would go about doing that. If you have a guide I’d be more than happy to try. – skippy130 Jan 15 '19 at 19:11
  • Ok so there was a bit of a misunderstanding before. Up till now I've been trying to compile the source files, not the JAR. Looking at the JAR file specfically, when I try to compile it using "java -jar jarFileName.jar" it says there is no manifest. Looking around online it seems to me that the shade plugin is not creating an executable JAR file for me. Any ideas as to why that might be? – skippy130 Jan 15 '19 at 21:33
1

The jars that you identify in your dependencies must be present in the Runtime classpath. This is not the classpath that is available when you compile the code; it is the classpath on the host where you run the application.

You must install these jars on the target host.

Edit: More details

You must do the following:

  1. Identify the runtime host.
  2. Create a directory on the runtime host into which you will install the dependent jar files.
  3. Include every jar in the classpath.

Consider abandoning the "roll-your-own" path. If you use Spring Boot (I like it, I don't work for them). One feature of spring boot is a reinvented "Fat Jar" that will include the dependencies inside one deliverable artifact (the fat jar) and will add them to the classpath at startup.

Edit:
The Spring boot executable jar file is not a "Fat Jar", instead it includes the dependencies in a directory in the executable jar and adds said jars to the classpath on startup.

DwB
  • 37,124
  • 11
  • 56
  • 82
  • How would I install these on the runtime compile path? I’ve only ever modified the CLASSPATH environment variable, but I thought maven made that unnecessary? – skippy130 Jan 15 '19 at 19:33