Heroku "could not find or load main class" error

Miltios :

First off, I'm aware there are dozens of duplicates to this question; I've been reading them for days. None of the solutions have worked so far. :(

I've written a webapp that runs fine locally, and also tested packaging it in a .war to make sure it runs elsewhere. But once I set it up on Heroku (via github integration) I get the dreaded "Error: Could not find or load main class com.charplanner.swtor.StartClass".

I checked on the server, and confirmed that StartClass.class is definitely there in /target/classes/com/charplanner/swtor/.

Here is my procfile:

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

My pom.xml is kinda long, so I'll trim out the dependencies:

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

And lastly, here's the full server log if anyone's still reading this far:

    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
Miltios :

Looks like I needed to fix three things. First, add the maven-dependency-plugin to my pom.xml to populate the target/dependency folder:

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

Second, use that same plugin to include webapp-runner.jar (which provides the actual main class to bootstrap the server):

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

And third, edit the procfile to point to webapp-runner using the Heroku recommended settings:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=162997&siteId=1