Newer
Older
KisoJikkenNWP / src / main / java / istlab / KisoJikken / CommandRunner.java
package istlab.KisoJikken;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class CommandRunner implements Runnable {
    String command;

    Thread thread;
    Process process;
    // JButton runB;

    public CommandRunner(String cmd) {
        command = cmd;
        // runB = rB;
    }

    public void startstop() {
        if (thread == null) {
            thread = new Thread(this);
            thread.start();
            // runB.setText("Stop");
        } else {
            process.destroyForcibly();
            thread = null;
            // runB.setText("Run");
        }
    }

    @Override
    public void run() {
        // Run script
        // System.out.println(scriptfile);
        List<String> cmds = Arrays.asList(command.split(" "));
        ProcessBuilder processBuilder = new ProcessBuilder(cmds);
        // ProcessBuilder processBuilder = new ProcessBuilder("pwd");
        processBuilder.directory(new File(App.userhome + App.nwpsrc));
        // processBuilder.inheritIO();
        JTAConsole con = new JTAConsole("(Exec) " + command);
        con.Systemoutprintln("== 実行開始 ==");
        con.Systemoutprintln("> cd ~" + App.nwpsrc);
        con.Systemoutprintln("> "+command);

        try {
            process = processBuilder.start();
            Launcher.allProcs.add(process);
            con.startBR(process);

            // BufferedReader reader = new BufferedReader(new
            // InputStreamReader(process.getInputStream()));
            // String line;
            // while ((line = reader.readLine()) != null) {
            // jta.append(line);
            // }
            process.waitFor();
        } catch (IOException | InterruptedException e1) {
            e1.printStackTrace();
        }
        con.Systemoutprintln("== 終了 ==  (ALT+Wで閉じる)");

        thread = null;
        // runB.setText("Run");
    }

}