Java HTTP 状态 500 - Servlet 执行引发异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25615874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:47:37  来源:igfitidea点击:

HTTP Status 500 - Servlet execution threw an exception

javaweb-servicesservletsjenasemantic-web

提问by Ravi Vyas

I am getting this error while calling a java class in servlet.

在 servlet 中调用 java 类时出现此错误。

Here is the error.

这是错误。

type Exception report

message Servlet execution threw an exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

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

root cause

java.lang.NoClassDefFoundError: com/hp/hpl/jena/query/QueryFactory
    com.complexible.common.csv.MuseumData.gts(MuseumData.java:109)
    com.complexible.common.csv.MuseumData.ass(MuseumData.java:89)
    com.complexible.common.csv.MuseumRDF.doGet(MuseumRDF.java:48)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

java.lang.ClassNotFoundException: com.hp.hpl.jena.query.QueryFactory
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    com.complexible.common.csv.MuseumData.gts(MuseumData.java:109)
    com.complexible.common.csv.MuseumData.ass(MuseumData.java:89)
    com.complexible.common.csv.MuseumRDF.doGet(MuseumRDF.java:48)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.55 logs.

Here is a code of my java class which will get the record from a RDF file using SPARQL and will return the output in JSON format in the console, then after i would catch the output and convert it into the string hence i could use that string into the servlet.

这是我的 java 类的代码,它将使用 SPARQL 从 RDF 文件中获取记录,并将在控制台中以 JSON 格式返回输出,然后在我捕获输出并将其转换为字符串之后,因此我可以使用该字符串进入 servlet。

package com.complexible.common.csv;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.NoClassDefFoundError;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class MuseumData {
     public static void main( String[] args ) throws IOException {


         String output=getrec();
         System.out.println(output);
         }


     public static String getrec() throws IOException 
        {
         String spr="prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"+ 
                 "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"+
                    "prefix crm:  <http://www.cidoc-crm.org/cidoc-crm/>\n"+        
                    "prefix owl: <http://www.w3.org/2002/07/owl#>\n"+
                     "prefix xsd: <http://www.w3.org/2001/XMLSchema#>\n"+
                     "prefix crm: <http://www.cidoc-crm.org/rdfs/cidoc_crm_v5.0.4_official_release.rdfs#>\n"+       
                 "\n"+
                     //?title ?person ?type instead of *
                 "SELECT  * WHERE   { <http://phdprototype.tk/collectionimage/4D0BFF17-5810-4644-A550-D35EE090D4A8.png>"+
                     "crm:P3_has_note ?title ; "+
                 "crm:E21_Person ?person ;"+
                     "crm:E62_String ?type ;"+
                 "crm:P82_at_some_time_within ?year;"+
                     "crm:P33_used_specific_technique ?material;}";

          Query query = QueryFactory.create(spr);

         Model model = ModelFactory.createDefaultModel();

         File f = new File("J:/Museum/data.rdf");
         model.read(new FileInputStream(f), "RDF/XML");

         //model.write(System.out, "TTL");

         final QueryExecution exec = QueryExecutionFactory.create(query, model);
         //ResultSetFormatter.out(System.out, exec.execSelect(), query);


         ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(baos);
            // IMPORTANT: Save the old System.out!
            PrintStream old = System.out;
            // Tell Java to use your special stream
            System.setOut(ps);
            // Print some output: goes to your special stream
            ResultSetFormatter.outputAsJSON(System.out, exec.execSelect());
            // Put things back
            System.out.flush();
            System.setOut(old);
            // Show what happened

           // return baos.toString();

            String gcr=baos.toString();
               return gcr;

        }

}

Here is a servlet code which will display the output in a browser.

这是一个 servlet 代码,它将在浏览器中显示输出。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    PrintWriter out= response.getWriter();
    String rec=MuseumData.getrec();
    out.print(rec);


}

采纳答案by FoggyDay

Find the .jar for com/hp/hpl/jena/query/QueryFactory and put it in WEB-INF lib in your .war file.

找到 com/hp/hpl/jena/query/QueryFactory 的 .jar 并将其放入 .war 文件的 WEB-INF 库中。

Here's how to do it if you happened to be developing your servlets in the Eclipse IDE:

如果您碰巧在 Eclipse IDE 中开发 servlet,请执行以下操作:

回答by Keerthivasan

You have missed the jarfile containing the class com.hp.hpl.jena.query.QueryFactoryin your classpath. Please add it in your package / classpath

您错过了jar包含类com.hp.hpl.jena.query.QueryFactory路径中的类的文件。请将它添加到您的包/类路径中

回答by Sujay

HTTP Status 500 - Servlet execution threw an exception

HTTP 状态 500 - Servlet 执行引发异常

As I was getting the same servlet exception error.. While upgrading from tomcat 6 to 8 .

因为我遇到了相同的 servlet 异常错误..从 tomcat 6 升级到 8 时。

Issue has beem resolved after added odbc6.jar in tomcat home lib and web-inf lib.

在 tomcat home lib 和 web-inf lib 中添加 odbc6.jar 后,问题已得到解决。

回答by Julio Cesar Brito Gomes

Issue has beem resolved after added odbc6.jar in tomcat home lib and web-inf lib.

在 tomcat home lib 和 web-inf lib 中添加 odbc6.jar 后,问题已得到解决。