0

I'm working on a project where I've to deliver .jar file to the user. I'm using Intellij IDEA and as build system I'm using Maven. I've added json-simple as a dependency.

<dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
</dependency>

My project works fine while I run from my IDE, but while I try to run my jar file from command line java -jar project.jar it shows the following error

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException
    at utils.PropertiesFile.readProperties(PropertiesFile.java:82)
    at utils.PropertiesFile.<init>(PropertiesFile.java:46)
    at NewApplication.<clinit>(NewApplication.java:26)
Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.ParseException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 3 more

I've other dependencies added to my pom.xml, jar file runs from the terminal without the json-simple dependency. I've found many same types of questions but all saying that json-simple is not added to build path but my problem with only jar file. While other dependencies work fine with the jar file but what problem with this simple-json?

My project structure is here and configuration for building jar enter image description here

Shahrear Bin Amin
  • 1,075
  • 13
  • 31
  • 1
    How do you build your `project.jar`? It should be a fat jar including other dependencies of your project along with `json-simple-1.1.1.jar` – Nowhere Man Jun 14 '20 at 17:52
  • @AlexRudenko Do I have to manually add `json-simple-1.1.1.jar` as It's in pom.xml file isn't is expected to add automatically? I've added a screenshot of building jar artifacts. Can you please check? – Shahrear Bin Amin Jun 14 '20 at 18:34
  • Your screenshot is not helpful because it does not show all the dependencies. You should use some maven plugin [as explained here](https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven). – Nowhere Man Jun 14 '20 at 18:48
  • 1
    @AlexRudenko thanks. It worked. – Shahrear Bin Amin Jun 14 '20 at 20:15
  • Does this answer your question? [How can I create an executable JAR with dependencies using Maven?](https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) – Nowhere Man Jun 14 '20 at 20:19
  • Yes your provided link answers question and worked. But the thing is why my previous jar file without `json-simple` was working I'm not clear. Although `json-simple` is not also showing in my screenshot dependencies. – Shahrear Bin Amin Jun 14 '20 at 21:06

0 Answers0