How to supply command line argument to perl script through Java -


i running perl script through java. code shown below.

try {     process p = runtime.getruntime().exec("perl 2.pl");     bufferedreader br = new bufferedreader(                                new inputstreamreader(p.getinputstream()));     system.out.println(br.readline()); } catch (ioexception e) {     // todo auto-generated catch block     e.printstacktrace(); } 

my perl script in such way when run directly through command line ask me supply input file. question how supply file name perl script through java?

if don't want add command line argument script (which cleaner , more robust) need write script's stdin.

this snippet should work (test.java):

import java.io.*;  public class test {     public static void main(string[] args)     {         processbuilder pb = new processbuilder("perl", "test.pl");         try {             process p=pb.start();             bufferedreader stdout = new bufferedreader(                  new inputstreamreader(p.getinputstream())             );              bufferedwriter stdin = new bufferedwriter(                 new outputstreamwriter(p.getoutputstream())             );              //write perl script's stdin             stdin.write("testdata");             //assure that data written , not remain in buffer             stdin.flush();             //send eof closing scripts stdin             stdin.close();              //read first output line perl script's stdout             system.out.println(stdout.readline());          }         catch (ioexception e)         {             e.printstacktrace();         }     } } 

to test can use short perl script (test.pl):

$first_input_line=<>; print "$first_input_line" 

i hoped helped. please have @ following stackoverflow article.

*jost


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -