Pregunta

I have a big perl script I want to start through my java program. I have extensively searched on the web for all the possibilities and they did not help me anything further.

I made the following java script for starting my commandline perl script.

try {
    String[] command = {"perl", "C:\\Users\\Rick\\Documents\\Perl\\InDelSub_finder_v0.2.0.pl"};//System.getProperty("user.dir")+"\\src\\InDelSub_finder_v0.2.0.pl", "-h"};
    String[] comm = {"-h"};
    System.out.println(Arrays.toString(command));
    System.out.println(Arrays.toString(comm));
    Process p = Runtime.getRuntime().exec(command, comm);
    p.waitFor();
    try{
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;
        while ((line = in.readLine())!=null){
            System.out.println(line);
        }
    }catch(IOException e2){
        e2.printStackTrace();
    }
    System.out.println("exitValue = " + p.waitFor());
} catch (IOException e1) {
// TODO Auto-generated catch block
    e1.printStackTrace();
} catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

I tried to use a String[] and a String to give to the exec(), but in the end both did not work, it keeps giving me a exitValue() = 255. I also tried (as you can see in the commented away section) that I tried multiple ways to access the right file, but this is not what is going wrong. I think it has something to do with that it can not open the program for some reason.

Could anyone please help me with this? I am really lost in this one and I need it to continue my work.

Thank you very much for your time.

¿Fue útil?

Solución

You are using bad exec signature (that with enviroment variables). Move your -h to command array.

I think that value returned from Perl script not depends on way you call it from Java.

To be sure your script is executed, just try with a very simple script - you can write something on disk to check if it is called, and then you can try with capturing some output to stdout.

Be aware that p.waitFor() can cause deadlock, beceause executed command can wait for you to receive its output to stdout and waitFor will wait until command ends.

Analyse your perl script to find out where it returns code 255.

Fast research gives me information, that Perl exits with code 255 when die function is used - this can be a tip.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top