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 | [reply] [d/l] |
|
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");
}
| [reply] [d/l] |
|
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. | [reply] [d/l] |
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. | [reply] |
|
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."
| [reply] [d/l] |
|
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 );
| [reply] [d/l] [select] |
Re: Calling Java using system()
by BrowserUk (Patriarch) on Jun 24, 2011 at 19:19 UTC
|
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.
| [reply] [d/l] |
|
| [reply] |
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??! | [reply] |
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().
| [reply] |
|
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
| [reply] |