简体   繁体   中英

Runtime.exec() without fork in java

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.

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM