使用java execute shell命令 [英] use java execute shell command

查看:368
本文介绍了使用java execute shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此类是执行命令的响应,打印结果

this class is response to execute the command ,the print the result

    public class ExecutorTask implements Runnable{

    @Override
    public void run() {

        Process process = null;
        try {
            process = Runtime.getRuntime().exec("cmd /c dir");
             BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
             String line="";
             while ((line = reader.readLine()) != null) {
                 System.out.println(line);
             }
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            return;
        }
    }
}

第二类是执行人运行shell使用线程

the second class is a executor to run the shell use a thread

public final class ShellCommandExecutor{

    public void execute(String command){

        ExecutorTask task = new ExecutorTask();
        Thread executorThread = new Thread(task);
        executorThread.start();

        /*try {
            Thread.sleep(1000);
            executorThread.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
    }

}

问题是为什么我必须在ShellCommandExecutor类中添加代码片段:

the problem is why i must in the class ShellCommandExecutor add code snippet:

try {
        Thread.sleep(1000);
        executorThread.interrupt();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

然后我可以看到打印结果:

then can i see the print result:

2012-08-21  00:32    <DIR>          .
2012-08-21  00:32    <DIR>          ..
2012-08-21  00:32             1,576 .classpath
2012-08-21  00:26             1,224 .project
2012-08-07  10:58    <DIR>          .settings
2012-08-24  15:19            10,965 pom.xml
2012-08-07  10:57    <DIR>          src
2012-08-21  00:32    <DIR>          target
2012-08-24  10:22                 0 velocity.log

为什么?

推荐答案

你创建了一个帖子

 executorThread.start();

如果你什么都不做,启动它的线程(你的主线程)将不会等待你的executorThread在返回之前完成,所以你的应用程序将在此线程执行任务之前退出。

if you do nothing else the thread that started it (your main thread) will not wait for your executorThread to finish before returning, so your application will exit before this thread has executed its task.

要等待你的executorThread完成,你应该致电:

To wait for your executorThread to finish you should call:

executorThread.join();

稍后在代码中。此时,您将确保它已完成任务。

later in the code. At this point you will be ensured that it has finished its task.

目前它的工作原理是因为您在主线程中等待1秒,在此期间您的其他线程执行它的行动。但是如果你的executorThread需要超过一秒的时间来执行它就行不通,所以在这种情况下你不应该 sleep()

Currently it works because you wait for 1 second in your main thread, during this second your other thread performs its action. But if your executorThread needed more than one second to perform it it will not work, so you should not sleep() in this case.

请参阅 Thread.join javadoc。

See Thread.join javadoc.

这篇关于使用java execute shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆