Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 stackoverflow.com 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
1  
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
add comment

4 Answers

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
add comment

-> 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
add comment

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

http://support.lexisnexis.com/us_en_us/record.asp?bRFS=1&ArticleID=10093

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
add comment
up vote 0 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");
            p.waitFor();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line = reader.readLine();
            while (line != null) {
                System.out.println(line);
                line = reader.readLine();
            }

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.