0

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

1 Answer 1

3

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! It works. may i ask one question? How to close cmd of the stanford program by using java?
Sorry, may I know how to close the CMD in this case after I finished using Stanfordserver?????? @Saravana

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.