[英]How can a java program decide which command to execute depending of os
我必须制作一个程序,它将使用Windows的“ipconfig”和Linux的“ifconfig”打印网络设置,但我需要为这两个操作系统提供独特的实现。
You can get the name of the operating system through 您可以通过获取操作系统的名称
System.getProperty("os.name")
Have a look at this page for some sample code. 有关示例代码,请查看此页面 。
If it's by any chance the IP of the local host you're interested in, there are ways to get this directly in Java: 如果您感兴趣的是本地主机的IP,那么有很多方法可以直接在Java中获取:
There is no way to determine what the "show ip information"-command is for an arbitrary operating system. 没有办法确定“show ip information”命令对于任意操作系统是什么。 You will have to hard-code what the command is (if the is one) for each operating system name manually.
您必须手动为每个操作系统名称硬编码命令(如果是一个)。
作为其他答案的补充,我将提到Commons Lang的SystemUtils
,它暴露了各种常量,如IS_OS_UNIX
, IS_OS_WINDOWS
等。
Building on aioobe's solution : 基于aioobe的解决方案 :
final String osname = System.getProperty("os.name").toLowerCase();
String processName;
if(osname.startsWith("win"))
processName="ipconfig /some /parameter";
else
processName="ifconfig -some -parameter";
Runtime.getRuntime().exec(processName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.