Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created a program in Java that uses an FTP connection to download files. It works just fine when I run it in Eclipse. However, when I export the project to an executable JAR file, the program does not connect to the FTP server, it connects only if I disable Windows Firewall.

How can I programmatically add an exception to this JAR file in the firewall so that the user would not have to turn off the firewall to use my program?

share|improve this question

migrated from Oct 18 '12 at 14:32

This question came from our site for professional and enthusiast programmers.

All connections pass through the java.exe interpreter. If its added in the firewall, nothing can block making new connections. – Prototype Stark Oct 18 '12 at 12:42
You cannot programmatically add an exception to the firewall. Otherwise every program could do that - and thereby compromise the firewall. – Prototype Stark Oct 18 '12 at 13:00
Ok, how i can programmatically ask exception in firewall, like some programs do. Popups window that ask for exception. – DanM Oct 18 '12 at 13:04
If you are unable to make the connection, just display the message, to add the exception to the user's firewall. – Ramhound Oct 18 '12 at 16:27

In your windows firewall settings, you have an "Exceptions" tab. Add "java" or "javaw" or whatever you use there as an exception. You can also add port 20 and port 21 to the list of exceptions.

share|improve this answer
Tried that, did`n worked. – DanM Oct 18 '12 at 12:58
Sure you added the right java.exe and/or javaw.exe? Exactly the one that is called when you double click on the jar? – Udo Klimaschewski Oct 18 '12 at 13:05

-> Control Panel -> Systems and Securtiy -> Windows Firewall -> Allowed Programs

You have to add java.exe interpreter to the firewall. You cannot add a .jar file to the firewall of windows.

share|improve this answer
Tried that, did`n worked. – DanM Oct 18 '12 at 12:58

You can manually add exceptions to the firewall so your program can get though without disabling it totally.

Exceptions can be added automatically, run the following commands.

Add firewall rule:

Process p = Runtime.getRuntime().exec("cmd /c start runas /user:domain\administrator /savecred:cmdrun.txt netsh advfirewall firewall add rule name=”FTP Service” action=allow service=ftpsvc protocol=TCP dir=in");

Disable stateful ftp filtering:

Process p = Runtime.getRuntime().exec("cmd /c start runas /user:domain\administrator /savecred:cmdrun.txt netsh advfirewall set global StatefulFTP disable");

Also, make sure your ftp connection is in passive mode.

share|improve this answer
up vote 1 down vote accepted

Solved it, added "netsh advfirewall set global StatefulFTP disable" via command promp, the code:

try {
            Process p = Runtime.getRuntime().exec("netsh advfirewall set global StatefulFTP disable");
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line = reader.readLine();
            while (line != null) {
                line = reader.readLine();

        } catch (IOException e1) {
        } catch (InterruptedException e2) {
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .