How can i invoke batch file without fork in java? I use NSSM command-line interface to install my product as windows service. I always use Runtime.exec() to operate with command-line. I bypass bytes from new process's streams and periodically check for it finish job. I wonder, if there is easy way to use command line? I don't need forked process, is there a way not to use it? I want just call some exec("command") function, which acts as simple procedure.
1 Answers
You cannot do the equivalent of Unix / Linux exec()
syscall or the Posix shell exec
command ... without forking ... in Java. It is not supported by the standard Java class libraries.
It may be possible to write a native code method to call the OS-provided native libraries to do an exec
or equivalent. However, this could behave in unexpected ways (e.g. no shutdown hooks) and would definitely be non-portable. And in the case of Windows, I don't think it is possible to do the equivalent of UNIX exec
without fork
: source Wikipedia.
However, you can easily run a batch file from Java:
But I don't understand the point of doing an exec
to run a batch file instead of the normal fork
/ exec
... which is what Runtime.exec()
does under the covers. Perhaps if you explained, we could understand why you need to do it, and offer a suggestion.

- 698,415
- 94
- 811
- 1,216