最近在做产品遇到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,会继承当前线程的环境变量。
分享到:
相关推荐
JAVA通过JNI调用本地方法,而本地方法是以库文件的形式存放的(在WINDOWS平台上是DLL文件形式,在UNIX机器上是SO文件形式)。通过调用本地的库文件的内部方法,使JAVA可以实现和本地机器的紧密联系,调用系统级的...
JNI,JNATIVE,JAWIN 使用Java调用DLL动态链接库的方案我知道的有四种:JNI,Jnative,Jawin,Jacob
jna 3.3.0 java 调用 dll/so
通过jni完成java调用c/c++,包含c/c++生成Dll动态库
java调用cmd创建目录和复制文件 在d盘创建一个以自己名字的文件夹 并把c盘windows fonts的楷体字体文件复制到此文件夹下
java调用C/C++过程,供初学者学习
包含多个java调用C/C++的代码实例,以及调用说明文档
java 调用DLL的方法,使用教程http://blog.csdn.net/pplcheer/article/details/52234697
java 以管理员身份调用cmd 需要用到的文件 使用命令方法 "解压路径"nircmd.exe elevate +"你需要用的cmd命令
JAVA通过JNI调用本地方法,而本地方法是以库文件的形式存放的(在WINDOWS平台上是DLL文件形式,在UNIX机器上是SO文件形式)。通过调用本地的库文件的内部方法,使JAVA可以实现和本地机器的紧密联系,调用系统级的...
如果你想知道怎样用java去本地调用C/C++接口,本文档应该是你需要的,特别对于无基础的朋友,无疑是很好的JNI入门指南,希望对你有帮助。
java javascript 调用cmd命令
用户提供一个.net的webservice,本人在java框架中调用此webservice。
ubuntu执行.sh文件,1 .sh文件是Ubuntu下可执行文件,首先需要确定它的权限是否是可执行的,2、 用vim打开sh文件,3、查看文件格式:4. 修改文件格式: 5. 保存退出: 6. 执行:./filename 注意:不可以用sh ...
c# 打压缩包,c++ 调用cmd发送cmd 命令c# 打压缩包,c++ 调用cmd发送cmd 命令c# 打压缩包,c++ 调用cmd发送cmd 命令c# 打压缩包,c++ 调用cmd发送cmd 命令c# 打压缩包,c++ 调用cmd发送cmd 命令c# 打压缩包,c++ ...
提高JAVA删除文件效率,JAVA调用OS命令删除文件代码
这是我的第一个易语言程序。只是一个调用CMD中copy/b命令合并文件的小软件,不过比直接在CMD中使用方便多了!
Java APNS推送消息 HTTP/2协议, 基于JDK 11及以上
java使用(jna)调用c/c++第三方动态库 dll文件 所用jar包
java调用c、c++,运用java.librarys("*.dll")文件,实现java调用c/c++