0
votes

When running a Java application from a .Jar file within an Azure Web app, the application runs but fails to bind to port 80.

Having followed the instructions in the link below: https://blogs.msdn.microsoft.com/azureossds/2015/12/28/running-java-jar-file-to-serve-web-requests-on-azure-app-service-web-apps/

The application starts up, but throws an error when attempting to bind:

java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_60] at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_60] at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_60] at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_60] at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.8.0_60]

I expect it will be a w3wp process that is already bound to port 80, so does not allow the java application to bind to the same port.

Here is what the web.config for the Web App looks like:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

<system.webServer>

<handlers>

<add name="httpPlatformHandler" path="*" verb="*"

modules="httpPlatformHandler" resourceType="Unspecified" />

</handlers>

<httpPlatform processPath="%ProgramW6432%\Java\jdk1.8.0_60\bin\java.exe"

arguments="-Djava.net.preferIPv4Stack=true -Dport.http=%HTTP_PLATFORM_PORT% -jar &quot;%HOME%\site\wwwroot\bin\demojar.jar&quot;"

stdoutLogEnabled="true"

startupRetryCount='10'>

</httpPlatform>

</system.webServer>

</configuration>

Is there a way of convincing the Azure Web App platform to allow the Java application bind to port 80?

1
You cannot have two apps both bound to the same port. So you need to resolve that conflict. Either you need to move your app to a different port, find the other app and remove it or move it to a different port, or run your app on a server that doesn't have something running on port 80 already. I'm guessing your server already has a web server running. - Joseph Larson
Hi @JosephLarson, thanks for the quick reply. The server is running as an Azure Web App. I can set the Java application to bind to another port (e.g. 8080), but Azure Web Apps only expose ports 80 and 443 to the public, so port 8080 is not reachable. Not sure how to check what process is binding to port 80 as netstat command is blocked on an Azure Web App. My guess is that it's a w3wp process. - Lenny

1 Answers

0
votes

They key is the %HTTP_PLATFORM_PORT% environment variable. The Web App Service will set this environment variable to be the port that it is expecting your Java application to listen on (which is why your web.config is also passing that value as an argument to the Java runtime).

Instead of listening on port 80 you should configure the Java app to bind to port %HTTP_PLATFORM_PORT%. IIS will then transparently route all requests received on port 80 into your application. It will also help with things like TLS termination on port 443 so the app can also be exposed over HTTPS.