System Commands - Java

后端 未结 4 2052
南方客
南方客 2020-12-21 23:43

I\'m trying to make a little java program that executes a system command but I can\'t seem to figure it out

The question is how does one execute a system command us

相关标签:
4条回答
  • 2020-12-21 23:53

    You might want to have a look into the documentation for java.lang.ProcessBuilder. An example is given there.

    0 讨论(0)
  • 2020-12-21 23:58

    Process Builder example

    0 讨论(0)
  • 2020-12-22 00:05

    Apache Commons-Exec is similar to ProcessBuilder, but helps avoid a lot of common problems with the streams, process timeout, platform differences, etcetera. I recommend using that instead of ProcessBuilder.

    0 讨论(0)
  • 2020-12-22 00:07

    Ultimately, you use the family of exec() methods in the java.lang.Runtime class. There's a class named java.lang.ProcessBuilder which helps in setting up a process to run, although you're not obligated to use it. Be sure to read this classic article on handling the input and output streams, running Windows CMD.EXE builtins, and other potential pitfalls.

    Running a command can be as simple as saying

    Runtime.exec("notepad");
    

    but as that article points out, there are plenty of subtleties to worry about.

    0 讨论(0)
提交回复
热议问题