aedolbey
asked on
calling Java program with complex arguments from Perl script
Experts,
I'm trying to call a Java program from a Perl script. The java program is kept in a .jar file, and requires that several arguments are specified. Part of the challenge is that the arguments themselves are complex, multi-word items with spaces that when called from a bash shell, are surrounded by quotation marks. On a bash command line, it would look like this:
adolbey$ java -jar MyProgram.jar work.db "foo 5 bar == baz" "alpha 10 beta == gamma"
So the question for me is how can I call this from a Perl script, and do so in a way that groups together the multi-item arguments? I've seen examples where within the Perl call, the whole thing is just 'java programName arg1 arg2', and is done simply, with something like this:
system("programName arg1 arg2");
In a case like this, arg1 and arg2 are single tokens, and don't need to be grouped together with quotation marks. But in the case I'm trying to do, several of the args consist of multiple pieces that need to somehow be kept together. How would I do this in the context of using 'system()'?
Any help with this would be greatly appreciated.
Thanks!
Andy
I'm trying to call a Java program from a Perl script. The java program is kept in a .jar file, and requires that several arguments are specified. Part of the challenge is that the arguments themselves are complex, multi-word items with spaces that when called from a bash shell, are surrounded by quotation marks. On a bash command line, it would look like this:
adolbey$ java -jar MyProgram.jar work.db "foo 5 bar == baz" "alpha 10 beta == gamma"
So the question for me is how can I call this from a Perl script, and do so in a way that groups together the multi-item arguments? I've seen examples where within the Perl call, the whole thing is just 'java programName arg1 arg2', and is done simply, with something like this:
system("programName arg1 arg2");
In a case like this, arg1 and arg2 are single tokens, and don't need to be grouped together with quotation marks. But in the case I'm trying to do, several of the args consist of multiple pieces that need to somehow be kept together. How would I do this in the context of using 'system()'?
Any help with this would be greatly appreciated.
Thanks!
Andy
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Regarding the last comment I added, about sending in quoted complex, space-separated arguments, the solutions suggested work, even in the context of running the code with <perl -e '[PERL CODE WITH '"foo bar"' HERE]' > in a bash shell.
Thanks for the quick response, ozo.
Andy
Thanks for the quick response, ozo.
Andy
If you want to avoid using ' in order to simplify using ' in -e from command line
system q(java -jar MyProgram.jar work.db "foo 5 bar == baz" "alpha 10 beta == gamma");
or
system "java", "-jar", "MyProgram.jar", "work.db", "foo 5 bar == baz", "alpha 10 beta == gamma";
system q(java -jar MyProgram.jar work.db "foo 5 bar == baz" "alpha 10 beta == gamma");
or
system "java", "-jar", "MyProgram.jar", "work.db", "foo 5 bar == baz", "alpha 10 beta == gamma";
ASKER
adolbey$ perl -e 'java jar...'
How could the two suggestions just given be done in that context? With perl -e, we're already taking away the possibility of using a single quote with system 'java...'