calling Java program with complex arguments from Perl script

Posted on 2013-09-25
Medium Priority
Last Modified: 2013-09-26

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.


Question by:aedolbey
  • 2
  • 2
LVL 85

Accepted Solution

ozo earned 2000 total points
ID: 39523723
system 'java -jar MyProgram.jar  work.db  "foo 5 bar == baz"  "alpha 10 beta == gamma"';
system 'java', '-jar', 'MyProgram.jar',  'work.db',  "foo 5 bar == baz",  "alpha 10 beta == gamma";

Author Comment

ID: 39523741
I should've added one more thing.  I'm trying to do this from a bash command line with:

  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...'

Author Closing Comment

ID: 39524580
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.

LVL 85

Expert Comment

ID: 39524628
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");
system "java", "-jar", "MyProgram.jar",  "work.db",  "foo 5 bar == baz",  "alpha 10 beta == gamma";

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question