Java & Windows 7: Reliably getting IPv4 netmask?
Asked Answered
P

3

6

I've run into a known bug with Java 6 on Windows. My understanding is that the normal way to get the netmask is to look up the network prefix length and do some bit shifts. The problem is that on Windows the prefix length is often returned incorrectly, so we get a 128 when we should get a 24 or 20.

In this solution, it is suggested to put -Djava.net.preferIPv4Stack=true on the Java command line. Unfortunately, on Windows 7, adding that as either a VM parameter or on the Java command line seems to have no effect.

(a) Does anyone know any OTHER work-arounds for this problem that might still work on Windows 7?

(b) Alternatively, is there an entirely different way to get the netmask that is reliable?

Thanks!

P.S. Here is the bug report that pertains to this.

Prospect answered 15/2, 2012 at 19:3 Comment(4)
Proper etiquette dictates that I list other places where I have asked this question: #9300066 codeguru.com/forum/showthread.php?t=521196 forums.oracle.com/forums/… coderanch.com/t/567601/sockets/java/…Prospect
Strange... the -Djava.net.preferIPv4Stack=true VM option does work in my case under Windows 7 for JRE 1.6. I put this in my Java code as System.setProperty("java.net.preferIPv4Stack","true"). Unless, something (library or whatever) is resetting it. Can you show your IP netmask codes?Ernaernald
Thanks for that line of code. I'm using Netbeans, and there's a place to put VM options. I tried putting the -D thing there. No go. I'm also launching the application using a wrapper created by Advanced Installer, and there's place to put command-line options. That didn't work either. I'm going to try your suggestion, adding that line of code at the top of my main method. Thanks!Prospect
@eee: Would you mind posting your answer as an answer so I can give you the bounty? Thanks. :)Prospect
E
3

The -Djava.net.preferIPv4Stack=true VM option should work under any OS. Alternatively, it can be put into Java code as System.setProperty("java.net.preferIPv4Stack","true");. Unless, something (library or whatever) is resetting its true state.

Ernaernald answered 20/2, 2012 at 22:45 Comment(0)
P
2

The code below displays the subnet mask. On a computer with more than one network connection (like a laptop with a wireless and Cat-5 Ethernet connection) it may write the subnet mask twice because there can be two different IP addresses for the client.

    String os = System.getProperty("os.name");        
    try {
        if(os.indexOf("Windows 7")>=0) {
            Process process = Runtime.getRuntime().exec("ipconfig");
            process.waitFor();
            InputStream commandOut= process.getInputStream();

            BufferedReader in = new BufferedReader(new InputStreamReader(commandOut));
            String line;
            while((line = in.readLine()) !=null) {
                if(line.indexOf("Subnet Mask")>=0) {
                    int colon = line.indexOf(":");
                    System.out.println(line.substring(colon+2));
                }
            }
        }
    catch(IOException ioe) {    }
    catch(java.lang.InterruptedException utoh) {   }

On my laptop with both a wired and wireless connection active, I get this output: 255.255.254.0 255.255.254.0

When I turn off my wireless connection, I only see one line of output for the wired Ethernet link, as expected.

Photokinesis answered 20/2, 2012 at 5:54 Comment(2)
That's a really nice answer, although a comment from eee above gave me the portable answer. Now I'm torn about how to handle the bounty.Prospect
The code above is portable in the sense that you can easily write code blocks to handle different OS versions. If you only need this fix for Windows 7, then the code above is what you want.Photokinesis
P
1

Since the problem us just in Windows 7, why not look for an OS specific solution? I know we can launch windows programs from Java, including the windows command line or bat files. There must be a way to re-direct the output of ipconfig to a text file in windows. Your program should be able to get the subnet mask by calling ipconfig and then reading the output.

Photokinesis answered 19/2, 2012 at 15:4 Comment(1)
Well, WE are seeing it in Win7, but the Sun bug isn't that specific. In any case, the same solution should work on any version of Windows. I have to learn how to check the OS type, fire off an external program, and capture its output. And then as a bonus, maybe use some clever regex thing to parse out the result (rather than writing custom code to parse it char-by-char).Prospect

© 2022 - 2024 — McMap. All rights reserved.