Heroku“找不到或加载主类”错误

时间:2019-06-20 00:03:07

标签: java maven heroku

首先,我知道这个问题有很多重复。我已经读了好几天了。到目前为止,没有一种解决方案有效。 :(

我编写了一个在本地运行良好的webapp,还测试了将其打包到.war中以确保其在其他地方运行的情况。但是,一旦我在Heroku上(通过github集成)对其进行设置,就会出现可怕的“错误:找不到或加载主类com.charplanner.swtor.StartClass”。

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

这是我的procfile:

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

1 个答案:

答案 0 :(得分:0)

好像我需要修复三件事。首先,将maven-dependency-plugin添加到我的pom.xml中以填充target / dependency文件夹:

<!-- 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