Runtime class in java

后端 未结 6 966
野性不改
野性不改 2020-12-18 12:03

How to execute a java program with the help of Runtime.getRuntime().exec(). For example we shall have the java file path as c:/java/abc.java. Please help me with the code.

6条回答
  •  再見小時候
    2020-12-18 12:45

    Do not forget that:

    • you may need to read stdout/stderr of a java program
    • you may have to set/update environment variable and PATH before executing your java command

      CreateProcess: c:\j2sdk1.4.0\bin\helloworld error=2

    means Win32's CreateProcess returns a 2 as error code when it cannot find the command you specify; more specifically, when the command does not refer to an executable file on its lookup path.

    Look at this SO question for a more complete "Runtime.getRuntime().exec()" code, and also to this snippet.

    This code creates a shell (as in Runtime.getRuntime().exec("cmd /K")), in which you write on sdtin whatever command you want to execute.

    The interest of this approach is to reuse the shell process to benefit from a previous command: it you execute a 'cd', then execute a 'dir', the latter command would display the content of the directory referenced by the cd command.

    The same would be true for PATH settings, just before using javac or java.

提交回复
热议问题