【问题标题】:HTTP Status 500 - Error when trying to invoke method from java fileHTTP 状态 500 - 尝试从 java 文件调用方法时出错
【发布时间】:2017-11-09 22:11:18
【问题描述】:

我正在尝试使用 tomcat 8.5.23 运行应用程序,但我一遍又一遍地收到相同的错误。每次尝试启动 tomcat 并打开本地主机时,我都会不断收到 HTTP 状态 500 错误。 这是错误:org.apache.jasper.JasperException:在第 [19] 行处理 JSP 页面 [/index.jsp] 时发生异常。这是我的文件(index.jsp 和 IntagramWB.java)

<html>    
<head>
<meta charset="UTF-8">
<meta name="description" content="Web Services">
<meta name="keywords" content="JSP, Bootstap, HTML5">

<!-- including the jQuery file to the bootstrap -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- including the bootstrap file -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

</head>
<title> My Restful API</title>
<body>
    <%@ page import = "com.mywebservices.InstagramWB" %>

    <% InstagramWB insta = new InstagramWB(); %>
    <% String details = insta.getDetails(); %>


    <script>
        var name = JSON.parse("<%= details %>");
            document.getElementById("name").innerHTML = details.data.full_name;

    </script>

    <h1 id = "name"> </h1>
    <h2>Last place: </h2>
</body>
</html>

我正在尝试从 java 类中调用一个方法。这是我的java文件:

package com.mywebservices;

import java.util.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class InstagramWB
{
    public InstagramWB()
    {}

public static String getDetails()
{
    // Variable for the access token
    String accessToken = "269891473.1677ed0.a60ed893764d4c5";
    String replyInsta = "";
    String jsonInsta = "";
    URL url;
    HttpURLConnection connection = null;
    InputStream is = null;

    try
    {
        // Endpoint to get the details of the user
        String endpoint = "https://api.instagram.com/v1/users/self/?access_token="+accessToken;

        // establishing the connection to the endpoint
        url = new URL(endpoint);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();
        is = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

        while ((replyInsta = reader.readLine()) != null)
        {
            jsonInsta = replyInsta;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return jsonInsta;
}
}

如果需要,这也是我的文件夹结构。

和例外情况:

【问题讨论】:

  • 你编译部署你的类了吗?它必须在WEB-INF/classes 文件夹中。

标签: java jsp tomcat


【解决方案1】:

查看堆栈跟踪的“根本原因”部分。实际错误是 java.lang.NoClassDefFoundError: InstagramWB

基本上,当你在第 19 行做...

InstagramWB insta = new InstagramWB();

...它找不到 InstagramWB 的 .class 文件。

您确定您的战争文件已正确创建,并且 InstagramWB 类在其中可用吗?尝试使用任何解压缩实用程序解压缩战争文件,例如Winzip、WinRAR 等,并检查是否可以在其中找到此类的 .class 文件。

【讨论】:

  • 一旦我放入 /tomcat/webapps 并启动 tomcat,war 文件就会自动解压缩。我检查了它,.class 文件在那里。
  • 好吧,无论出于何种原因,类加载器都无法找到它,这就是您收到此错误的原因。请参阅这篇关于 Tomcat 中的类路径和类加载的文章。这可能会为您提供一些进一步调试原因的指导:mulesoft.com/tcat/tomcat-classpath
猜你喜欢
  • 1970-01-01
  • 2014-12-24
  • 2012-05-09
  • 2013-01-25
  • 1970-01-01
  • 2017-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多