除了nullip之外,我不能让它返回任何东西.我必须在格式化String数组中的操作的方式中遗漏一些东西,请帮忙!另外,Java中的命令行工作是否有更好的sdk?更新为了将来参考,这是一个EC2实例,并且执行InetAddress.getLocalHost()会返回null,因此我已经恢复到命令行(AWS SDK对于深入了解本地主机IP来说是一种痛苦).
//要运行的命令: /sbin/ifconfig | awk 'NR==2{print$2}' | sed 's/addr://g'
String[] command = new String[] {"/sbin/ifconfig", "awk 'NR==2{print$2}'", "sed 's/addr://g'" };
String ip = runCommand(command);
public static String runCommand(String[] command) {
String ls_str;
Process ls_proc = null;
try {
ls_proc = Runtime.getRuntime().exec(command);
} catch (IOException e1) {
e1.printStackTrace();
}
DataInputStream ls_in = new DataInputStream(ls_proc.getInputStream());
try {
while ((ls_str = ls_in.readLine()) != null) {
return ls_str;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
Runtime.exec()时会尝试使用?Runtime.exec()不会.它总结了您在使用时可能遇到的所有主要缺陷Runtime.exec()(包括#2中提到的缺陷)并告诉您如何避免/解决它们.| 归档时间: |
|
| 查看次数: |
467 次 |
| 最近记录: |