1

I am trying to execute a jar file, in terminal.

Jar created in intellij:

Build -> Build Artifacts -> Clean
Build -> Build Artifacts -> Build

Execution cmd:

java -jar helloworld.jar

Error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONObject
        at main.java.com.example.helloworld.sampleclass.mymethod(sampleclass.java:53)
        at main.java.com.example.helloworld.sampleclass.main(sampleclass.java:14)
Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONObject

I have added the following dependency also.

Pom.xml Dependency:

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

The code executes perfectly when I try to run in intellij, but I am getting this error when I run with the jar. Any suggestions would be great.

data_person
  • 4,194
  • 7
  • 40
  • 75
  • This question have already been answered here [See more](https://stackoverflow.com/questions/250166/noclassdeffounderror-while-trying-to-run-my-jar-with-java-exe-jar-whats-wron) – SoT Jun 09 '20 at 04:28

3 Answers3

1

Ensure that you have correctly configured the artifact in (File -> Project Structure -> Artifacts) settings. Most likely, you have not specified the libraries that should be added to the class-path in the Output Layout tab.

I can assume that you created an artifact before adding a dependency. Try to remove the current artifact configuration and create a new one. IntelliJ IDEA will prepare a jar file with all necessary dependencies based on one of the following settings.

See the links below to learn how to manage Artifacts in IntelliJ IDEA:

Egor Klepikov
  • 3,386
  • 5
  • 19
  • 34
1

i know, it's not very intuitive, but maven doesn't actually embed your dependencies into your jar, unless you explicitly ask for it. If you open your jar like a zip (because jar files are essentially zip files with class files in them), you'll see that the class that you see an error about is actually not there.

Personally, rather than using the intellij export (I'm not sure if it just builds the jar the same way maven does), I would use maven directly with mvn package command (optionally mvn package -DskipTests if you don't want to execute your unit tests every time you build your jar). mvn package will build the jar and put it somewhere int the ./target directory.

And then, unless you want to specify the path to your jar dependencies in the command line every single time you run your app, you also will want to use the maven's shade plugin. The shade plugin unzips all the dependencies that you specify in the pom.xml and puts them into your new jar along with the code of your own application: http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html

Sorry, I know it gets a little bit complicated here, but if you want to have an executable while using maven dependencies, shading is your best option.

Kamil Janowski
  • 1,872
  • 2
  • 21
  • 43
1

In my case I had to extract the maven library to output root.

Project settings(Ctrl + Alt + Shift + S) -> Artificats -> Output Layout -> Move element to output root.

enter image description here

desertSniper87
  • 795
  • 2
  • 13
  • 25