Search code examples
javagitherokushadowjar

Heroku Java app crashing locally but not on the web


I have my main class:

public class Main {

    public static void main(String[] args){
        Spark.port(getHerokuAssignedPort());
        get("/hello", (req, res) -> "Hello Heroku World");
    }



    private static int getHerokuAssignedPort() {
        ProcessBuilder processBuilder = new ProcessBuilder();
        if (processBuilder.environment().get("PORT") != null) {
            return Integer.parseInt(processBuilder.environment().get("PORT"));
        }
        return 4567; //return default port if heroku-port isn't set (i.e. on localhost)
    }

}

My procfile:

web: java -jar build/libs/CrowdSubhaHeroku-1.0-SNAPSHOT-all.jar

Note: I'm using shadowJar since a normal jar creation doesn't include dependencies I need such as Spark and Firebase.

Now, if I do:

heroku local web

And go to localhost:5000, I get a 404 error. However, the application doesn't actually crash. It stays running.

That is unlike when I do:

heroku open

After a git add ., git commit -m "x", and a git push heroku master

Which crashes with an "Application Error" and gives me this ONLY:

2017-02-23T15:18:18.848727+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=crowdsubha.herokuapp.com request_id=ce636951-862e-4b2f-a698-924db3039a07 fwd="94.187.7.67" dyno= connect= service= status=503 bytes=
2017-02-23T15:18:20.022743+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=myapp.herokuapp.com request_id=d1e9ec86-ffe4-4a09-9934-81e25378c32c fwd="94.187.7.67" dyno= connect= service= status=503 bytes=

Those are the only errors I got. The previous errors are the logs from yesterday.

I'm not exactly sure what's the problem. I suspect it has something to do with the shadowJar. But I'm not sure.


Solution

  • I found out what's the problem. I had to change my build method from gradlew clean stage when deploying to Heroku with git push Heroku master to gradlew shadowJar.

    How I did it is mentioned in the docs:

    Heroku config:set GRADLE_TASK="shadowJar"
    

    Now, every time to do a git push Heroku master, it doesn't run gradlew clean stage, but gradlew shadowJar, which I use to include dependencies into my Java web application.