This prints NULL:

String sss = System.getSecurityManager() == null ? "NULL" : System.getSecurityManager().getClass().getName();
System.out.println(sss);

And this throws exception:

ServerSocket serversocket = new ServerSocket(80);

This one:

Exception in thread "main" java.lang.Error: java.net.BindException: Permission denied
	at httpserver.HTTPServer.main(HTTPServer.java:68)
Caused by: java.net.BindException: Permission denied
	at java.net.PlainSocketImpl.socketBind(Native Method)
	at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)
	at java.net.ServerSocket.bind(ServerSocket.java:328)
	at java.net.ServerSocket.<init>(ServerSocket.java:194)
	at java.net.ServerSocket.<init>(ServerSocket.java:106)
	at httpserver.HTTPServer.main(HTTPServer.java:38)

This actually works on Windows, but fails on OsX Snow Leo. Also it works with any other port, yet port 80 is not in use, and I run everything as admin.
Thanks!

Could be a firewall blocking the port for you?

OsX firewall is turned off.

This code:

ServerSocket serversocket = null;
for (int i = 0; i <= 2000; i++) {
    try {
        serversocket = new ServerSocket(i);
        serversocket.close();
        System.out.println(i + " port FREE");
    } catch (BindException ex) {
        System.out.println(i + " port BLOCK");
    }
}

Prints this:

0 port FREE
1 port BLOCK
2 port BLOCK
3 port BLOCK
[...]
1021 port BLOCK
1022 port BLOCK
1023 port BLOCK
1024 port FREE
1025 port FREE
1026 port FREE
[...]
1998 port FREE
1999 port FREE
2000 port FREE

I've found this on the web:

# Port: The port to which the standalone server listens. For
# ports < 1023, you will need httpd to be run as root initially.

Also when I use java -jar httpserver.jar , I have the same result, but when I use sudo java -jar httpserver.jar , I have all ports free! Now what else did I do but forced the program to run as a user who've started it in the 1st place?
Problem half way solved. Now anybody knows, how to fix this in Java? I guess I have to throw up that fancy box that asks for my password every time I run something serious... I can't really understand, cause I'm new to Unix, and as far as I know, I'm already doing this logged in as admin user! Also this way I can't test it from the IDE...
Thanks!

AFAIK, you require "superuser" (also called as "root") privileges for binding to ports below 1024 and `sudo` does exactly that, run the program as a "superuser". From the sudo wikipedia entry:

By default, sudo will prompt for a user password but it may be configured to require the root password, and will require it only once every 15 minutes per pseudo terminal, or no password at all.

As you can see, it by default happily accepts the logged in user's password (just to confirm you really are you) before running commands as root. This can of course be configured differently to make sure sudo actually requires 'root' password but that's a different story.

Also, though admin users are almost like "superusers", there are a few areas wherein you actually require a superuser and you have stumbled upon one of them. The solution? When testing use a port which is above 1023, something like 8080. I don't do my development on a *nix box but there is a thread on SO which talks about the same stuff, maybe you can draw some inspiration from there but using a different port would be the easiest.

Thanks, I've already found the visudo too. My problem is that even though I have the right to run sudo, I don't have the rights associated with it without actually running sudo.
So the testing is not really important, I can change the port to 8080. So the only question is, after the deployment: Can't I just use that fancy password prompt that for example installers often use, and the user just types in the password to give rights, instead of this sudo thing?
Thanks!

If you want to distribute your application to users, just package your JAR along with shell scripts for specific platforms, just as it is done for applications like Tomcat etc.

If you are using such shell scripts, it would be pretty simple to put in the sudo in the script and all the user will see is a password prompt. Something like:

# runserver.sh
sudo java -jar httpserver.jar

This way, your users won't be aware of "sudo" in the script and would just have to type in the password, as intended.

So you say, I can't throw that box up from java.... Well, thx anyway!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.