1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for answering to my question killer_PL, I have tried moving the -h also to the command array, but this did not work. (I did call a small script that only gives back the time, that did work.) However I did not find anywhere that the perl gives this code back with the die function. Thank you very much for that, I can work with this a bit further.
I seriously owe you one man! You saved me. There are die functions in my perl script that gave the code 255 back. I did not understand that it came from my perl instead of from my java.
Don't forget to continously fetch content from running process - do a research for that. waitFor can just hung your application if output buffer will be filled.
I tried the help function to my perl script (the -h in my java) and it did do the thing I wanted it to do. I am now going to change my perl script to the correct format (it is going to take some time). If anything goes wrong I will put something here. Again, thank you, I was working with this for atleast 2 weeks now. Like I said, I owe you one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.