Roger Stark
Roger Stark

Reputation: 163

java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException with eclipse and spring

I have a simple XML file that I have parsed to JSON. All is fine and dandy, I have a Java class that is stand alone (i.e. it has a public static void main (String args[])....)

This has a private constructor (because I need to call it with Strings either a filename or the actual data). So I have two methods that return an instance of the object. I know a bit of Java as you can tell.

OK. When I run the code in Eclipse that runs the main method my file is loaded and decoded as required. It also works for a raw String that I run via JUnit.

So I know the following facts -

  1. the parsing of a static String works and decodes perfectly
  2. if I provide a file it is loaded and decoded correctly.

Now the issue:

As soon as I run it in Spring framework I can write to standard out the entire file content that I have run via the stand alone code.

But before it can run anything at all I get the below error -

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:920)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

The stand alone code is run in Eclipse, and the Spring is run pointing to that code using Tomcat 7.

Why is it not finding the ParsException correctly?

The imports in the calling Spring controller are

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.commons.lang.StringUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

Is their a way of altering the build order, and would that fix it?

Upvotes: 7

Views: 54603

Answers (9)

Abhishek Thomas
Abhishek Thomas

Reputation: 314

Just add maven dependency of JSON simple in your pom.xml. No need to configure any classpath.

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

Upvotes: 0

Sathiamoorthy
Sathiamoorthy

Reputation: 11580

Use the below java library for process JSONArray and JSONObject

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20200518</version>
</dependency>

Upvotes: 0

luiscalmeida
luiscalmeida

Reputation: 1

IF you are making an AI in Spring as I was when I had this error:

You also need to add the Jar to your AI "jlib" folder, for instance "...\AI\Skirmish\MyAI\0.1\jlib".

It's not enough to add it in Eclipse or Netbeans and build the project. Spring must have the jar too.

I know it is a late answer it but can help other people with the same error.

Upvotes: 0

Akhil KC
Akhil KC

Reputation: 76

I have had a similar error and spent 3 4 hours removing and adding new jar files. What my problem was that i added jar file directly from the download location ie THE ZIP files ..!!! after adding a proper extracted jar file , My problem seems to be resolved. Hope this helps someone..!!

Upvotes: 0

Gyanesh Sharma
Gyanesh Sharma

Reputation: 481

  1. Add json-simple-1.1.1.jar file in your build path. On Eclipse, right click on project-> build path -> configure build path -> java build path -> add external jars -> select the jar file.

  2. Add build path entry into deployment assembly. On the same properties window, Select "deployment assembly" option. Then add "Java Build Path" entries. This should show you the build path entry you just made in step one.

Upvotes: 7

Roger Stark
Roger Stark

Reputation: 163

The solution that I used in the end was to change to sourceforge.net json parser, a couple of code tweaks and my JUnit tests still worked and Tomcat did not complain. This may not be the best solution but it worked.

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

Upvotes: 0

Anugoonj
Anugoonj

Reputation: 575

Looks like you have missed including the json-simple-.jar in your classpath. Include the same and it should get solved.

Hope this helps !

Upvotes: 8

R H
R H

Reputation: 397

Could you please add this jar json-simple-1.1.1.jar in your classpath and try it out ? This jar can be downloaded at http://code.google.com/p/json-simple/downloads/detail?name=json-simple-1.1.1.jar

Upvotes: 1

user529543
user529543

Reputation:

you are missing a jar deployment.

it is related for Tomcat7. A few jar needed to be exported ( forgot which ones) other web server doesn't need but Tomcat.

Upvotes: 0

Related Questions