1

When I "mvn exec:java" the java program, I have problems to set port to 80, but 8080 can be successful.

 Undertow server = Undertow.builder()
                    .addHttpListener(80, "localhost")
                    .setHandler(path)
                    .build();
            server.start();

when I set it as 80, i got exceptions like

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: java.net.SocketException: Permission denied
        at io.undertow.Undertow.start(Undertow.java:141)
        at cc.cmu.edu.Q1.Q1Controller.main(Q1Controller.java:46)
        ... 6 more
Caused by: 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 sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:67)
        at org.xnio.nio.NioXnioWorker.createTcpConnectionServer(NioXnioWorker.java:175)
        at org.xnio.XnioWorker.createStreamConnectionServer(XnioWorker.java:242)
        at io.undertow.Undertow.start(Undertow.java:120)
        ... 7 more
Rocket
  • 55
  • 1
  • 10

1 Answers1

5

You need root access to be able to listen on all ports below 1024. Start your application as root, or with a sudo command.

ant1g
  • 969
  • 9
  • 13
  • Following solution may be more preferable for production environments: https://stackoverflow.com/questions/33703965/spring-boot-running-app-on-port-80#answer-33704078 – Halil Sep 21 '17 at 09:53
  • This really depends on your requirements. The answer you linked suggests to use apache2, which can be cumbersome to deal with in some cases. Exposing a service on port 80 directly is perfectly fine I think. – ant1g Sep 22 '17 at 09:20