Java 执行Windows 命令行
2015-12-01 16:02
746 查看
有时候,项目需要用Java 语言执行系统命令,Javva 执行命令的方式也非常简单。
【1. 工具类】
【2.常量类 】
【3. 测试类】
【4.源代码下载】Java 操控键盘,鼠标,剪切板Demo
【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
相关文章推荐
- eclipse 给jar包关联javadoc
- Java 操作键盘鼠标
- spring源码分析之spring-jdbc模块详解
- java常用工具类!
- 滚动面板 我的java学习之路 ,滚动面板,多线程
- 一. Spring框架防XXS跨站攻击
- JAVA 遛一遛
- 【菜鸟学Java】4:Servlet 技术
- WIN7下java环境的搭建
- Java 开发@ JDBC链接SQLServer2012
- java适配器模式
- SpringMvc之DispatcherServlet详解
- xssProject在java web项目中应用
- java有车有房有能力最基本运用
- ssh 分页struts2+hibernate3+spring 分页
- 从头认识java-11.3 格式化输出(1)
- javaSE(17)(打印流、转换流、对象流、配置文件和递归)
- eclipse手动导入dtd文件
- struts国际化
- spring初始化