gpt4 book ai didi

java - 为部署到 heroku tomcat 的 Java Web WAR 找不到添加依赖项的类

转载 作者:行者123 更新时间:2023-11-28 23:56:56 25 4
gpt4 key购买 nike

按照这里的教程:https://devcenter.heroku.com/articles/java-webapp-runner,我更改了 POM 以包含几个依赖项和我的 Web 应用程序的源代码(servlet),以利用 init() 方法中添加的依赖项来加载一份文件。我使用 webapp-runnermvn heroku:run-war 在本地测试了该应用程序,servlet 按预期工作!但是,当我使用 mvn heroku:deploy-war 部署到 Heroku 并在完成部署后导航到该链接时,当我第一次打开该链接时出现以下错误:

HTTP Status 500 - Servlet.init() for servlet HelloWorld threw exception

type Exception report

message Servlet.init() for servlet HelloWorld threw exception

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

exception

javax.servlet.ServletException: Servlet.init() for servlet HelloWorld threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

root cause

java.lang.ExceptionInInitializerError
org.nd4j.nativeblas.NativeOpsHolder.<init>(NativeOpsHolder.java:16)
org.nd4j.nativeblas.NativeOpsHolder.<clinit>(NativeOpsHolder.java:9)
org.nd4j.linalg.cpu.nativecpu.ops.NativeOpExecutioner.<init>(NativeOpExecutioner.java:37)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:423)
java.lang.Class.newInstance(Class.java:442)
org.nd4j.linalg.factory.Nd4j.initWithBackend(Nd4j.java:5267)
org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:5183)
org.nd4j.linalg.factory.Nd4j.<clinit>(Nd4j.java:167)
org.deeplearning4j.models.embeddings.loader.WordVectorSerializer.loadTxt(WordVectorSerializer.java:1578)
org.deeplearning4j.models.embeddings.loader.WordVectorSerializer.loadTxtVectors(WordVectorSerializer.java:1502)
herokutest.HelloWorld.init(HelloWorld.java:41)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

root cause

java.lang.RuntimeException: ND4J is probably missing dependencies. For more information, please refer to: http://nd4j.org/getstarted.html
org.nd4j.nativeblas.NativeOps.<clinit>(NativeOps.java:39)
org.nd4j.nativeblas.NativeOpsHolder.<init>(NativeOpsHolder.java:16)
org.nd4j.nativeblas.NativeOpsHolder.<clinit>(NativeOpsHolder.java:9)
org.nd4j.linalg.cpu.nativecpu.ops.NativeOpExecutioner.<init>(NativeOpExecutioner.java:37)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:423)
java.lang.Class.newInstance(Class.java:442)
org.nd4j.linalg.factory.Nd4j.initWithBackend(Nd4j.java:5267)
org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:5183)
org.nd4j.linalg.factory.Nd4j.<clinit>(Nd4j.java:167)
org.deeplearning4j.models.embeddings.loader.WordVectorSerializer.loadTxt(WordVectorSerializer.java:1578)
org.deeplearning4j.models.embeddings.loader.WordVectorSerializer.loadTxtVectors(WordVectorSerializer.java:1502)
herokutest.HelloWorld.init(HelloWorld.java:41)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

root cause

java.lang.UnsatisfiedLinkError: no jnind4j in java.library.path
java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
java.lang.Runtime.loadLibrary0(Runtime.java:870)
java.lang.System.loadLibrary(System.java:1122)
org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:727)
org.bytedeco.javacpp.Loader.load(Loader.java:502)
org.nd4j.nativeblas.NativeOps.<clinit>(NativeOps.java:37)
org.nd4j.nativeblas.NativeOpsHolder.<init>(NativeOpsHolder.java:16)
org.nd4j.nativeblas.NativeOpsHolder.<clinit>(NativeOpsHolder.java:9)
org.nd4j.linalg.cpu.nativecpu.ops.NativeOpExecutioner.<init>(NativeOpExecutioner.java:37)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:423)
java.lang.Class.newInstance(Class.java:442)
org.nd4j.linalg.factory.Nd4j.initWithBackend(Nd4j.java:5267)
org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:5183)
org.nd4j.linalg.factory.Nd4j.<clinit>(Nd4j.java:167)
org.deeplearning4j.models.embeddings.loader.WordVectorSerializer.loadTxt(WordVectorSerializer.java:1578)
org.deeplearning4j.models.embeddings.loader.WordVectorSerializer.loadTxtVectors(WordVectorSerializer.java:1502)
herokutest.HelloWorld.init(HelloWorld.java:41)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

root cause

java.lang.UnsatisfiedLinkError: no nd4j in java.library.path
java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
java.lang.Runtime.loadLibrary0(Runtime.java:870)
java.lang.System.loadLibrary(System.java:1122)
org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:727)
org.bytedeco.javacpp.Loader.load(Loader.java:493)
org.nd4j.nativeblas.NativeOps.<clinit>(NativeOps.java:37)
org.nd4j.nativeblas.NativeOpsHolder.<init>(NativeOpsHolder.java:16)
org.nd4j.nativeblas.NativeOpsHolder.<clinit>(NativeOpsHolder.java:9)
org.nd4j.linalg.cpu.nativecpu.ops.NativeOpExecutioner.<init>(NativeOpExecutioner.java:37)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:423)
java.lang.Class.newInstance(Class.java:442)
org.nd4j.linalg.factory.Nd4j.initWithBackend(Nd4j.java:5267)
org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:5183)
org.nd4j.linalg.factory.Nd4j.<clinit>(Nd4j.java:167)
org.deeplearning4j.models.embeddings.loader.WordVectorSerializer.loadTxt(WordVectorSerializer.java:1578)
org.deeplearning4j.models.embeddings.loader.WordVectorSerializer.loadTxtVectors(WordVectorSerializer.java:1502)
herokutest.HelloWorld.init(HelloWorld.java:41)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

个人资料

web:    java $JAVA_OPTS -jar target/dependency/webapp-runner.jar --port $PORT target/*.war

POM.xml

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.tomcat.heroku</groupId>
<artifactId>heroku-tomcat</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>heroku-tomcat Maven Webapp</name>
<url>http://maven.apache.org</url>


<properties>
<nd4j.version>0.6.0</nd4j.version>
<dl4j.version>0.6.0</dl4j.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.deeplearning4j</groupId>
<artifactId>deeplearning4j-ui</artifactId>
<version>${dl4j.version}</version>
</dependency>

<dependency>
<groupId>org.deeplearning4j</groupId>
<artifactId>deeplearning4j-nlp</artifactId>
<version>${dl4j.version}</version>
</dependency>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native</artifactId>
<version>${nd4j.version}</version>
</dependency>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-common</artifactId>
<version>${nd4j.version}</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>



</dependencies>


<build>
<finalName>heroku-tomcat</finalName>
<plugins>

<plugin>
<groupId>com.heroku.sdk</groupId>
<artifactId>heroku-maven-plugin</artifactId>
<version>1.1.1</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>copy</goal></goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.github.jsimone</groupId>
<artifactId>webapp-runner</artifactId>
<version>8.0.30.2</version>
<destFileName>webapp-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>


</project>

源代码:

package herokutest;

import org.apache.commons.lang.math.NumberUtils;
import org.bytedeco.javacpp.presets.opencv_core;
import org.deeplearning4j.models.embeddings.loader.WordVectorSerializer;
import org.deeplearning4j.models.embeddings.wordvectors.WordVectors;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Collection;

/**
* Created by Mr_RexZ on 10/23/2016.
*/
public class HelloWorld extends HttpServlet {

private String message;
String loc="/WEB-INF/glove.6B.50d.txt";


@javax.ws.rs.core.Context
ServletContext context;
WordVectors wordVectors;

public void init(final ServletConfig config) throws ServletException
{
message = "Hi";

String fullPath = config.getServletContext().getRealPath(loc);
File file = new File(fullPath);
try {
wordVectors = WordVectorSerializer.loadTxtVectors(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{

response.setContentType("text/html");

boolean submitSim = request.getParameter("submitSim") != null;
boolean submitAn = request.getParameter("submitAn") != null;


String findSim = request.getParameter("findSim");
request.setAttribute("findSim", findSim);

String resSim = request.getParameter("resSim");
request.setAttribute("resSim", resSim);
if (request.getParameter("numSim")!=null && NumberUtils.isNumber(request.getParameter("numSim"))) {
int numSim = Integer.parseInt(request.getParameter("numSim"));
request.setAttribute("numSim", request.getParameter("numSim"));

if(submitSim) {
Collection<String> lst = wordVectors.wordsNearest(findSim, numSim);
request.setAttribute("resSim", Arrays.toString(lst.toArray()));
}

}
String an1 = request.getParameter("an1");
String an2 = request.getParameter("an2");
String an3 = request.getParameter("an3");
request.setAttribute("an1", an1);
request.setAttribute("an2", an2);
request.setAttribute("an3", an3);

String resAn = request.getParameter("resAn");
request.setAttribute("resAn", resAn);

if (request.getParameter("numAn")!=null && NumberUtils.isNumber(request.getParameter("numAn"))) {
int numAn = Integer.parseInt(request.getParameter("numAn"));
request.setAttribute("numAn", request.getParameter("numAn"));

if (submitAn) {
Collection<String> lst = wordVectors.wordsNearest(Arrays.asList(new String[] {an2,an3}) , Arrays.asList(new String[] {an1}),numAn);
request.setAttribute("resAn", Arrays.toString(lst.toArray()));
}
}
request.getRequestDispatcher("/index.jsp").forward(request, response);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}

public void destroy()
{
// do nothing.
}
}

但是,当我删除额外的依赖项时,网络应用程序可以正常工作。据我了解,使用 web-app runner 在本地测试应用程序以测试该应用程序是否可以运行足以查看它在部署到 Heroku 时是否也可以正常运行,因为它可以防止运行时问题。但我不明白为什么它在增加依赖性的情况下不起作用?

最佳答案

您的潜在问题实际上与nd4j 中的c++ 代码有关。我假设您已经有一个经过训练的模型,这意味着您应该相对熟悉 nd4j 后端 ( http://nd4j.org/backend.html )。如果是这种情况,请确保将带有最新版本 nd4j 的 nd4j-native-platform 添加到您的类路径中。

Nd4j 原生平台会将各种平台的所有原生库捆绑在一起。

如果出于任何原因这不起作用,您可以采用分类器路线。

读这个: What is the purpose of Classifier tag in maven?

你会在这里看到: http://repo1.maven.org/maven2/org/nd4j/nd4j-native/0.6.0/

查看 jar 文件的名称,您会看到 nd4j-native-SOME CLASSIFIER HERE

由于您在 Linux 上进行部署,您可能需要:linux-x86_64.jar

这是 OS-CHIP_ARCHITECTURE,你还会看到 mac、windows、powerpc 和 android

此时为 0.6.0。

dl4j 示例将始终具有最新版本:github.com/deeplearning4j/dl4j-examples

关于java - 为部署到 heroku tomcat 的 Java Web WAR 找不到添加依赖项的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40207591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com