Newer
Older
ServerTester / src / main / java / info / istlab / ServerTester / JExecutor.java
package info.istlab.ServerTester;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class JExecutor implements Runnable {

    Thread thread;
    Process process;
    String absolutePath;
    String commandLineOption;
    String pkgname;
    String javafname;

    /**
     * 引数はコマンドライン
     * 
     * @param p
     * @param comlineOpt
     */
    public JExecutor(String p, String comlineOpt) {
        absolutePath = p;
        commandLineOption = comlineOpt;
        // if (commandLineOption.equals("arg1 arg2 ...")) {
        // commandLineOption = "";
        // }
        thread = new Thread(this);
        // String[] ary;
        // if (File.separator.equals("\\")) {
        // ary = absolutePath.split("\\\\");
        // } else {
        // ary = absolutePath.split(File.separator);
        // }
        // for (int i = 0; i < ary.length; i++) {
        // if (ary[i].equals("src")) {
        pkgname = "info.istlab.ServerTester";
        javafname = p;
        // break;
        // }
        // }
        thread.start();
    }

    @Override
    public void run() {
        ArrayList<String> comlist = new ArrayList<String>();
        // comlist.add("java -D\"file.encoding=UTF-8\" ");
        if (App.isWindows) {
            comlist.add("wt");
        } else {

        }
        comlist.add("java");
        // JARで起動した場合は、クラスパスでJARファイルを指定する
        if (App.isJAR) {
            comlist.add("-cp");
            comlist.add(App.userdir + "/ServerTester-1.0-JARfile.jar");
        }
        comlist.add(pkgname + "." + javafname);
        // コマンドラインオプションをtrimした結果が空文字列なら追加しない
        if (commandLineOption.trim().length() > 0) {
            String[] split = commandLineOption.split(" ");
            for (int i = 0; i < split.length; i++)
                comlist.add(split[i]);
        }

        ProcessBuilder processBuilder = new ProcessBuilder(comlist);
        processBuilder.redirectErrorStream(true);
        JTAConsole con = null;
        if (!App.isWindows) {
            con = new JTAConsole("(Exec) java " + pkgname + "." + javafname + " " + commandLineOption);
            // con.setMainSrcByFileName(absolutePath);
            con.Systemoutprintln("=== 実行開始 ===");
            // con.Systemoutprintln(CommandRunner.prompt + "cd ~" + App.nwpsrc);
            con.Systemoutprintln(CommandRunner.prompt + "java " + pkgname + "." + javafname + " " + commandLineOption);
        }
        if (App.isJAR) {
            File dir = new File(App.userdir);
            System.out.println("[JExecutor:JAR] chdir to " + dir.getAbsolutePath());
            processBuilder.directory(dir);
        } else {
            File dir = new File(App.userdir + "/target/classes");
            System.out.println("[JExecutor] chdir to " + dir.getAbsolutePath());
            processBuilder.directory(dir);
        }
        // processBuilder.inheritIO();
        try {
            process = processBuilder.start();
            if (!App.isWindows) {
                // Launcher.allProcs.add(process);
                con.startBR(process);
            }
            // process.waitFor();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        if (con != null)
            con.Systemoutprintln("=== 実行終了 ===  (ALT+Wで閉じる)");
        thread = null;
    }
}