-1

In my project i have imported perl scripts . so my project now contains:

  1. A class file consisting of java code to execute a task and
  2. A perl script , which need to be called in the above (1.) class.

The main operation i need to achieve is : i want to call the perl script present in the same project to execute in the mid of the java code.

something like this:

public class Demo_Java{

// ... 
    System.out.println("entered 1st###############");
    Process p = Runtime.getRuntime().exec("perl /javaProject/perlScript.pl");
    p.waitFor();
}

But this is not working , i simply mentioned it for illustrating the requirement..
Is there any way i can call the perl script into the java file ?

EDIT: I tried running the following code :

    process = Runtime.getRuntime().exec("perl C:/javaProject/perlScript.pl ");
    process.waitFor();
    if(process.exitValue() == 0)
    {
        System.out.println("Command execution passed");
    }
    else
    {
        System.out.println("Command execution Failed");
    }

and am getting error as -> command execution failed .. may be this is due to syntax error on Runtime.getRuntime().exec("perl C:/javaProject/perlScript.pl "); , is this correct format to write exec ?

SomeOne
  • 93
  • 8
  • Re "*But this is not working*", How so? What error are you getting? Did you check if the file's permissions? Did you check the shebang line? – ikegami Jan 04 '18 at 08:29
  • OS might be relevant. – ikegami Jan 04 '18 at 08:30
  • 2
    Removing "perl" tag. No reason to believe this has anything to do with the language of the program being called. – ikegami Jan 04 '18 at 08:34
  • yes you right , perl tag not required , my bad .. and about that code , that line itself is not getting computed, its been skipped , and directly next lines are getting executed .. – SomeOne Jan 04 '18 at 08:50
  • OS is relevant , am running eclipse over windows 7(64 bit), if i run the perl script individually in eclipse , its working great . i somehow want to call and make it run in program while java code execution .. Please help , if you know any other way to call it . – SomeOne Jan 04 '18 at 08:54
  • 1
    Your diagnosis is incorrect. Lines aren't skipped. They may not do much, but that's something entirely different. What error are you getting? How do you know the program isn't being executed? – ikegami Jan 04 '18 at 09:22
  • sorry for wrong interpretations , am new to this .. actually , i am not getting any error , Please see in code above i just edited it .. there is a print statement , after that there is this perl script execution statement and then one method .. when i execute the program , the print statement is executed obviously as expected , but after that directly the method is getting executed .. so its not working , is there any other way?? – SomeOne Jan 04 '18 at 09:29
  • How do you know the program didn't get executed? Wouldn't `exec` would have thrown an exception otherwise? – ikegami Jan 04 '18 at 09:31
  • as i said there is no output for that line , am not getting any output for that line in console – SomeOne Jan 04 '18 at 09:33
  • The fact that the program didn't output anything doesn't mean it didn't run. What exit code did you get? – ikegami Jan 04 '18 at 09:33
  • firstly the script is to enter into a linux machine , and execute some commands, you can refer here -> [link](https://stackoverflow.com/questions/47509180/log-into-a-linux-machine-and-execute-multiple-commands) , so it doesn't affect the existing code of java , therefore it wont give any error ,java code run successfully , the main motive here is to trigger that perl script in mid of java operation to achieve some task.. – SomeOne Jan 04 '18 at 09:38
  • You're not answering the questions I'm asking. One last time: What exception was thrown by `exec`, what exception was thrown by `waitFor`, or what exit code was returned by `waitFor`? – ikegami Jan 04 '18 at 09:41
  • am answering the question which you are asking , but you not getting it , anyway i will show you the output am getting to give you some idea , here is the output -> " enterred 1st############### HEREXXXXXXXXXXXXXXXXXXXXX11111111111111111111 Entered method_1 " , am not getting any error or any exit exception , just these print lines – SomeOne Jan 04 '18 at 09:46
  • It's impossible to get neither an exception nor an exit code. (wtf is an "exit exception"?) What exit code was returned by `waitFor`? – ikegami Jan 04 '18 at 09:48
  • 1
    Maybe your Perl script triggers something asynchronously which fails (e. g. because the working directory is incorrect). Please test your Java code with a [simple Perl script (e. g. creating of a new file)](https://perlmaven.com/writing-to-files-with-perl). – howlger Jan 04 '18 at 10:51
  • hi @howlger , i tried with simple perl script , it gives the same error , please see the question again , i have edited it , see in EDIT section , may be the format of writing exec is wrong. can you please guide on that.. – SomeOne Jan 05 '18 at 05:59
  • 1
    Windows paths look like `C:\\path\\file`, by the way – OneCricketeer Jan 05 '18 at 06:03
  • And if the exit value is not 0, then we need to actually see 1) You running the command on the CMD 2) The real return value from that command outside of Java – OneCricketeer Jan 05 '18 at 06:06
  • @cricket_007 that worked great , u rock! , one more question , what if i want to write qualified name of perl script imported in the java project like -> `Runtime.getRuntime().exec("perl javaProject/perlScript.pl ");` where `javaProject/perlScript.pl` is qualified name in eclipse .. this is requirement , because, i no need to change the path every-time i change the location of workspace .. – SomeOne Jan 05 '18 at 07:04

1 Answers1

1

You mention a "syntax error", which probably is the issue considering paths look like "C:\\path\\file" in Windows Java apps.

what if i want to write qualified name of perl script imported in the java project like

Perl is not "imported into" any Java project. Only Java classes are "imported".

As far as Java is concerned, your script is just a plain text file on disk. It's the fact that you execute a OS command that it even knows that it is a script, and this relies on Perl being installed and the perl binary on your PATH, so it really isn't portable...

That being said, Java has no concept of "fully-qualified" file names. It know about "absolute paths", though, which is what I think you mean. However, you should rely more on your classpath of the Java app, not the path on your specific machine.

For example, Difference between getClass().getClassLoader().getResource() and getClass.getResource()?

So, if you had your Perl file on the classpath, you would need to get the absolute path to it. Something along the lines of,

String path = getClass().getResource("/script.pl").toString();
Runtime.getRuntime().exec("perl " + path);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245