On our Linux system we use named pipes for interprocess communication (a producer and a consumer).
In order to test the consumer (Java) code, I would like to implement (in Java) a dummy producer which writes to a named pipe which is connected to the consumer.
Now the test should also work in the Windows development environment. Thus I would like to know how to create a named pipe in Windows from Java. In Linux I can use mkfifo (called using Runtime.exec()
), but how should I do this on Windows?
A FIFO, also known as a named pipe, is a special file similar to a pipe but with a name on the filesystem. Multiple processes can access this special file for reading and writing like any ordinary file. Thus, the name works only as a reference point for processes that need to use a name in the filesystem.
Every pipe is placed in the root directory of the named pipe filesystem (NPFS), mounted under the special path \\. \pipe\ (that is, a pipe named "foo" would have a full path name of \\. \pipe\foo ). Anonymous pipes used in pipelining are actually named pipes with a random name.
Named pipes can be used to provide communication between processes on the same computer or between processes on different computers across a network. If the server service is running, all named pipes are accessible remotely.
For one thing, named pipe content resides in memory rather than being written to disk. It is passed only when both ends of the pipe have been opened. And you can write to a pipe multiple times before it is opened at the other end and read.
Use Named Pipes to Communicate Between Java and .Net Processes
Relevant part in the link
try { // Connect to the pipe RandomAccessFile pipe = new RandomAccessFile("\\\\.\\pipe\\testpipe", "rw"); String echoText = "Hello word\n"; // write to pipe pipe.write ( echoText.getBytes() ); // read response String echoResponse = pipe.readLine(); System.out.println("Response: " + echoResponse ); pipe.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
In windows, named pipes exist but they cannot be created as files in a writeable filesystem and there is no command line tool. They live in a special filesystem and can be created only by using the Win32 API.
Looks like you'll have to resort to native code, or switch from pipes to sockets for IPC - probably the best longterm solution, since it's much more portable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With