Some Perl lines won't execute from Java Runtime.getRuntime().exec() -
i have strange behavior when calling perl script java.
the perl code is:
#!/usr/bin/env perl use warnings; print "dada\n"; $file = "settings.txt"; open $ds, "<", $file; print "doudou\n"; while ($line = <$ds>){ print "$line\n"; last if $. == 4; } print "dodo\n"; close $ds; as can see, in code want read file "settings.txt" contents :
1 : 2 : b 3 : c 4 : d and script works when called cygwin , output :
dada doudou 1 : 2 : b 3 : c 4 : d dodo but when call java using following code:
string line; string cmd = "perl c:\\data\\tests\\match_settings.pl"; try { process proc = runtime.getruntime().exec(cmd); bufferedreader in = new bufferedreader(new inputstreamreader(proc.getinputstream())); while ((line = in.readline()) != null) { system.out.println("line :" + line); } in.close(); } catch (ioexception a) { // todo auto-generated catch block a.printstacktrace(); } then when java function executed, output:
line :dada line :doudou line :dodo which means $line = <$ds> not executed, don't know why.
any idea ?
sorry long post, wanted precise possible.
ps : know of wondering "why not read settings.txt file java code himself ?" that's not question here ;)
it seems folder perl code executed not same when executing cygwin , java. perl script can't find file.
you should add error checking in perl code, , verify current working dir directory.
i write:
#!/usr/bin/env perl use warnings; use cwd; $cwd = cwd(); print "dada\n"; print "in folder: $cwd"; $file = "settings.txt"; open ($ds, "<", $file) || die "can't open settings.txt. error code: $!"; print "doudou\n"; while ($line = <$ds>){ print "$line\n"; last if $. == 4; } print "dodo\n"; close $ds; this debug why, when script executed java, don't expected results.
you should redirect stderr stdout in java code, error lines.
Comments
Post a Comment