在java中添加/设置ipv4 LAN地址的最佳方法 [英] Best way to add/set an ipv4 LAN address in java

查看:49
本文介绍了在java中添加/设置ipv4 LAN地址的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣从 Java 应用程序(实际上是 Scala)添加一个额外的 ipv4 本地网络 IP 地址.到目前为止,我找到的唯一解决方案是 netsh 的运行时执行,但我不希望依赖于 netsh 并尽可能使用核心 java 库.什么是最好的解决方案?此外,看起来即使 netsh 对我也不起作用,因为我在 linux 上.

I'm interested in adding an additional ipv4 local network ip address from a java application (actually Scala). So far, the only solution I've found is runtime exec of netsh, but I'd prefer not to have a dependency on netsh and use core java libraries if possible. What's the best solution for this? Moreover, it looks like even netsh won't work for me since I'm on linux.

推荐答案

您可以调用 ifconfig(或 ipconfig/netsh用于窗户).

You could make a call to ifconfig (or ipconfig/netsh for windows).

使用以下命令:

ifconfig eth0 <NEW-IP> netmask 255.255.255.0 up

在 Java 中:

Process p = Runtime.getRuntime().exec(new String[]{"bash","-c","ifconfig eth0 <NEW-IP> netmask 255.255.255.0 up"});

这篇关于在java中添加/设置ipv4 LAN地址的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆