您的位置:首页 > 编程语言 > Java开发

Java 执行Windows 命令行

2015-12-01 16:02 746 查看
有时候,项目需要用Java 语言执行系统命令,Javva 执行命令的方式也非常简单。

【1. 工具类】

package org.zgf.robot.util;
/**
* @ClassName: EsecUtil
* @Description: 执行命令行工具类
* @author: zonggf
* @date: 2015年12月1日 下午3:05:37
*/
public class ExcuteCommandUtil {

/**
* @Title: excuteCommand
* @Description: 执行command 命令
* @param command
* @return
* @return: boolean
* @author: zonggf
* @time: 2015年12月1日 下午3:08:04
*/
public static boolean excute(String command){
try{
Runtime.getRuntime().exec(command);
return true;
}catch(Exception ex){
System.out.println("执行命令:" + command + "错误");
}
return false;
}

}


【2.常量类 】

package org.zgf.robot.constant;
/**
* @ClassName: EscConstant
* @Description: 可执行的esc列表
* @author: zonggf
* @date: 2015年12月1日 下午12:25:56
*/
public class CommandConstant {

/** 打开记事本  */
public static String openNotepad = "notepad";
/** 打开命令行  */
public static String openCmd = "cmd";
/** 打开控制面板 */
public static String openControl = "control";
/** 关机命令  */
public static String shutDown = "shutdown -s";

}


【3. 测试类】

package org.zgf.robot.util;

import org.junit.Test;
import org.zgf.robot.constant.CommandConstant;
/**
* @ClassName: Test_ExcuteCommandUtil
* @Description: 测试执行命令
* @author: zonggf
* @date: 2015年12月1日 下午3:10:39
*/
public class Test_ExcuteCommandUtil {

/** 打开命令行   */
@Test
public void test_openCmd(){
ExcuteCommandUtil.excute(CommandConstant.openCmd);
}

/** 打开控制面板   */
@Test
public void test_openControl(){
ExcuteCommandUtil.excute(CommandConstant.openControl);
}

/** 打开记事本   */
@Test
public void test_openNotepad(){
ExcuteCommandUtil.excute(CommandConstant.openNotepad);
}

/** 关机 */
@Test
public void test_shutDown(){
ExcuteCommandUtil.excute(CommandConstant.shutDown);
}

}


【4.源代码下载】Java 操控键盘,鼠标,剪切板Demo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: