Java之执行Windows 命令行-yellowcong
来源:互联网 发布:装修设计效果图软件 编辑:程序博客网 时间:2024/06/05 07:26
通过Java程序,来执行Window命令,里面包含了java打开浏览器和java打开文件夹的操作,以级java如何操作获取任务列表
window命令工具
package com.yellowcong.utils;import java.awt.Desktop;import java.io.File;import java.io.IOException;import java.net.URI;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Scanner;/** * window 的工具方法 * @author yellowcong * @date 2016年1月8日 * */public class WindowsCommonUtils { private WindowsCommonUtils(){} /** * 执行命令 * @param common * @return */ public static Process exec(String common){ try { Process process = Runtime.getRuntime().exec(common); return process; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * 通过pid杀死某个进程 * @param pid */ public static void killProcess(int pid){ WindowsCommonUtils.exec("tskill "+pid); } /** * 通过 name杀死某个进程 * @param name */ public static void killProcess(String name){ List<Task> tasks = WindowsCommonUtils.getTask(name); //循环来杀死进程 for(WindowsCommonUtils.Task task:tasks){ WindowsCommonUtils.killProcess(task.getPid()); } } /** * 通过名称来获取任务 * 一个名称可以获取到多个进程 * @param name * @return */ public static List<Task> getTask(String name){ List<Task> tasks = listProcess(); List<Task> result = new ArrayList<WindowsCommonUtils.Task>(); for(Task task:tasks){ if(task.getName().equals(name) ||task.getName().equals(name+".exe")){ result.add(task); } } return result; } /** * 获取所有的Task 列表 * @return */ public static List<Task> listProcess(){ Process process = WindowsCommonUtils.exec("taskList"); Scanner scanner = new Scanner(process.getInputStream()); List<Task> tasks = new ArrayList<Task>(); int index = 0; while(scanner.hasNextLine()){ String str = scanner.nextLine(); if(index >2){ String name = str.substring(0,30).trim(); int pid = new Integer(str.substring(30, 34).replace(" ", "")); String dialogName = str.substring(35,60).trim(); int dialogCount =new Integer(str.substring(60,63).replace(" ", "")); int ramUse = new Integer(str.substring(64,74).replace(" ", "").replace(",","")); tasks.add(new Task(name, pid, dialogName, dialogCount, ramUse)); } index ++; } //tskill scanner.close(); return tasks; } /** * 打开文件管理器 * @param file */ public static void openFileDialog(File file){ if(!file.exists()){ throw new RuntimeException(new Date().toLocaleString()+" 文件路径不存在" ); } if(!file.isDirectory()){ throw new RuntimeException(new Date().toLocaleString()+" 只能打开文件夹,不可以打开文件" ); } try { String osname = System.getProperty("os.name").toLowerCase(); String [] cmd = new String [2]; if(osname.startsWith("windows")){ cmd[0] ="explorer"; }else{ //unix的情况 cmd[0] ="netscape"; } cmd[1] = file.getPath(); //通过cmd的直接 打开,这个 是window的, 没有 想到我们的linux// String [] cmd = new String[]{"cmd","/c","start"," ",file.getPath()}; Runtime.getRuntime().exec(cmd); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 给定一个 http://xx路径 供浏览器打开 * @param url */ public static void openBrowser(String url){ try { Desktop.getDesktop().browse(new URI(url)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //创建内部类 public static class Task{ //任务名称 private String name; //pid private int pid; //对话框 private String dialogName; //对话框数量 private int dialogCount; //内存使用 private int ramUse; public Task(String name, int pid, String dialogName, int dialogCount, int ramUse) { super(); this.name = name; this.pid = pid; this.dialogName = dialogName; this.dialogCount = dialogCount; this.ramUse = ramUse; } public Task() { super(); // TODO Auto-generated constructor stub } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getDialogName() { return dialogName; } public void setDialogName(String dialogName) { this.dialogName = dialogName; } public int getDialogCount() { return dialogCount; } public void setDialogCount(int dialogCount) { this.dialogCount = dialogCount; } public int getRamUse() { return ramUse; } public void setRamUse(int ramUse) { this.ramUse = ramUse; } }}
阅读全文
0 0
- Java之执行Windows 命令行-yellowcong
- Java 执行Windows 命令行
- windows cmd 命令行java (执行.class)
- CentOS之Java安装-yellowcong
- Java之数字格式化-yellowcong
- Java之颜色工具-yellowcong
- Java之二维码工具包-yellowcong
- Java之二分查找-yellowcong
- IT忍者神龟之命令行执行java程序
- Windows命令行执行TestNG
- CentOS之设定界面和命令行启动-yellowcong
- JAVA之Java Decompiler反编译工具-yellowcong
- Java之文件压缩工具类-yellowcong
- Java之字符串工具类-yellowcong
- Java之日期工具类-yellowcong
- Java之文件工具类-yellowcong
- Java之文件下载工具类-yellowcong
- Java之图片裁剪工具类-yellowcong
- 线性表(List)---栈的顺序存储
- 超有用!原生JS实现增加删除class(addClass,removeClass,toggleClass)
- Python:对称的二叉树
- Transfer water
- dnsmasq使用注意事项
- Java之执行Windows 命令行-yellowcong
- Java之字符串工具类-yellowcong
- FTPrep, 17 letter combination of a phone number
- Java之日期工具类-yellowcong
- LuaIde 免费版本
- Java之文件工具类-yellowcong
- 调试视频教程
- Java之文件下载工具类-yellowcong
- Cocos2d 调试