java 远程执行linuxschell 命令

package com.c.elmer.util.tool;

import java.io.File;
import java.util.List;
import java.util.Properties;

public class LinuxExec {
	
/**  path 为当前路径
*/
	public static boolean exec(String common,String path){
//		Linux

		Properties props=System.getProperties(); //获得系统属性集   

		String osName = props.getProperty("os.name"); //操作系统名称   
		
		if(osName.toLowerCase().indexOf("linux") != -1){
			return shell(common,path);
		}
		return false;
	}
	
	public static boolean shell(List commons){
		
		boolean success = false;
		
		try{
			for(int i=0;i<commons.size();i++){
				
				Process p = Runtime.getRuntime().exec(commons.get(i).toString());
				
				
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return success;
		
	}
	public static boolean shell(String common,String path){
		
		boolean success = false;
		
		try{
			Process p = Runtime.getRuntime().exec(common,null,new File(path));
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return success;
		
	}

}


使用如:

			if(common.equals("")){
				common = "tar -zcvf export.tar.gz ./export";
			} 
			LinuxExec.exec(common,exportPath);

你可能感兴趣的:(java,C++,c,linux,OS)