B4J Question B4J server on Amazon EC2 - java.net.SocketException: Permission denied

roumei

Active Member
Licensed User
I'm trying to run Erel's HelloWorld Example on an Amazon EC2 instance. This is what I did:

1) Create the EC2 instance, open port 80 (at least I think I did, see image) and install Java.
2) Compile the B4J project with port number 80 and copy it together with the www directory to the Home directory on the EC2 instance.
3) Run the jar file with:
B4X:
java -jar /home/ec2-user/HelloWorld/HelloWorld.jar
This produces the following error. Does anybody have an idea what the problem might be?
B4X:
main._appstart (java line: 55)
java.net.SocketException: Permission denied
at java.base/sun.nio.ch.Net.bind0(Native Method)
at java.base/sun.nio.ch.Net.bind(Net.java:455)
at java.base/sun.nio.ch.Net.bind(Net.java:447)
at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:227)
at java.base/sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:80)
at org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:340)
at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:308)
at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:244)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.server.Server.doStart(Server.java:398)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at anywheresoftware.b4j.object.ServerWrapper.Start(ServerWrapper.java:220)
at b4j.example.main._appstart(main.java:55)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:96)
at b4j.example.main.main(main.java:28)

main.main (java line: 28)
java.lang.RuntimeException: java.net.SocketException: Permission denied
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:138)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:96)
at b4j.example.main.main(main.java:28)
Caused by: java.net.SocketException: Permission denied
at java.base/sun.nio.ch.Net.bind0(Native Method)
at java.base/sun.nio.ch.Net.bind(Net.java:455)
at java.base/sun.nio.ch.Net.bind(Net.java:447)
at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:227)
at java.base/sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:80)
at org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:340)
at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:308)
at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:244)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.server.Server.doStart(Server.java:398)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at anywheresoftware.b4j.object.ServerWrapper.Start(ServerWrapper.java:220)
at b4j.example.main._appstart(main.java:55)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
... 2 more
 

Attachments

  • EC2_Security.PNG
    EC2_Security.PNG
    36.9 KB · Views: 207

roumei

Active Member
Licensed User
Just guessing, perhaps port 80 is already taken by some other service in your EC2 instance
Thank you. The EC2 instance is completely new. Do you know a way to check this?

Have you tried to execute the jar from /home/ec2-user/HelloWorld"?
Yes, that gives the same error.
 
Upvote 0

roumei

Active Member
Licensed User
Thank you! I changed the port as suggested to 51042 in the B4J project and opened all ports for the EC2 for testing purposes. This solved the 'Permission denied' error and the server seems to be running now.
I then tried to access the public ip address but that failed with a 404 error. Do you know if I have to specify a folder? (I randomly tried with /HelloWorld, /www, /HelloWorld/www and with/without index.html).
 

Attachments

  • 404.PNG
    404.PNG
    13.7 KB · Views: 209
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
Thank you! I changed the port as suggested to 51042 in the B4J project and opened all ports for the EC2 for testing purposes. This solved the 'Permission denied' error and the server seems to be running now.
I then tried to access the public ip address but that failed with a 404 error. Do you know if I have to specify a folder? (I randomly tried with /HelloWorld, /www, /HelloWorld/www and with/without index.html).
You should use http://35.156.25.18:51042/hello

B4X:
    srvr.AddHandler("/hello", "HelloPage", False)
    srvr.AddHandler("/FormExampleHelper", "FormExampleHelper", False)
    srvr.AddHandler("/FileUpload", "FileUpload", False)
 

Attachments

  • 1640706083712.png
    1640706083712.png
    2.7 KB · Views: 199
Upvote 0

roumei

Active Member
Licensed User
Maybe it's relevant for others trying to set up an AWS EC2 instance:

After solving the issues above, it was still not possible to use port 80 and display the default page (index.html) with the public IPv4 address. It seems that the B4J server program running on OpenJDK 11 listened to the IPv6 instead of the IPv4 address. Amazon only supports IPv4 for Elastic IP Addresses. I added the following line in AppStart and now everything is working as expected:

B4X:
SetSystemProperty("java.net.preferIPv4Stack" , "true")
 
Upvote 0
Top