Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 674
Ali Bdeir Avatar asked Feb 23 '17 15:02

Ali Bdeir


People also ask

Can Heroku use localhost?

Start your app locally using the heroku local command, which is installed as part of the Heroku CLI. Your app should now be running on http://localhost:5000/.

Why does Heroku app crash?

If your Procfile is pointing to the wrong server file. e.g If your server is in server. js and your Procfile points to app. js this would definitely crash your app and Heroku would greet you with the H10-App crashed error code message.

Does Heroku work with Java?

Heroku makes it easy to deploy and scale Java apps. Whether you use the standard libraries with application servers like Tomcat or Jetty, or frameworks like Spring or Play, Heroku helps you build things your way with the tools you love.


1 Answers

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.

like image 96
Ali Bdeir Avatar answered Oct 05 '22 23:10

Ali Bdeir