Java Spark on a Heroku server not binding to the port and crashing

+2 votes
asked Sep 13, 2017 by max-fortin

Every time i try to send a request to the Heroku server, it hangs until the app crashes due to the R10 error which is the server not binding to the port given by Heroku. I have no idea why this is doing this. Here is my main class with a simple server set up for testing.

public static void main(String[] args) {
    port(getHerokuAssignedPort());
    get("/",(req,res) -> "Request Recived!");
}
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)
}

Here is my procfile:
web: java -jar attendance-bot.jar

Like I said, the program hangs until it crashes due to a port not being bound.

2 Answers

0 votes
answered Sep 13, 2017 by codefinger

Try using Integer.valueOf(System.getenv("PORT")) to get the port number.

0 votes
answered Sep 14, 2017 by max-fortin

It was my mistake. I forgot to build the jar when pushing to the heroku server.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
...