1

I have a servlet which makes a connection to SQL server, gets resultset, and sends a string to the Android client. Untill here it is all fine.

Now I woull like to send objects to the client, so I understood I need to use JSON. I added the jar of JSON to the build path of the servlet and of the client. .Eclipse recognized it and I complied successfully.

This is in the servlet part:

List<String> myListOfStrings = new ArrayList<String>(); 
myListOfStrings.add("first word");
myListOfStrings.add("second word");
myListOfStrings.add("third word");

JSONArray arrOfJSON=new JSONArray();          
for (String s : myListOfStrings){
   arrOfJSON.put(s);
}
//Here I serialize the stream to a String.
final String output = arrOfJSON.toString();
response.setContentLength(output.length());
//And write the string to output.
response.getOutputStream().write(output.getBytes());
response.getOutputStream().flush();
response.getOutputStream().close();

The problem is on RUN-time:

java.lang.ClassNotFoundException: org.json.JSONArray
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at Servlet2ForLNM_Pack.Servlet2ForLNM_Class.doPost(Servlet2ForLNM_Class.java:1374)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

May someone help me to understand where did I go wrong?

Brett Walker
  • 3,566
  • 1
  • 18
  • 36
Eliran
  • 59
  • 1
  • 8
  • Can you describe you deployment procedure. Are you using Maven or some other build system? It is not so much about compiling but rather about how you package and deploy. – Brett Walker May 03 '15 at 12:51

2 Answers2

0

This error arised because java is unable to get the classes used by you. Download java-json.jar file.

You have to follow following steps

1) You have to add the jar file of json in the java path bin and lib

2) you also need to add the jar file of json in server lib folder.

Restart the server the problem will be solved

Pranav Agrawal
  • 466
  • 6
  • 17
0
  1. Check that your jar is placed in /WebContent/WEB-INF/lib
  2. Tell eclipse to include the jar in war file when building the project source

I suggest that you move to maven build system. Maven allows you to configure whole project from single file(no eclipse configuration required).

You run mvn clean install from project root directory to build war file. Your pom.xml would look like this

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>yourGroupId</groupId>
    <artifactId>yourArtifactId</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <!-- this is important, other libs can be excluded -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
            <version>3.0.1</version>
        </dependency>
        <!-- your json lib -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20141113</version>
        </dependency>
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.8.7</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>your app name</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Community
  • 1
  • 1
gkiko
  • 2,283
  • 3
  • 30
  • 50
  • Allright, your second paragraph "Tell eclipse to include the jar in war file when building the project" solved it. I had to go to Deployment assembly and add the jar there TOO. Thank you! – Eliran May 03 '15 at 14:49