gpt4 book ai didi

java - Heroku "could not find or load main class"错误

转载 作者:行者123 更新时间:2023-11-30 12:05:02 26 4
gpt4 key购买 nike

首先,我知道这个问题有几十个重复;我已经读了好几天了。到目前为止,所有解决方案都没有奏效。 :(

我已经编写了一个在本地运行良好的网络应用程序,并且还测试了将其打包在 .war 中以确保它在其他地方运行。但是一旦我在 Heroku 上设置它(通过 github 集成),我就会得到可怕的“错误:无法找到或加载主类 com.charplanner.swtor.StartClass”。

我检查了服务器,确认 StartClass.class 确实在/target/classes/com/charplanner/swtor/中。

这是我的程序文件:

web: java -cp target/classes/:target/dependency/* com.charplanner.swtor.StartClass

我的 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.charplanner</groupId>
<artifactId>swtor</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>swtor Maven Webapp</name>
<url>http://charplanner.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
...
</dependencies>

<build>
<finalName>swtor</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<attachClasses>true</attachClasses>
<!--<webXml>target/web.xml</webXml>-->
<webResources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>TomcatServer</server>
<path>/swtor</path>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

最后,如果有人还在读到这里,这里是完整的服务器日志:

    2019-06-19T23:24:40.563135+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -XX:CICompilerCount=2 -Dfile.encoding=UTF-8 
2019-06-19T23:24:40.718167+00:00 app[web.1]: Error: Could not find or load main class com.charplanner.swtor.StartClass
2019-06-19T23:24:43.330499+00:00 heroku[web.1]: Starting process with command `java -cp target/classes/:target/dependency/* com.charplanner.swtor.StartClass`
2019-06-19T23:24:44.893457+00:00 heroku[web.1]: State changed from starting to crashed
2019-06-19T23:24:44.873708+00:00 heroku[web.1]: Process exited with status 1
2019-06-19T23:24:44.714263+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2019-06-19T23:24:44.717823+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -XX:CICompilerCount=2 -Dfile.encoding=UTF-8
2019-06-19T23:24:44.817063+00:00 app[web.1]: Error: Could not find or load main class com.charplanner.swtor.StartClass

最佳答案

看来我需要解决三件事。首先,将 maven-dependency-plugin 添加到我的 pom.xml 以填充目标/依赖文件夹:

<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-dependency-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
</dependency>

其次,使用相同的插件来包含 webapp-runner.jar(它提供实际的主类来引导服务器):

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

第三,使用 Heroku 推荐的设置编辑 procfile 以指向 webapp-runner:

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

关于java - Heroku "could not find or load main class"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56677373/

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