This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have contents in memory. Let's say I am storing the string "Hi There" as String str = "Hi There";
I have an executable program that reads contents from a file. I would like to take the contents from memory ("Hi There"), and have it read "from the filesystem" by the executable program, without having to actually write the contents on disk. Something like this:
Process p = Runtime.getRuntime().exec("executable -readfrom file");
Where "file" is a virtual file containing the contents in memory (str).
Can anyone point me in the right direction? I've tried several topics and I'm not sure how to google something like this.
If reaching into the memory space of another program was allowed it would be a HUGE security risk. Operating system designers spend a lot of effort making sure it can't happen.
What exactly do you think you would gain by this hack?
Note that java.io.File has perfectly usable methods for creating and deleting temporary file.
Joined: Oct 16, 2010
I'm sorry maybe the explanation of my problem was not well described.
I am not asking that another executable have access to the memory space of my running program, but rather, does Java have an API for creating virtual files where another program would access one of these virtual files as if it was accessing the file from the file system. Using this virtual file, I could stream data to its handle from content created dynamically in memory or from a database or from the network (etc etc). Since the other executable requires a file or directory as input, it would be fed this information from my running Java process.
The reason one may want to do this is that disk IO is very expensive, and files written to disk are not "dynamic" in nature, as the file must be removed and completely rewritten to the disk. There are a few concrete examples I could give if anyone is interested.
As far as I know, there are just three virtual streams like that. In Java you may know them as System.in, System.out and System.err. Other (console) programs have the same streams, but they may be called differently (e.g., in C they are stdin, stdout and stderr after including stdio.h).
You can use p.getOutputStream() to get an OutputStream. Anything you write to this (remember to flush() to ensure the data is sent) will be available as stdin in the called process. Likewise, anything that the process writes to stdout is available as p.getInputStream(), and anything the process writes to stderr is available as p.getErrorStream(). You can use these to communicate with the process, but only if the process is coded to read from stdin instead of a file.
No matter how you solve this, you should read When Runtime.exec() won't by Michael C. Daconta. The article is almost 10 years old but is still the #1 article to read when using java.lang.Process, even if you don't need p.waitFor().