use of runtime
Hello can anyone help me? i am trying to execute perl using a java aplication. I use that code:
...
String eje = ("perl ireval.pl -j adhoc.qrels -trec < viperprueba.txt");
Runtime rt = Runtime.getRuntime();
try{
Process p= rt.exec(eje);
BufferedReader br = new BufferedReader (new InputStreamReader(p.getInputStream()));
System.out.println(br.readLine());
System.out.println(br.readLine());
br.close();
p.destroy();
...
the problem is that it doesn't work, perl doesn't execute.
i think that is the fact that i use "<" anyone know someway to resolve it. Thanks
If perl doesn't execute it's because exec doesn't know where to find it. Try specifying the full path.
As for the <, no, that won't work. < > | & and others are interpreted by the shell (e.g. bash, zsh, tcsh...), which is not present when you exec() like this. You can do something like this. Check the docs for your shell of preference for details, and of course use your proper full paths: eje = "/bin/bash -c '/usr/bin/perl ireval.pl -j adhoc.qrels -trec < viperprueba.txt'";
and then exec that.
Also, read this if you haven't already:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
well what doesn't work is that i want to retrieve the information that throws the execution of:
/bin/bash -c '/usr/bin/perl ireval.pl -j adhoc.qrels -trec < viperprueba.txt'
when i put it in the shell it show me the results i whant to view, but when i execute it in a java application whit exec, it doesn't do anything to me because like you said me doesn't undestand <. What i want is a way to say exec to use < viperprueba.txt, and i haven't find it yet. Some idea more? Thanks
I don't know what you mean by "want exec to use < ..."
Exec does not understand <. bash understands it.
You need to narrow down where the problem is.
Try execing this: "/bin/bash -c 'ls > /tmp/zzz'"
Does that work? If not, what exactly happens.
Do you have /bin/bash?
Is perl in /usr/bin?
When you take the exact command /bin/bash -c '/usr/bin/perl ireval.pl -j adhoc.qrels -trec < viperprueba.txt'
into a command line, does that work?
"/bin/bash -c 'ls > /tmp/zzz'"
Does that work? If not, what exactly happens.
when i put it in the method exec it doesn't do anithing and doesn't create the file zzz
Do you have /bin/bash? yes
Is perl in /usr/bin?yes
When you take the exact command
/bin/bash -c '/usr/bin/perl ireval.pl -j adhoc.qrels -trec < viperprueba.txt'
into a command line, does that work? YES
like you see is very rare. this is the code i am using:
import java.io.*;
public class j{
public static void main(String args[]){
// String eje2 = ("perl ireval.pl -j adhoc.qrels -trec < viperprueba.txt");
Runtime rt = Runtime.getRuntime();
try{
Process p= rt.exec("/bin/bash -c 'ls'");
//BufferedWriter bw = new BufferedWriter (new OutputStreamWriter(p.getOutputStream()));
//OutputStream out = p.getOutputStream();
//out.write("< riperprueba.txt".getBytes());
//p.getOutputStream().close();
//p.waitFor();
//bw.write("< viperprueba.txt");
//bw.newLine();
//bw.write("< viperprueba.txt");
//bw.write("ireval.pl -j adhoc.qrels -trec < viperprueba.txt");
//bw.close();
BufferedReader br = new BufferedReader (new InputStreamReader(p.getInputStream()));
System.out.println(br.readLine());
System.out.println(br.readLine());
System.out.println(br.readLine());
System.out.println(br.readLine());
System.out.println(br.readLine());
System.out.println(br.readLine());
br.close();
//p.waitFor();
//System.out.println(p.exitValue() + "ww");
p.destroy();
//rt.runFinalization();
FileWriter fw = new FileWriter("al.txt");
System.out.println("hace algo?");
}catch(IOException e){System.out.println("Error al ejecutar el proceso " + e);}
// catch(InterruptedException e){System.out.println("Error al ejecutar el proceso " + e);}
}
}
you see something extrange. Thanks again for your time