HiPownedBi HiPownedBi - 2 months ago 14
Java Question

Run cmd commands through java processBuilder

I am trying to use ProcessBuild to run the cmd statement.

ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "start");
Process p = pb.start();


However, I can only open the cmd.exe

I do not know how to add statement to the ProcessBuild so that the all the jar in the folder can run.
Usually, I open the cmd in the stanford-corenlp-full-2015-12-09 folder, and add this statement to run:
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer

enter image description here

So how to write this statement Run cmd commands through java??
I am getting errors as the statement consists "*".
How to edit the ProcessBuilder so that i can run the statement?
Thanks a lot

Answer

You could set the directory from where the command to be executed

    List<String> cmds = Arrays.asList("cmd.exe", "/C", "start", "java", "-mx4g", "-cp", "*", "edu.stanford.nlp.pipeline.StanfordCoreNLPServer");
    ProcessBuilder builder = new ProcessBuilder(cmds);
    builder.directory(new File("D:/stanford-corenlp-full-2015-12-09"));
    Process proc = builder.start();

UPDATE as requested in comments

    OutputStream out = proc.getOutputStream();
    new Thread(() -> {
        try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out))) {
            bw.write("[command here]");
            bw.flush();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

    }).start();