This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tim Miller
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic