Reputation: 111
I'm trying to pass the arguments from a jar file(samplecode.jar) to a Perl script using
ProcessBuilder pb=new ProcessBuilder("perl", "C:\\Xampp\\perl\\bin\\echocopy.pl", "10");
and in Perl file retrieving the value and passing the value to a jar file(Monitor.jar).The Perl code is
while(my $accNo = <STDIN>){
if(length($accNo) > 1){
$accNo = substr $accNo, 0 , (length($accNo) - 1);
print "value received is $accNo";
my $cmd = "";
my $res = "";
$cmd = "java -jar C:\Newfolder1\Monitor.jar \"$accNo\"";
print "Execution Success";
$res = qx/$cmd/;
print "$res\n";
} else {
printf "1\n";
}
}
The Monitor.jar code is
import java.io.FileWriter;
public class Verifys {
public static void main(String[] args) {
try {
String n = args[0];
System.out.printf("the received value is:\n", n);
FileWriter myWriter = new FileWriter("C:\\Newfolder1\\filename1.txt");
myWriter.write(n);
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (Exception e) {
System.out.println("Not Successfully wrote to the file.");
e.printStackTrace();
}
}
}
and in Monitor.jar I'm trying to receive that value and store it in a file but I'm unable to do it.Could anyone please help me out.
Upvotes: 2
Views: 189
Reputation: 385655
Command-line arguments are found in @ARGV
.
@ARGV == 1
or die("usage: $0 <accNo>\n");
my ($accNo) = @ARGV;
Fixes for a number of issues:
use String::ShellQuote qw( shell_quote );
my @cmd = ( "java", "-jar", "C:\\Newfolder1\\Monitor.jar", $accNo );
my $cmd = shell_quote(@cmd);
my $result = `$cmd`;
die("Can't execute Monitor: $!\n") if $? == -1;
die("Monitor killed by signal ".( $? && 0x7F )."\n") if $? && 0x7F;
die("Monitor exited with error ".( $? >> 8 )."\n") if $? >> 8;
print("Monitor executed successfully.\n");
print($result);
Better yet, avoid the shell:
use IPC::System::Simple qw( capturex );
my @cmd = ( "java", "-jar", "C:\\Newfolder1\\Monitor.jar", $accNo );
my $result = capturex(@cmd);
print("Monitor executed successfully.\n");
print($result);
Of course, if you're really just going to output what you captured, why capture it at all?
my @cmd = ( "java", "-jar", "C:\\Newfolder1\\Monitor.jar", $accNo );
system( { $cmd[0] } @cmd );
die("Can't execute Monitor: $!\n") if $? == -1;
die("Monitor killed by signal ".( $? && 0x7F )."\n") if $? && 0x7F;
die("Monitor exited with error ".( $? >> 8 )."\n") if $? >> 8;
print("Monitor executed successfully.\n");
or
use IPC::System::Simple qw( systemx );
my @cmd = ( "java", "-jar", "C:\\Newfolder1\\Monitor.jar", $accNo );
systemx(@cmd);
print("Monitor executed successfully.\n");
Upvotes: 1