-->

Heroku, Java, Procfile, Could not find or load mai

2019-02-19 03:35发布

问题:

I'm using heroku with maven to run a server. My goal is to have heroku run the java class server.class as a web dyno.

How would I write a procfile to execute the java program server.class as web?


My current Procfile

web: java -cp $JAVA_OPTS target/classes/v1/a1/server

My error.(From heroku logs)

Picked up JAVA_TOOL_OPTIONS: -Xmx350m -Xss512k -Dfile.encoding=UTF-8
Error: Could not find or load main class target.classes.v1.a1.server
State changed from starting to crashed


Possibly useful information

The procfile

web: java -cp $JAVA_OPTS target/classes/v1/a1/*

Returns

Error: Could not find or load main class target.classes.v1.a1.myOtherClass

My original Procfile(Also didn't work)

web: java -cp target/classes/:target/dependency/* server
  • My file structure is a bit different than the example given in the heroku docs so I modified the procfile a bit.
  • My dependencies are not inside /target/dependencies.
  • My classes are inside target/classes/v1/a1/.
  • server.java has a main method and valid constructor method.
  • All my dependencies seem to be in order.
  • Maven does builds my .java files into .class files in the target directory.
  • I'm on unix so quotes and semicolons probably won't work.

回答1:

I think your Procfile should contain:

web: java -cp target/classes/:target/dependency/* v1.a1.server

This assumes the following:

  • Your server class is in the file target/v1/a1/server.class
  • The Java code for your server class includes package v1.a1;
  • Your class name and file name are lowercase.

A few problems I noticed in your earlier attempts included:

  • You are passing $JAVA_OPTS to the -cp options (incorrect)
  • You are using / instead of . in the fully qualified class name (incorrect)
  • You are including the target dir in the fully qualified class name (incorrect)

The files in the target/classes/ and target/dependency/ directory belong on the classpath (i.e. passed to -cp) while the last argument to the java command should be the fully qualified class name (in the form package.Class).



回答2:

The default Procfile for heroku is written for Linux. Where the separator used is ":".

To use the Procfile on Windows machines modify the Procfile as below

web: java -cp target/classes/;target/dependency/* com.yourpackage.MainClassName


回答3:

I forgot a bit of my POM.

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals><goal>copy-dependencies</goal></goals>
                </execution>
            </executions>
        </plugin>

And modified my Procfile a bit

web: java $JAVA_OPTS -cp target/classes/:target/dependency/* v1.a1.server