Search code examples
javaspark-java

Spark fails to bind http port


I am trying to bind port 80 to my Spark server :

import static spark.Spark.get;
import static spark.Spark.port;

public class Main {

    public static void main(String[] args) {
        port(80);
        get("/hello", (request, response) -> "hello world");
    }
}

But for some reason I getting the following error :

22:51:56.067 [Thread-0] ERROR spark.Spark - ignite failed
java.net.SocketException: Permission denied
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:433)
    at sun.nio.ch.Net.bind(Net.java:425)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
    at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:298)
    at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
    at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.server.Server.doStart(Server.java:431)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at spark.embeddedserver.jetty.EmbeddedJettyServer.ignite(EmbeddedJettyServer.java:149)
    at spark.Service.lambda$init$2(Service.java:496)
    at java.lang.Thread.run(Thread.java:748)

I searched everywhere in my OS if there's any connected bind to port 80, but i didn't find .
there's any solution?


Solution

  • Binding to port 80 directly is likely a bad idea.

    This port (as any port < 1024) requires root privileges (but running as root is highly discouraged), or some capabilities setting.

    Normally you run Spark on a non-privileged port. If you do want to expose it as the default for Web connections, consider proxying it via a Web server, such as nginx or apache; you would likely want to host other stuff nearby anyway.