0

I have a problem.

I write a Java console app in intelliJ. I add the maven package org.json.json and if i run the project in IntelliJ everything works.

If I start the class in the console with java Main I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject

I have try and search a lot but nothing works

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
Ezak
  • 137
  • 1
  • 3
  • 14
  • What do you do with the class in IntelliJ? IntelliJ doesn't mask the Maven commands you're running; you would need to reproduce those in the terminal, too. You also fail to mention what you've looked at, and what errors you got when it didn't work. – Makoto May 25 '17 at 20:31
  • @HovercraftFullOfEels: Yes, the canonical is correct, but more of a mathematician's answer at this point. Maven is doing that heavy lifting for the end user, but the answer to "add it to the classpath" would only confuse at best. – Makoto May 25 '17 at 20:31
  • another question. What is the simplest way to use org.json.JSONObject in my Class witj Intellij? – Ezak May 25 '17 at 20:36
  • Looks you are missing the jar for Json in your Runtime. – Yohannes Gebremariam May 25 '17 at 20:40
  • You're missing the json library from your classpath on the console. You can also package up your console application as a fat jar using something like [sbt-assembly](https://github.com/sbt/sbt-assembly), if you wish to avoid the need for adding dependencies manually on console versions of your app. – ManoDestra May 25 '17 at 20:48
  • Possible duplicate of [Why am I getting a NoClassDefFoundError in Java?](https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java) – ManoDestra May 25 '17 at 20:48

2 Answers2

1

The maven-shade-plugin is a fancy plugin with plenty of options. For creating simpler jars containing jars I use the maven-assembly-plugin. Also verify that the scope within the org.json.json dependency is set to compile.

The details are documented at: https://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#jar-with-dependencies

For example:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <id>jar-with-dependencies</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>your.main.class.package.your_main_class</mainClass>
            </manifest>
        </archive>
      </configuration>
    </execution>
  </executions>
</plugin>
Kailash
  • 527
  • 4
  • 13
0

Just running a class from a console is not enough. You also need to set classpath to point to all of its dependencies including json jars.

Since you are using Maven, my advice is to use its "maven-shade-plugin" in order to create one large jar that has your class and all of json jar classes and run your class with "-jar" option.

tsolakp
  • 5,858
  • 1
  • 22
  • 28