java实现liunx,windows命令的调用

2 篇文章 0 订阅

java实现liunx,windows命令的调用 需要2个类:Process,Runtime

其中我们要了解:

Process exec(String command)    在单独的进程中执行指定的字符串命令。 

Process exec(String command, String[] envp)    在指定环境的单独进程中执行指定的字符串命令。  

Process exec(String command, String[] envp, File dir)    在有指定环境和工作目录的独立进程中执行指定的字符串命令。 

Process exec(String[] cmdarray)   在单独的进程中执行指定命令和变量。   

Process exec(String[] cmdarray, String[] envp)   在指定环境的独立进程中执行指定命令和量。

Process exec(String[] cmdarray, String[] envp, File dir)        在指定环境和工作目录的独立进程中执行指定的命令和变量。

其中

使用Runtime来执行command,生成Process对象 连接到cmd,其中cmd里liunx和windows有一定的区别

代码如下:

package yr;
import java.io.PrintWriter;
public class cmdTe {

        //windows下
public static void windowsCmd() throws Exception {
Process p=Runtime.getRuntime().exec("cmd");//连接windows的cmd
PrintWriter ps=new PrintWriter(p.getOutputStream());//输入命令
ps.println("e:");
ps.println("cd /");
ps.println("mkdir azj");
ps.println("cd azj");
ps.println("echo success > azj.txt");
ps.close();//关闭流
new Thread(new SyncPipe(p.getInputStream(), System.out,null)).start();
}

         //liunx下
public static void liunxCmd() throws Exception {
String [] as={"/bin/sh","-c","ls -l; exit"};//java调用liunx命令是通过shell进行调用的 ,想要输入多个命令如:String [] as={"/bin/sh","-c","ls -l; touch /home/a.txt;exit"}红色调用sh命令(必须的)绿色调用的指令
Process p=Runtime.getRuntime().exec(as);
new Thread(new SyncPipe(p.getInputStream(),null,new StringBuffer())).start();
}
public static void main(String[] args) throws Exception {
liunxCmd();
}
}


package com.azj.cmd;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
class SyncPipe implements Runnable  
{  
    public SyncPipe(InputStream istrm, OutputStream ostrm,StringBuffer s) {  
        istrm_ = istrm;  
        ostrm_ = ostrm;
        StringBuffer = s; 
    }  
   
    public void run() {  
        try{  
            if(null !=ostrm_){
            System.out.println("com windows");//windows cmd
        final byte[] buffer = new byte[1024];  
            for (int length = 0; (length = istrm_.read(buffer)) != -1;){  
            ostrm_.write(buffer, 0, length);//输出命令及显示的结果
            }
            istrm_.close();
            ostrm_.close();
           }else{
          System.out.println("com liunx"); //liunx cmd
          BufferedReader br=new BufferedReader(new InputStreamReader(istrm_));
          String line;
          if((line=br.readLine())!=null){
          StringBuffer.append(line).append("\n");//输出命令及显示的结果
          }
          System.out.println(StringBuffer.toString());
          istrm_.close();
           }
        }  
        catch (Exception e) {  
            throw new RuntimeException("处理命令出现错误:"+e.getMessage());  
        }  
    }  
    private final OutputStream ostrm_;  
    private final InputStream istrm_;  
    private final StringBuffer StringBuffer;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ctrl+C+V程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值