aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Java & Windows (7): Reliably getting IPv4 netmask? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Java & Windows (7): Reliably getting IPv4 netmask? " Watch "Java & Windows (7): Reliably getting IPv4 netmask? " New topic
Author

Java & Windows (7): Reliably getting IPv4 netmask?

Tim Miller
Greenhorn

Joined: Feb 16, 2012
Posts: 4
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.

At http://stackoverflow.com/questions/1221517/how-to-get-subnet-mask-using-java, 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!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Please BeForthrightWhenCrossPostingToOtherSites(⇐click) as a matter of courtesy to those whose help you seek. Thanks!


https://forums.oracle.com/forums/thread.jspa?threadID=2347643&tstart=0
Tim Miller
Greenhorn

Joined: Feb 16, 2012
Posts: 4
Jeff Verdegan wrote:Please BeForthrightWhenCrossPostingToOtherSites(⇐click) as a matter of courtesy to those whose help you seek. Thanks!
https://forums.oracle.com/forums/thread.jspa?threadID=2347643&tstart=0


Oh, sorry about that. I first posted on StackOverflow, but I didn't get a response. Usually I'm amazed at how quick people are to respond on that site, so I'm assuming the lack of response is due to this being a hard problem. I've been digging pretty hard on this, and most I've seen is some suggestions as to how to fix the problem in Java itself, but no work-arounds that actually work for me. Thus, I was motivated to post in multiple places in the hope that I'd get some help somewhere.

I really apologize for the confusion!

Also, here's a list of other places where I have asked about this:
http://stackoverflow.com/questions/9299566/java-windows-7-reliably-getting-ipv4-netmask
http://www.codeguru.com/forum/showthread.php?t=521196
https://forums.oracle.com/forums/thread.jspa?threadID=2347643&tstart=0

I am actively monitoring each site.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Tn Miller wrote:
Jeff Verdegan wrote:
Also, here's a list of other places where I have asked about this:


Thanks for that! And good luck! (I'd help you if I knew the answer, but sadly, I don't.)
Tim Miller
Greenhorn

Joined: Feb 16, 2012
Posts: 4
This answer worked. I needed to put this line of code first in my main method:

System.setProperty("java.net.preferIPv4Stack","true");

I don't know why putting it on the command line didn't take, but this worked.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java & Windows (7): Reliably getting IPv4 netmask?