Spark web application ports work randomly

I built my application with backend spark-web framework. I use port 8080 to run this

import static spark.Spark.*;

public class App {
    public static void main(String[] args) {
        port(8080);
        var routing = new Routing();
        routing.initRoutes();
    }
}

The problem is that it works really randomly. For example, yesterday everything worked correctly. Today, when I did not change anything, just run intellij and application, the error like this occured: Process finished with exit code 100. When I changed port to 3000, application started work again, but I do not wanna change port everytime because of my api documentation with port 8080in postman. That is not the first time when this problem with port occurs.

Can anybody explain what is going on?

Answer

It sounds like that port is being used already and did not get released either by your previous execution of the program or by another process. You would have to make sure that the process that is using the port is fully terminated or stops using that port before a new process can bind to that port.