Ubuntu 下 Java 网络参数配置指南
一 前置检查与安装
二 在 Java 层设置网络参数
java \
-Dhttp.proxyHost=proxy.example.com \
-Dhttp.proxyPort=8080 \
-Dhttps.proxyHost=proxy.example.com \
-Dhttps.proxyPort=8080 \
-Dhttp.nonProxyHosts="localhost|127.0.0.1|*.example.com" \
-jar your-application.jar
System.setProperty("http.proxyHost", "proxy.example.com");
System.setProperty("http.proxyPort", "8080");
System.setProperty("https.proxyHost", "proxy.example.com");
System.setProperty("https.proxyPort", "8080");
System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1|*.example.com");
System.setProperty("http.proxyUser", "username");
System.setProperty("http.proxyPassword", "password");
import java.net.InetAddress;
public class NetCheck {
public static void main(String[] args) throws Exception {
InetAddress a = InetAddress.getByName("www.google.com");
System.out.println("OK: " + a.getHostAddress());
}
}
三 在 Ubuntu 层设置网络参数
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: no
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
nmcli con mod <连接名> ipv4.method manual
nmcli con mod <连接名> ipv4.addresses 192.168.1.100/24
nmcli con mod <连接名> ipv4.gateway 192.168.1.1
nmcli con mod <连接名> ipv4.dns "8.8.8.8 8.8.4.4"
nmcli con down <连接名> && nmcli con up <连接名>
auto eth0
iface eth0 inet static
address 192.168.30.133
netmask 255.255.255.0
gateway 192.168.30.1
dns-nameservers 8.8.8.8 8.8.4.4
四 常见问题排查