NoClassDefFoundError:org / json / JSONArray

时间:2017-01-10 18:41:26

标签: java json maven tomcat intellij-idea

我正在尝试拨打网络服务。访问https://developers.google.com/identity/protocols/OAuth2InstalledApp#refresh时会触发此异常,说明 NoClassDefFoundError:org / json / JSONArray ,而我已将此依赖项包含在我的pom.xml中。 War是通过tomcat 8.0.36部署的,它是在intellij的Tomcat运行配置中设置的。到目前为止我尝试了什么:

  • 使用不同版本的org.json
  • 清理/安装pom.xml
  • 从/Users/cemalonder/Library/Caches/IntelliJIdea2016.3/tomcat/
  • 清除maven缓存
  • 从/Users/cemalonder/.m2/删除所有版本的org.json并重新安装
exception

javax.servlet.ServletException: Servlet execution threw an exception
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NoClassDefFoundError: org/json/JSONArray
    rest.service.GetAuthorsService.doGet(GetAuthorsService.java:53)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
<?xml version="1.0" encoding="UTF-8"?>
<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>groupId</groupId>
    <artifactId>MyBookHistory</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0-b01</version>
        </dependency>

        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
        </dependency>

    </dependencies>

</project>
package rest.service;
import org.json.JSONArray;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

/**
 * Created by cemalonder on 09/01/2017.
 */
@WebServlet("/getauthors")
public class GetAuthorsService extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // objects that will be used for response
        PrintWriter out = resp.getWriter();

        // request - response configurations
        resp.setContentType("applictaion/json; charset=UTF-8");
        resp.addHeader("Access-Control-Allow-Origin", "*");
        resp.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");

        // data configuration
        Integer authorSize = 6;

        String[] authorImages = { "http://www.hayalperdesi.net/images/haberler/1168.jpg",
                                  "https://farzimuhaldotcom.files.wordpress.com/2012/01/nfk.jpg",
                                  "http://kitaplikkedisi.com/wp-content/uploads/2014/11/William-Burroughs-014.jpg",
                                  "http://www.fakirelma.com/files/uploads/news/default/orhan-pamuk-yazarlar-af9a1b2321937079ac42.jpg",
                                  "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRp8gM20DyWSr489aVssuXyVrRbYLf5HO8PTaWyD1EZ7iCdFaVD4w",
                                  "http://hepsi10numara.com/wp-content/uploads/2013/02/iyi-yazarlar.jpg"};

        String[] authorNames = {"name1", "name2", "name3", "name4", "name5", "name6", "name7"};

        String[] authorBio = {"randomString1",
                              "randomString2",
                              "randomString3",
                              "randomString4",
                              "randomString5",
                              "randomString6",
                              "randomString7"};

        /* create JSONArray for multiple author information. Than for each author
           create a JSONObject. */

        JSONArray authors = new JSONArray();
        for (int i = 0; i < authorSize; i++) {
            JSONObject author = new JSONObject();
            author.put("authorImage", authorImages[new Random().nextInt(authorImages.length)]);
            author.put("authorName", authorNames[new Random().nextInt(authorNames.length)]);
            author.put("authorBio", authorBio[new Random().nextInt(authorBio.length)]);
            authors.put(author);
        }

        out.print(authors);

        if (out != null) try {
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Tomcat正在部署

/Users/cemalonder/Development/Libraries/apache-tomcat-8.0.36/bin/catalina.sh run
[2017-01-10 09:56:28,048] Artifact MyBookHistory:war exploded: Server is not connected. Deploy is not available.
10-Jan-2017 21:56:30.100 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.0.36
10-Jan-2017 21:56:30.102 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Jun 9 2016 13:55:50 UTC
10-Jan-2017 21:56:30.103 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.0.36.0
10-Jan-2017 21:56:30.103 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Mac OS X
10-Jan-2017 21:56:30.103 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            10.12
10-Jan-2017 21:56:30.103 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          x86_64
10-Jan-2017 21:56:30.103 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /Library/Java/JavaVirtualMachines/jdk1.8.0_74.jdk/Contents/Home/jre
10-Jan-2017 21:56:30.103 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_74-b02
10-Jan-2017 21:56:30.104 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
10-Jan-2017 21:56:30.104 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /Users/cemalonder/Library/Caches/IntelliJIdea2016.3/tomcat/Tomcat_MyBookHistory
10-Jan-2017 21:56:30.104 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /Users/cemalonder/Development/Libraries/apache-tomcat-8.0.36
10-Jan-2017 21:56:30.105 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/Users/cemalonder/Library/Caches/IntelliJIdea2016.3/tomcat/Tomcat_MyBookHistory/conf/logging.properties
10-Jan-2017 21:56:30.105 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
10-Jan-2017 21:56:30.108 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote=
10-Jan-2017 21:56:30.108 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099
10-Jan-2017 21:56:30.108 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false
10-Jan-2017 21:56:30.109 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.authenticate=false
10-Jan-2017 21:56:30.109 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=127.0.0.1
10-Jan-2017 21:56:30.110 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
10-Jan-2017 21:56:30.110 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.endorsed.dirs=/Users/cemalonder/Development/Libraries/apache-tomcat-8.0.36/endorsed
10-Jan-2017 21:56:30.110 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/Users/cemalonder/Library/Caches/IntelliJIdea2016.3/tomcat/Tomcat_MyBookHistory
10-Jan-2017 21:56:30.110 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/Users/cemalonder/Development/Libraries/apache-tomcat-8.0.36
10-Jan-2017 21:56:30.111 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/Users/cemalonder/Development/Libraries/apache-tomcat-8.0.36/temp
10-Jan-2017 21:56:30.112 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/cemalonder/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
10-Jan-2017 21:56:30.594 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
10-Jan-2017 21:56:30.716 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
10-Jan-2017 21:56:30.723 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
10-Jan-2017 21:56:30.725 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
10-Jan-2017 21:56:30.725 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1788 ms
10-Jan-2017 21:56:30.774 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
10-Jan-2017 21:56:30.774 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.36
10-Jan-2017 21:56:30.784 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
10-Jan-2017 21:56:30.792 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
10-Jan-2017 21:56:30.794 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 68 ms
Connected to server
[2017-01-10 09:56:31,273] Artifact MyBookHistory:war exploded: Artifact is being deployed, please wait...
[2017-01-10 09:56:31,946] Artifact MyBookHistory:war exploded: Artifact is deployed successfully
[2017-01-10 09:56:31,946] Artifact MyBookHistory:war exploded: Deploy took 673 milliseconds
10-Jan-2017 21:56:40.794 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /Users/cemalonder/Development/Libraries/apache-tomcat-8.0.36/webapps/manager
10-Jan-2017 21:56:40.857 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /Users/cemalonder/Development/Libraries/apache-tomcat-8.0.36/webapps/manager has finished in 63 ms

1 个答案:

答案 0 :(得分:0)

好的,我明白了。似乎Intellij的maven配置存在问题。我把catalina基地目录更改为catalina home,现在可以使用了。

添加此(指向与Dcatalina.home相同的方向)

  

-Dcatalina.base = /用户/ cemalonder /开发/库/ Apache的Tomcat的8.0.36

作为参数运行/调试配置

enter image description here