View Full Version : Java and using named pipes

Ramses de Norre
February 28th, 2007, 03:21 PM
Yesterday I was shown the concept of named pipes (also called fifos), but it seems that java can't handle them?
I made a fifo in my home directory with mkfifo clip and wrote a little script to link a purpose to it:


if [ -p $LOC ]
xclip -in < $LOC > out
echo "$LOC doesn't exist or is no named pipe, exiting"
exit 1

This all works perfect, but now the problem:
When I write this in java:
PrintWriter pW=new PrintWriter(new BufferedOutputStream(new FileOutputStream(new File("/home/ramses/clip")))); the application just hangs on that line... No extensive cpu load, no exceptions, just nothing...
If I create just an OutputStream it works, but not with a PrintWriter.
The same line does work on a regular file.

Anyone who can explain this and maybe knows a workaround?
And the file permissions are set to 777 so that isn't the problem, I can also perfectly write to it from the command line.

Ramses de Norre
March 1st, 2007, 11:57 PM

March 2nd, 2007, 12:55 AM
This works for me:

mkfifo pipe
cat < pipe

import java.io.*;

class Test {
public static void main(String[] args) throws Exception {
PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream("/home/jukka/pipe")));

And then java Test in another terminal.

Edit: Don't forget to flush the stream

Ramses de Norre
March 2nd, 2007, 12:20 PM
Strange, I didn't try for a few days and now suddenly it does work...
I activated the fifo with
while(true); do xclip -in < clip > out; done;
And wrote this little java app (it's just a test, I know it isn't good:p)

package ogpTest;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JOptionPane;

public class Clipboard
public static void main(String[] args) throws IOException
String s;
PrintWriter pW=new PrintWriter(new BufferedOutputStream(new FileOutputStream("/home/ramses/clip")));

while (true)
s = JOptionPane.showInputDialog("Give string to put on clipboard");

It doesn't work as it should though... The things pasted on the clipboard aren't always altered, I guess I'll need to look a bit deeper into this.

February 18th, 2010, 06:14 PM
Use printLN instead of print, the newline is important.