求个java Runtime.getRuntime().exec的问题,每次只是匹配了第一个判断条件

waylau 发布于 2015/06/05 19:35
阅读 271
收藏 0
求个  Runtime.getRuntime().exec的问题,每次只是匹配了第一个判断条件(处理 centos 7 的虚拟机特殊情况 ),第二个判断条件一直判断不上,也就是说,如果运行在   centos 7 的虚拟机 就能判断到,而运行到 centos 7 下就不行,怎么破?
下图是 centos 7 环境

下图是centos 7 虚拟机环境


public String getLinuxMACAddress() {
		String mac = null;
		BufferedReader bufferedReader = null;
		Process process = null;

		if (process == null) {
 
			try {
				// 处理 centos 7 的虚拟机特殊情况
				process = Runtime.getRuntime().exec("ip addr show enp0s3");
 
				if(process != null){
					bufferedReader = new BufferedReader(new InputStreamReader(
							process.getInputStream()));
					String line = null;
					int index = -1;
					while ((line = bufferedReader.readLine()) != null) {
						index = line.toLowerCase().indexOf("link/ether");
						if (index != -1) {
							mac = line.substring(index + 10).trim();
							break;
						}
					}
					mac = mac.substring(0, 17);
				}
				int exitVal = 0;
				try {
					exitVal = process.waitFor();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            System.out.println("Process exitValue: " + exitVal);
			} catch (IOException e) {
				logger.info(e.getMessage());
			}
 
		}
		

		
		if (process == null) {
 
			try {
				// 处理 centos 7 的特殊情况
				process = Runtime.getRuntime().exec("ip addr show ens33");
 
				if(process != null){
					bufferedReader = new BufferedReader(new InputStreamReader(
							process.getInputStream()));
					String line = null;
					int index = -1;
					while ((line = bufferedReader.readLine()) != null) {
						index = line.toLowerCase().indexOf("link/ether");
						if (index != -1) {
							mac = line.substring(index + 10).trim();
							break;
						}
					}
					mac = mac.substring(0, 17);
				}
				int exitVal = 0;
				try {
					exitVal = process.waitFor();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            System.out.println("Process exitValue: " + exitVal);
			} catch (IOException e) {
				logger.info(e.getMessage());
			}
 
		}
 
		
		if (process == null) {
 
			try {
				process = Runtime.getRuntime().exec("ifconfig eth0");
 
				if(process != null){
					bufferedReader = new BufferedReader(new InputStreamReader(
							process.getInputStream()));
					String line = null;
					int index = -1;
					while ((line = bufferedReader.readLine()) != null) {
						index = line.toLowerCase().indexOf("硬件地址");
						if (index != -1) {
							// 取出mac地址并去除2边空格
							mac = line.substring(index + 4).trim();
							break;
						}
					}
				}
				int exitVal = 0;
				try {
					exitVal = process.waitFor();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            System.out.println("Process exitValue: " + exitVal);
			} catch (IOException e) {
				logger.info(e.getMessage());
			}
		}

 
		
		if (bufferedReader != null) {
			try {
				bufferedReader.close();
			} catch (IOException e) {
				logger.info(e.getMessage());
			}
		}

		bufferedReader = null;
		process = null;

		return mac;

	}




加载中
1
如比如比
如比如比
catch NullPointerException 到第二个处理,依次,全错的时候再抛出。
1
waylau
waylau

引用来自“茶壶”的评论

catch NullPointerException 到第二个处理,依次,全错的时候再抛出。
楼上正解 

最终解决的代码如下:

public String getLinuxMACAddress() {
		String mac = null;
		BufferedReader bufferedReader = null;
		Process process = null;
		try {
			try {
				// 处理 centos 7 的虚拟机特殊情况
				process = Runtime.getRuntime().exec("ip addr show enp0s3");

				if (process != null) {
					bufferedReader = new BufferedReader(new InputStreamReader(
							process.getInputStream()));
					String line = null;
					int index = -1;
					while ((line = bufferedReader.readLine()) != null) {
						index = line.toLowerCase().indexOf("link/ether");
						if (index != -1) {
							mac = line.substring(index + 10).trim();
							break;
						}
					}
					mac = mac.substring(0, 17);
				}
			} catch (NullPointerException e) {
				try {
					// 处理 centos 7 的特殊情况
					process = Runtime.getRuntime().exec("ip addr show ens33");

					if (process != null) {
						bufferedReader = new BufferedReader(
								new InputStreamReader(process.getInputStream()));
						String line = null;
						int index = -1;
						while ((line = bufferedReader.readLine()) != null) {
							index = line.toLowerCase().indexOf("link/ether");
							if (index != -1) {
								mac = line.substring(index + 10).trim();
								break;
							}
						}
						mac = mac.substring(0, 17);
					}

				} catch (NullPointerException e2) {
					try {
						process = Runtime.getRuntime().exec("ifconfig eth0");

						if (process != null) {
							bufferedReader = new BufferedReader(
									new InputStreamReader(
											process.getInputStream()));
							String line = null;
							int index = -1;
							while ((line = bufferedReader.readLine()) != null) {
								index = line.toLowerCase().indexOf("硬件地址");
								if (index != -1) {
									// 取出mac地址并去除2边空格
									mac = line.substring(index + 4).trim();
									break;
								}
							}
						}
					} catch (NullPointerException e3) {
						logger.info(e3.getMessage());
					}
				}
			}

		} catch (IOException e) {
			logger.info(e.getMessage());
		} finally {
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					logger.info(e.getMessage());
				}
			}

			bufferedReader = null;
			process = null;
		}

		return mac;

	}




返回顶部
顶部