Running exe from java

I understand that the typical code to run exe from java

Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("some.exe"); 

or with parameter

 Process process = new ProcessBuilder("some.exe","param1","param2").start(); 

I have a web application that wraps exe, and users can execute them on request. As I understand it, the above two approaches will always create a new process for each request . I want to avoid this.

Is there a way in which I save a pool of reloadable instances and use them in a typical concurrent script . I want to avoid a situation where each user request starts a new process.

+4
source share
5 answers

It depends on whether this some.exe . For example, the ls (or dir ) ls does its job and stops. You cannot say “do it again” - you need a way to communicate with the process, and the process must have some interface (usually stdio) that you can use to remotely control it.

If your external process cannot do this, you need to create a new process for each request, or you must start the process in another place and save the results in the cache for some time, so several user requests get the same data without you need to start the process .

So, if you get 100 requests per user and a minute, and it’s all right for the data to be one minute, you can start the process once a minute and reuse the old results.

+1
source

This is the beginning of a costly external process, not the creation of a Java Process object.

If your exe implements any server with a protocol for communication, you just start it and use this protocol. But then this question would not have arosen.

If it is a simple tool, for example. what processes the file, you need to run it every time and rely on (or configure) the caching mechanisms of the operating system / file system. Unable to pause or reuse it.

+1
source

Have you considered the Apache Commons Pool ? This should be good enough for this scenario.

0
source

How about Commons Pool combined with Commons Exec ?

0
source

Since you pass parameters on the command line, and the executable subprocess cannot be processed and does not remain constant in memory, your request does not seem very useful - the only thing you can save is to create a process frame, but only to a certain extent.

0
source

Source: https://habr.com/ru/post/1396404/


All Articles