0

Is it possible to create a "virtual" file in Java and to pass it as parameter to a Process/ProcessBuilder?

Let's say I want to call an external .exe file (Windows) with a parameter arg. arg must be a file on the hard disk. Consider following code as example:

Process p = Runtime.getRuntime().exec("someProgram.exe -file " + arg)

So, is it possible to create arg as "virtual" file and pass it in such way to the external process in Java? If so, how could I implement it? I would prefer to avoid writing the file to the hard disk at first because HDD I/O is quite slow.

Bastian
  • 1,553
  • 13
  • 33
  • 1
    Take a look at the accepted answer to [this question](http://stackoverflow.com/questions/18903549/writing-to-inputstream-of-a-java-process) to get an idea of how you might do something like this. This is not an exact answer to your question but an example of how two processes can communicate using standard input and output. Your external program would need to be capable of accepting input via standard input as Will Hartung pointed out in his answer. Since I assume you can't modify the external program's code you may have no choice but to write to a file. – D.B. Sep 25 '16 at 01:55

1 Answers1

0

If you have the data already, and the external process can accept the data from standard input, you can stream the data directly in to the process, skipping the file writing entirely.

You can get the input stream from the Process, and look into Pipe[Input|Output]Stream in Java to tie it all together.

Will Hartung
  • 115,893
  • 19
  • 128
  • 203