JAva呼び出しcmd/shファイル


最近、製品を作るときにjavaがwindowsの下でbatスクリプトを呼び出し、linuxの下でshellスクリプトを呼び出すことに遭遇しました.後で調べるために記録します.
まずRuntimeUtilsツールクラスを貼り付けます.これにより、Runtimeでのexecの呼び出しが簡略化されます.
package com.zohan.www.util;

import java.io.File;
import java.io.IOException;

import org.apache.commons.lang3.StringUtils;

/**
 * @ClassName: RuntimeUtils
 * @Description: runtime            
 * @author zohan [email protected]
 * @date 2012-10-30   8:42:09
 * 
 */
public class RuntimeUtils {
	
	/**
	 * 
	* @Title: exec
	* @Description:        
	* @param  command    
	* @param  envp     
	* @param  dir    
	* @return Process        
	* @throws IOException  
	 */
	public static Process exec(String command, String envp, String dir)
			throws IOException {
		String regex = "\\s+";
		String args[] = null;
		String envps[] = null;
		if (!StringUtils.isEmpty(command)) {
			args = command.split(regex);
		}

		if (!StringUtils.isEmpty(envp)) {
			envps = envp.split(regex);
		}

		return Runtime.getRuntime().exec(args, envps, new File(dir));

	}

}

 次はtomcatをオンにするテストクラスとtomcatをオフにするテストクラスTestRuntimeUtilsです.
package com.zohan.ww.system;



import org.junit.Test;

import com.zohan.www.util.RuntimeUtils;

/**
 * @ClassName: TestRuntimeUtils
 * @Description: RuntimeUtils  
 * @author zohan [email protected]
 * @date 2012-10-30   9:02:48
 * 
 */
public class TestRuntimeUtils {

	/**
	 * 
	 * @Title: testStartTomcat
	 * @Description: windows    tomcat
	 * @throws Exception
	 * @return void     
	 * @throws
	 */
	@Test
	public void testStartTomcat() throws Exception {
		String command = "cmd.exe /c  startup.bat";
		String dir = "D:\\ehcache\\apache-tomcat-6.0.35\\bin";
		Process process = RuntimeUtils.exec(command, null, dir);
		int i = process.waitFor();
		System.exit(i);
	}

	/**
	 * 
	 * @Title: testStopTomcat
	 * @Description: windows    tomcat
	 * @throws Exception
	 *                 
	 * @return void     
	 * @throws
	 */
	@Test
	public void testStopTomcat() throws Exception {
			
		String command = "cmd.exe /c start  shutdown.bat";
		String dir = "D:\\ehcache\\apache-tomcat-6.0.35\\bin";
		Process process = RuntimeUtils.exec(command, null, dir);
		int i = process.waitFor();
		System.exit(i);
	}
	
	/**
	 * 
	* @Title: testStartWas
	* @Description: linux     was  
	* @throws Exception        
	* @return void        
	* @throws
	 */
	@Test
	public void testStartWas() throws Exception{
		
		String command = "sh startServer.sh server1";
		String dir = "/usr/IBM/WebSphere/AppServer/profiles/AppSrv01/bin";
		Process process = RuntimeUtils.exec(command, null, dir);
		int i = process.waitFor();
		System.exit(i);
		
	}
	
	/**
	 * 
	* @Title: testStopWas
	* @Description: linux    was  
	* @throws Exception        
	* @return void        
	* @throws
	 */
	@Test
	public void testStopWas()throws Exception{
		String command = "sh stopServer.sh server1 -username admin password";
		String dir = "/usr/IBM/WebSphere/AppServer/profiles/AppSrv01/bin";
		Process process = RuntimeUtils.exec(command, null, dir);
		int i = process.waitFor();
		System.exit(i);
	}
	
	
	

}
 

 注意:このプロセスで環境はnullであり、現在のスレッドの環境変数が継承されます.