Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check programatically (without string-matching) whether using IPV6 or IPV4 for JVM

Tags:

java

jvm

matlab

I want to check whether JVM options for a particular application (in this case, Matlab) have been set to prefer IPV4 or if they still use IPV6.

I know how to set the JVM to prefer IPV4. In my case, it can be done by adding the line

-Djava.net.preferIPv4Stack=true

to the java.opts file within $MATLABROOT/bin/maci64/.

I can also check whether this line has already been added to java.opts via string-matching. I've pasted my current solution (a Matlab script that checks for string-match, and adds the line if it does not exist) at the bottom of this question.

I don't know how, though, to check whether IPV4 or IPV6 is preferred without string-matching. Obviously this seems preferred.

Does anybody know how to check IPV4 vs. IPV6 in the JVM without string-matching?

Here's my current solution, that depends on string-matching:

% OSX platform-specific: revert to IPv4
if (computer('arch') == 'maci64')
  javaoptspath = fileread([matlabroot '/bin/' computer('arch') '/java.opts']);
  k = strfind(javaoptspath, '-Djava.net.preferIPv4Stack=true');
  if isempty(k)
    setenv('DRAKE_IPV4_SET_MATLABROOT', matlabroot)
    setenv('DRAKE_IPV4_SET_ARCH', computer('arch'))
    display('Since you are on Mac, we will need to set your JVM to prefer IPV4 instead of IPV6 for MATLAB')
    display('Please enter your sudo password below')
    ! (echo "" | echo "-Djava.net.preferIPv4Stack=true") | sudo tee -a $DRAKE_IPV4_SET_MATLABROOT/bin/$DRAKE_IPV4_SET_ARCH/java.opts
  end
end
like image 832
Pete Florence Avatar asked Oct 30 '22 21:10

Pete Florence


1 Answers

You can access the underlying java system properties without parsing the options string by using the java.lang.System class directly from Matlab.

For example:

ipv4_preferred = java.lang.System.getProperty('java.net.preferIPv4Stack')

The result of getProperty will be empty if the user has not set -Djava.net.preferIPv4Stack=..., so a more complete solution might be:

ipv4_preferred = java.lang.System.getProperty('java.net.preferIPv4Stack');
if isempty(ipv4_preferred)
  ipv4_preferred = false;
end
like image 80
rdeits Avatar answered Nov 09 '22 07:11

rdeits