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 inlw@sina.com
 * @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 inlw@sina.com
 * @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,会继承当前线程的环境变量。

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package generconfig; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; import org.springframework.context.support.GenericXmlApplicationContext; import com.pactera.service.schedule.ThreadReadPath; import com.pactera.util.CommTool; /*打包用File->Export->runnable JAR File Export Launch选择对应的类 Export destination 选择地址 Library handling:Copy required */ public class SMSThreadSender { public static void main(String[] args) { if(args.length==0) { System.out.println("短信服务开启,开始加载Spring配置。"); GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.setValidating(false); context.load("classpath:sysconfig/applicationContext.xml"); context.refresh(); System.out.println("开整·~~~~"); CommTool.smsthreadisruning=true; ThreadReadPath thread = new ThreadReadPath(); thread.smsname="sms"+CommTool.threadid; Thread t1 = new Thread(thread); t1.setName("sms"); t1.start(); System.out.println("已经启动"); } if(args.length==1) { String pfile=args[0]; System.out.println("短信服务开启,开始加载Spring配置。"+pfile); GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.setValidating(false); context.load(pfile); // context.load("classpath:sysconfig/applicationContext.xml"); context.refresh(); CommTool.smsth
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值