简体   繁体   English

以编程方式检查(无字符串匹配)是否使用IPV6或IPV4进行JVM

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

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. 我想检查特定应用程序(在本例中为Matlab)的JVM选项是否已设置为更喜欢IPV4,或者它们是否仍使用IPV6。

I know how to set the JVM to prefer IPV4. 我知道如何将JVM设置为更喜欢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/. 到$ MATLABROOT / bin / maci64 /中的java.opts文件。

I can also check whether this line has already been added to java.opts via string-matching. 我还可以检查这行是否已经通过字符串匹配添加到java.opts。 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. 我已经粘贴了我当前的解决方案(一个检查字符串匹配的Matlab脚本,如果它不存在则添加该行)在这个问题的底部。

I don't know how, though, to check whether IPV4 or IPV6 is preferred without string-matching . 但是,我不知道如何在没有字符串匹配的情况下检查IPV4或IPV6是否是首选。 Obviously this seems preferred. 显然这似乎是首选。

Does anybody know how to check IPV4 vs. IPV6 in the JVM without string-matching? 有没有人知道如何在没有字符串匹配的情况下检查JVM中的IPV4与IPV6?

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

You can access the underlying java system properties without parsing the options string by using the java.lang.System class directly from Matlab. 您可以直接从Matlab使用java.lang.System类来访问底层的java系统属性,而无需解析选项字符串。

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: 如果用户未设置-Djava.net.preferIPv4Stack=... ,则getProperty的结果将为空,因此更完整的解决方案可能是:

ipv4_preferred = java.lang.System.getProperty('java.net.preferIPv4Stack');
if isempty(ipv4_preferred)
  ipv4_preferred = false;
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM