System Groovy脚本中的java.lang.NumberFormatException
我试图通过将从机的ip地址添加到系统变量并将其传递到下一个作业来保存该从机的ip地址。但当我运行该作业时,出现以下异常: 日志:System Groovy脚本中的java.lang.NumberFormatException,java,jenkins,groovy,dsl,Java,Jenkins,Groovy,Dsl,我试图通过将从机的ip地址添加到系统变量并将其传递到下一个作业来保存该从机的ip地址。但当我运行该作业时,出现以下异常: 日志: 从机2:X.X.X.X java.lang.NumberFormatException:用于输入字符串:“X.X.X.X” 位于java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在java.lang.Integer.parseInt(Integer.java:58
从机2:X.X.X.X
java.lang.NumberFormatException:用于输入字符串:“X.X.X.X”
位于java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
在java.lang.Integer.parseInt(Integer.java:580)处
位于java.lang.Integer。(Integer.java:867)
IPv4地址中包含3个点,因此无法直接将其解析为整数
我想您正在尝试将其转换为相应的int
,表示IP 32位。这可以在Java中完成,如下所示:
Slave Machine 2: X.X.X.X
java.lang.NumberFormatException: For input string: "X.X.X.X"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.<init>(Integer.java:867)
公共静态int-ipToInt32(字符串ip){
Inet4Address-ipAddress;
试一试{
ipAddress=(Inet4Address)InetAddress.getByName(ip);
}捕获(未知后异常e){
抛出新的IllegalStateException(“无法将IP转换为位:“+IP+””,e);
}
byte[]ipBytes=ipAddress.getAddress();
return((ipBytes[0]&0xFF)您无法将ipadd转换为整数。因为它不是有效的整数。在我看来,您不必强制将ipadd转换为整数。因此,我建议用以下行替换行字符串myString=new integer(ipadd)
public static int ipToInt32(String ip) {
Inet4Address ipAddress;
try {
ipAddress = (Inet4Address) InetAddress.getByName(ip);
} catch (UnknownHostException e) {
throw new IllegalStateException("Cannot convert IP to bits: '" + ip + "'", e);
}
byte[] ipBytes = ipAddress.getAddress();
return ((ipBytes[0] & 0xFF) << 24)
| ((ipBytes[1] & 0xFF) << 16)
| ((ipBytes[2] & 0xFF) << 8)
| (ipBytes[3] & 0xFF);
}
public static int ipToInt32(String ip) {
Inet4Address ipAddress;
try {
ipAddress = (Inet4Address) InetAddress.getByName(ip);
} catch (UnknownHostException e) {
throw new IllegalStateException("Cannot convert IP to bits: '" + ip + "'", e);
}
byte[] ipBytes = ipAddress.getAddress();
return ((ipBytes[0] & 0xFF) << 24)
| ((ipBytes[1] & 0xFF) << 16)
| ((ipBytes[2] & 0xFF) << 8)
| (ipBytes[3] & 0xFF);
}
String myString = new String(ipadd)