Fix the Java.Net.SocketException: Permission Denied in Java

Sheeraz Gul Jan 30, 2023 Aug 19, 2022
  1. Causes of the java.net.SocketException: Permission denied Error in Java
  2. Fix the java.net.SocketException: Permission denied Error in Java
Fix the Java.Net.SocketException: Permission Denied in Java

This tutorial demonstrates the java.net.SocketException: Permission denied error in Java.

Causes of the java.net.SocketException: Permission denied Error in Java

The SocketException usually occurs when there is a problem with the network connection. It can be Permission denied, Connection reset, or anything else.

The java.net.SocketException: Permission denied error occurs when there is no permission from the network to connect with a certain port. The error can occur while connecting or configuring the network settings on different platforms.

The error java.net.SocketException: Permission denied can occur on any server type like Tomcat or OpenShift.

Here are the main reasons for the error java.net.SocketException: Permission denied:

  1. When the operating system doesn’t allow a particular port number.
  2. The antivirus or firewall stops the connection to a certain network.
  3. Sometimes a problem with the older version of Java.

Fix the java.net.SocketException: Permission denied Error in Java

For example, while configuring the HTTPS certificate for the Tomcat server, the error java.net.SocketException: Permission denied can occur while starting the server:

Caused by: java.net.SocketException: Permission denied
                at sun.nio.ch.Net.bind0(Native Method)
                at sun.nio.ch.Net.bind(Net.java:438)
                at sun.nio.ch.Net.bind(Net.java:430)
                at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:225)
                at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
                at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:221)
                at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:1118)
                at org.apache.tomcat.util.net.AbstractJsseEndpoint.init(AbstractJsseEndpoint.java:223)
                at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:587)
                at org.apache.coyote.http11.AbstractHttp11Protocol.init(AbstractHttp11Protocol.java:74)
                at org.apache.catalina.connector.Connector.initInternal(Connector.java:1058)
                ... 13 more

The reason for this error is that the operating system is stopping the connection. Because the Linux system doesn’t allow non-root users to use the root less than 1024; hence, it becomes a permission problem.

Now, the solutions to this problem can be:

  1. The best solution is to use the root account to start the Tomcat server.
  2. If Linux is not allowing port numbers less than 1024, use a port greater than this number. The port number will be added to the URL request.

Similarly, while using the openshift server, the same error java.net.SocketException: Permission denied can occur. And the reason could be either the firewall or antivirus is stopping it or the Java version you are using is not compatible.

The possible solution for this can be:

  1. Stop the antivirus and firewall. Or check they are not blocking the server.

  2. Use Java 8 or above versions. Or put the following VM arguments in your application to be able to run and connect using Java 7:

    -Djava.net.preferIPv4Stack=true
    
  3. Please check the third-party libraries or packages if they have some specific requirements. Some of them will also cause the java.net.SocketException: Permission denied error.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Error