Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Calling Java using system()

by sophix (Sexton)
on Jun 24, 2011 at 19:02 UTC ( [id://911298]=perlquestion: print w/replies, xml ) Need Help??

sophix has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys,

I would like to execute a Java program using system() in Perl. Although the execution command works fine in the command line, it does not work when I try to execute through a Perl script.

This is the stand-alone command:

c:\users/sophix/Desktop/prottest3/prottest3>java -jar prottest-3.0.jar -i alignment1.aa -t tree1.trees  -o output1.out -all -verbose -all-matrices -threads 3 -G -F -all-distributions -ncat 4

This works fine. Now when I want to embed this into a script to run it through many files, as follows

    my $out = system("/users/sophix/Desktop/prottest3/prottest3/java -jar prottest-3.0.jar -i " . $name . "\.aa" . "-t " . $name . "\.trees" . "-o " . $name . "\.out" . "-all -verbose -all-matrices -threads 3 -G -F -all-distributions -ncat 4");

I get the following error: '/users/sophix/Desktop/prottest3/prottest3/java' is not recognized as an internal or external command, operable program or batch file.

Any idea for why it is not working?

Thanks

Replies are listed 'Best First'.
Re: Calling Java using system()
by angiehope (Pilgrim) on Jun 24, 2011 at 19:40 UTC
    Hi!
    As system takes an array as an argument, I would rewrite your command like this:
    chdir("c:/users/sophix/Desktop/prottest3/prottest3/"); my @external_command = ( "java", "-jar prottest-3.0.jar", "-i ".$name."\.aa", "-t ".$name."\.trees", "-o ".$name."\.out" , "-all","-verbose", "-all-matrices", "-threads 3", "-G", "-F", "-all-distributions", "-ncat 4"); my $out = system(@external_command);
    What exactly would you like to do with your java program?
    cpan:://IPC::Run3 works on various operating systems and allows you to process standard output and error messages from your java program separately.
    Note: added chdir() after reading comment by saberworks Best wishes
      Hi,

      Rewriting did not work.

      The program I am using is called ProtTest - it is a Java application. http://darwin.uvigo.es/software/prottest3/prottest3.html

      I am able to run this program for a single file, but I have a huge number of files and I want to loop them processing one bye one

      So this is the snippet I wrote:

      my @ctl = glob("/users/sophix/Desktop/prottest3/prottest3/*.aa"); my ($ctltrimmed, $ctlfile, $ctlfiletrimmed); foreach my $ctlfile (@ctl) { if ($ctlfile =~ m/prottest3\/prottest3\/(\S+).aa$/) {$ctlfiletrimm +ed = $1;} my $out = system("/users/sophix/Desktop/prottest3/prottest3/java.e +xe -jar prottest-3.0.jar -i " . $ctltrimmed . "-t " . $ctltrimmed . " +\.trees" . "-o " . $ctltrimmed . "\.out" . "-all -verbose -all-matric +es -threads 3 -G -F -all-distributions -ncat 4"); }

        sophix:

        It looks like you ignored saberworks' suggestion. Your java.exe is probably not in the directory you specified. First change directories, then execute the code. Something like (untested):

        chdir("/usrs/sophix/Desktop/prottest3/prottest3"); my @ctl = glob("*.aa"); my ($ctltrimmed, $ctlfile, $ctlfiletrimmed); foreach my $ctlfile (@ctl) { if ($ctlfile =~ m/(\S+).aa$/) { $ctlfiletrimmed = $1; } my $out = system("java -jar prottest-3.0.jar -i " . $ctltrimmed . "-t " . $ctltrimmed . "\.trees" . "-o " . $ctltrimmed . "\.out" . "-all -verbose -all-matrices " . "-threads 3 -G -F -all-distributions -ncat 4"); }

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Calling Java using system()
by saberworks (Curate) on Jun 24, 2011 at 19:26 UTC
    Is the "java" command actually in /users/sophix/Desktop/prottest3/prottest3/ ? Sounds like it's not. If you are running it from that directory so you can use relative path names, you should chdir to that directory first and then do not include it as part of the command path.
      Hi,

      Thank you for your reply.

      No the "java" command but the executable jar file resides in that folder.

      I tried changing the directory as follows:

      my $pwd = cwd(); my $newDIR ="/users/sophix/Desktop/prottest3/prottest3/"; chdir $newDIR or die "Hmm\n"; my $currdir = cwd();

      Now, when I have java or java.exe, it complains: "Unrecognized option: -jar prottest-3.0.jar Could not create the Java virtual machine."

        c:\users/sophix/Desktop/prottest3/prottest3>java

        The ">" shows that you are calling java at the command line with no path specified, yet in your system call you give an absolute path, incorrectly as saberworks pointed out. You seem to have that part resolved.

        -jar prottest-3.0.jar -i alignment1.aa -t tree1.trees -o output1.out + -all -verbose -all-matrices -threads 3 -G -F -all-distributions -nca +t 4

        This works fine. Now when I want to embed this into a script to run it through many files, as follows

        my $out = system("/users/sophix/Desktop/prottest3/prottest3/java -jar prottest-3.0.jar -i " . $name . "\.aa" . "-t " . $name . "\.trees" . "-o " . $name . "\.out" . "-all -verbose -all-matrices -threads 3 -G -F -all-distributions -ncat 4");

        In the system call there are missing spaces between filenames and command line switches. For example between "\.aa" . "-t ".

        I suggest two things to straighten this out. First, don't use concatenation here until you get it working. Then only use concatenation to split the long line to make it more readable. Embed your variables into a long string.

        Second, save the string into a variable, print it so you can see what is going to be called. Then after it looks like expected, pass this varible to the system command.

        One more thing I noticed, you don't need to backslash escape periods in a string. You aren't using these in regexes.

        my $cmd = "java -jar prottest-3.0.jar -i ${name}.aa -t ${name}.trees - +o ${name}.out -all -verbose -all-matrices -threads 3 -G -F -all-distr +ibutions -ncat 4"; print $cmd . "\n"; #system( $cmd );
Re: Calling Java using system()
by BrowserUk (Patriarch) on Jun 24, 2011 at 19:19 UTC

    Try adding '.exe' after the 'java' part. Ie:

    my $out = system( "/users/sophix/Desktop/prottest3/prottest3/java.exe" . " -jar prottest-3.0.jar -i $name.aa -t $name.trees -o $name.out" + . "-all -verbose -all-matrices -threads 3 -G -F -all-distributions + -ncat 4" );

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Hi,

      Having experienced before with some other applications that replacing works, I had tried replacing java with java.exe, and it does not work in this case.

Re: Calling Java using system()
by 7stud (Deacon) on Jun 24, 2011 at 21:12 UTC
    Why are you playing a guessing game with people who are trying to help you? Post the full path to every file mentioned in your program. OKAY??!
Re: Calling Java using system()
by Anonymous Monk on Jun 25, 2011 at 10:43 UTC
    You are using wrong path seperator. Try replacing "/" with "\" and start your path with 'C:\'. And it will work. Better use catfile().
      I ran into this very old post when trying to do such thing also and this is the solution: my $response = system ("java -jar /<complete path to java script + parameters"); At least for people that find this post it will still help

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://911298]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (1)
As of 2024-04-25 00:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found