5

Perl accepts a scipt via STDIN. After pressing CTRL-D perl knows the "End of Script". After doing so, the script is executed.

Now my question: I wand to do that from Java.

  1. Open Process Perl
  2. Copy Script into STDIN of Perl-Process
  3. HOW DO I SIGNAL PERL THE CRTL-D WITHOUT CLOSING THE STREAM (from within java)
  4. Send some input to the Script
  5. get some Output from Script.

proc = Runtime.getRuntime().exec("perl", env);
commandChannel = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
responseChannel = new BufferedReader(new InputStreamReader(proc.getInputStream()));
InputStreamReader mymacro = new InputStreamReader(getClass().getResourceAsStream("macro.perl"));

char[] b = new char[1024];
int read;

try
{
    while ((read = mymacro.read(b)) != -1)
    {
        commandChannel.write(b, 0, read);
    }
    commandChannel.flush();
    PERL IS WAITING FOR END OF SCRIPT; ??
}  ...
2
  • 1
    What happens if you write __END__ to Perl rather than closing STDIN? (or ^D or ^Z -- see perldoc.perl.org/perldata.html#Special-Literals ) Commented Nov 2, 2011 at 9:10
  • I don't know why, but with a small script it works, but with my larger script not... I think I will write the script to a temporary file and load that... Commented Nov 3, 2011 at 8:17

2 Answers 2

1

But this exactly what CTRL-D does: closing the STDIN of the process. So in your case closing the proc.getInputStream() is the appropriate action to have the expected behavior as you simulate it in the shell.

If you want to do "something else" just do it. Like writing a special command to your script via STDIN which then interprets it as such and jumps into another state or whatever is desired

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

4 Comments

But when I close the Outputstream (to STDIN of my script), I cannot use it any more. In the Shell it is possible. Run perl. enter als Script $line=<STDIN>; print $line; then CTRL-D ... now the script runs and can echo your input
Hmm, my java knowledge is a little bit rusty, but when you say OutputStream you mean proc.getOutputStream() or getInputStream()? Because I think that getInputStream is STDIN of your process.
Make sure that you .write a "\n" (line-break) in your script. This will for $line=<STDIN> to not block anymore.
I tried to run the script via "perl FILENAME" ... works Now I want to embedd the script in a jar file and transfer it to perl via stdin ...
1

this example does exactly what you need:

public class TestPl {
public static void main(String[] args) {
  Process proc;
  try {
     proc = Runtime.getRuntime().exec("perl");
     // a simple script that echos stdin.
     String script = "my $s=<>;\n" +
                     "print $s;";

     OutputStreamWriter wr = new OutputStreamWriter(proc.getOutputStream());

     //write the script:
     wr.write(script);

     //signal end of script:
     wr.write(4);
     wr.write("\n");

     //write the input:
     wr.write("bla bla bla!!!");

     wr.flush();
     wr.close();

     String output = readFromInputStream(proc.getInputStream());
     proc.waitFor(); 
     System.out.println("perl output:\n"+output);
  } catch (IOException e) {
     e.printStackTrace();
  } catch (InterruptedException e) {
     e.printStackTrace();
  }
}

public static String readFromInputStream(InputStream inputStream) throws IOException {
  BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
  String line;
  StringBuffer buff = new StringBuffer();
  while ((line=br.readLine())!=null){
     if (buff.length() > 0){
        buff.append("\n");
     }
     buff.append(line);
  }
  br.close();
  return buff.toString();
}

}

4 Comments

I use a temporary file for the Script now. But I'l try your code ... because I don't like temp files. Thanks.
It does not work. The Script should output an "OK" string. I do not get it -> the script is not started correctly... What's wrong?
Are you shore the "OK" is written to the STD output? maybe the script writes it to STDERR? I used this code with some SQL conversion scripts and it worked but if you want you can send me an example for not working script and I'll check it.
Yes I am sure, because following works (but is not so optimal): Write Script to Tempfile; Run ´PERL mytempfile´

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.