Java类通常自己编译,但在通过JSP

时间:2018-05-30 02:50:12

标签: java android firebase jsp tomcat

我有一个成功向我的Android手机发送Firebase消息的java类。我试图从JSP文件运行该类,但它导致NoClassFoundException和NoClassDefFoundError。 这是JSP文件:

<%@ page import="send.Notify" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    Api token is: <%
        out.print(request.getParameter("token"));
    %>
<%

Notify.SendMessage(request.getParameter("token"),"title","body");
%>
</body>
</html>

要明确的是,当Notify.SendMessage被注释掉时,我从out.print获得正常输出,所以问题不在于输入。

这是我的班级:

package send;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

public class Notify {
public static void main(String[] args) throws Exception{

    SendMessage("f7ne...",
            "title", "body");
}

public static void SendMessage(String registrationToken, String title, String body) throws Exception{
    FileInputStream serviceAccount =
            new FileInputStream("C:\\Users\\michael\\IdeaProjects\\servlet 3\\web\\notification-32ebc-firebase-adminsdk-5lbs4-643271e91a.json");

    FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredentials(GoogleCredentials.fromStream(serviceAccount))
            .setDatabaseUrl("https://notification-32ebc.firebaseio.com")
            .build();

    FirebaseApp.initializeApp(options);

    Notification notification = new Notification(title, body);
    Message message = Message.builder()
            .setToken(registrationToken)
            .setNotification(notification)
            .build();


    String response = FirebaseMessaging.getInstance().send(message);
    System.out.println(response);
}
private static String getAccessToken() throws IOException {
    GoogleCredential googleCredential = GoogleCredential
            .fromStream(new FileInputStream("C:\\Users\\michael\\IdeaProjects\\servlet 3\\web\\notification-32ebc-firebase-adminsdk-5lbs4-643271e91a.json"))
            .createScoped(Arrays.asList("https://www.googleapis.com/auth/firebase.messaging",
                    "https://www.googleapis.com/auth/firebase.database",
                    "https://www.googleapis.com/auth/userinfo.email",
                    "https://www.googleapis.com/auth/firebase"));
    googleCredential.refreshToken();
    return googleCredential.getAccessToken();
}

}

这是错误:

Type Exception Report

Message An exception occurred processing [/notification.jsp] at line [15]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: An exception occurred processing [/notification.jsp] at line [15]

12:         /*Notify.SendMessage("f7neXi6q4KQ:APA91bFI3acBwgtDq99_D2duax_qY1zAMGCE62yHQ5CsL-UhyJdvAs97CUhzXzY0q7tyWKX2JM0WlyvZtL-arTW9s1useO816ujda5c4gYKM-I_uCN8m81EH9clwNeVG6kQzGU-zl93k",
13:                 "jsp title", "jsp body");*/
14:         try{
15:             Notify.SendMessage(request.getParameter("token"),"title","body");
16:         }catch (Exception e){
17:             out.print("error!");
18:         }


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:593)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:467)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/google/firebase/FirebaseOptions$Builder
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:667)
    org.apache.jsp.notification_jsp._jspService(notification_jsp.java:154)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

java.lang.NoClassDefFoundError: com/google/firebase/FirebaseOptions$Builder
    send.Notify.SendMessage(Notify.java:26)
    org.apache.jsp.notification_jsp._jspService(notification_jsp.java:135)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

java.lang.ClassNotFoundException: com.google.firebase.FirebaseOptions$Builder
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1292)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1121)
    send.Notify.SendMessage(Notify.java:26)
    org.apache.jsp.notification_jsp._jspService(notification_jsp.java:135)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.

这是我的文件夹结构: https://prnt.sc/jocl6h

2 个答案:

答案 0 :(得分:0)

问题在于Intellij不会将您添加到项目中的依赖项也用于tomcat服务器。为了解决这个问题,我手动将我的maven文件夹(C:\ Users \ user.m2)中的所有jar文件添加到project / web / WEB-INF / lib文件夹中。

这是一个解决方案,但我仍在寻找合适的方法。

更新:我最近试图重构我的程序。在IntelliJ中,从apache webapp原型创建一个新的maven项目。然后,您可以添加所有依赖项和类,Tomcat将成功找到所有这些。

答案 1 :(得分:0)

类路径中缺少firebase-core jar。 firebase的文档包含gradle的说明。

dependencies { implementation 'com.google.firebase:firebase-core:16.0.1' }

如果您正在使用maven,请使用相应的maven声明替换依赖项

<dependency> <groupId>com.google.firebase</groupId> <artifactId>firebase-core</artifactId> <version>16.0.1</version> </dependency>