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.

1
  • 1
    Take a look at the accepted answer to this question 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. Commented Sep 25, 2016 at 1:55

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answers! Could you provide a code snippet, please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.