日期:2014-05-16  浏览次数:20910 次

java里面执行windows命令

1 ?启动一个java程序并输出日志:

List<String> commadList = new ArrayList<String>();

commadList.add("cmd");

commadList.add("/c");

commadList.add("java -Dfile.encoding=GBK -classpath ./;classes;bin;lib/system/*;lib/user/*;lib/* -XX:PermSize=256m -XX:MaxPermSize=512m -Xms512m -Xmx512m com.sunline.flowbank.cr.core.CRMain");

commadList.add("start");

commadList.add(subsysid);

logger.info("开始启动" +obj.getSubsysId() +"子系统!" );

ProcessBuilder pbuilder = new ProcessBuilder(commadList);

pbuilder.directory(new File(projPath));

proc = pbuilder.start();

?

2 ?关闭一个进程并输出日志

if( "".equals(obj.getProcId()))

{

cmd = "cmd /c wmic process where caption=\"java.exe\" get ?commandline,processid | findstr /C:\"CRMain start "+obj.getSubsysId()+"\"";

}else

{

cmd = "cmd /c wmic process where \"caption='java.exe' and processid='"+obj.getProcId()+"'\" get processid | findstr \""+obj.getProcId()+"\"";

}

Process proc ?= Runtime.getRuntime().exec(cmd);

InputStream is = proc.getInputStream();?

InputStreamReader isr = new InputStreamReader(is, "GBK");?

BufferedReader br = new BufferedReader(isr);?

String line;?

while ((line = br.readLine()) != null) {?

logger.info(line);?

//if(!line.equals(""))

//System.out.println( line.substring(line.lastIndexOf("CRMain start test")+"CRMain start test".length(),line.length()).trim());

if(!line.equals(""))

{

obj.setProcId(line.trim());

?

for(SubsysObj subtmp : subsylist)

{

if(subtmp.equals(obj))

obj.setMsg(subtmp.getMsg());

}

return true;

}

?

obj.setProcId("");

}?

?

3 ?dos下查看和杀死进程

?

查看进程是否在运行

tasklist /nh|find /i "进程名.exe"

ps:双引号中的进程名也可以是部分匹配

?

杀进程:

taskkill /im 进程名

taskkill /pid 进程序号 -t(结束该进程) -f(强制结束该进程以及所有子进程)

wmic process where name="进程名" call terminate

ntsd -c p -p 进程序号

ps:第一种和第三种可同时关闭多个进程名相同的进程

?

4? 查询一个端口是否开启

netstat -aon|findstr 端口号