Results 1 to 5 of 5

Thread: Java and using named pipes

  1. #1
    Join Date
    Jan 2006
    Location
    Leuven, Belgium
    Beans
    3,414

    Java and using named pipes

    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:
    Code:
    #!/bin/bash
    
    LOC=/home/ramses/clip
    
    if [ -p $LOC ]
    then
            while(true)
            do
                    xclip -in < $LOC > out
            done
    else
            echo "$LOC doesn't exist or is no named pipe, exiting"
            exit 1
    fi
    This all works perfect, but now the problem:
    When I write this in java:
    Code:
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Leuven, Belgium
    Beans
    3,414

    Re: Java and using named pipes

    Someone?

  3. #3
    Join Date
    Feb 2006
    Location
    Finland
    Beans
    189
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Java and using named pipes

    This works for me:

    mkfifo pipe
    cat < pipe

    Code:
    import java.io.*;
    
    class Test {
      public static void main(String[] args) throws Exception {
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream("/home/jukka/pipe")));
        pw.println("hello");
        pw.close();
      }
    }
    And then java Test in another terminal.

    Edit: Don't forget to flush the stream
    Last edited by Ragazzo; March 2nd, 2007 at 01:11 AM.

  4. #4
    Join Date
    Jan 2006
    Location
    Leuven, Belgium
    Beans
    3,414

    Re: Java and using named pipes

    Strange, I didn't try for a few days and now suddenly it does work...
    I activated the fifo with
    Code:
    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)
    Code:
    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");
    			pW.print(s);
    			pW.flush();
    		}
    	}
    }
    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.

  5. #5
    Join Date
    Aug 2006
    Beans
    2

    Re: Java and using named pipes

    Use printLN instead of print, the newline is important.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •