Results 1 to 8 of 8
  1. #1
    aswinkavali is offline Member
    Join Date
    May 2014
    Posts
    10
    Rep Power
    0

    Question Executing perl file from resource folder

    Hi,

    I need to execute a perl file from a resource folder, So that i can able to run the perl file after changing the project to a jar file. And also i need to pass an argument also.

    Is it possible to do that? Could you please post one sample code?


    Thanks,
    Aswin kavali
    Last edited by aswinkavali; 05-27-2014 at 08:54 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: Executing perl file from resource folder

    Is this a java programming problem?
    What have you tried?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    aswinkavali is offline Member
    Join Date
    May 2014
    Posts
    10
    Rep Power
    0

    Default Re: Executing perl file from resource folder

    Hi,

    Normally i will try like this,

    Process p = Runtime.getRuntime().exec("perl file.pl argument");

    But this wont work if i convert this to jar right?

    We need to save that file in a resource folder and if we want to read that file we need to use
    InputStream inputstream = Read.class.getResourceAsStream("/hello.pl");
    InputStreamReader and bufferedreader for reading the file.

    but how to use this method for running this perl file using getruntime() ?

    Thanks,
    Aswinkavali

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: Executing perl file from resource folder

    this wont work if i convert this to jar right?
    Why not?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    thomasboyles is offline Member
    Join Date
    May 2014
    Posts
    1
    Rep Power
    0

    Default Re: Executing perl file from resource folder

    OK. Here's what you have to do:

    Open up the PERL file, determine it's inputs and outputs and/or downstream process points. Next, recode the entire thing as a Java class or classes, then call it where you were trying to invoke the PERL script. Finally, get a copy of either "Sams Teach Yourself Java in 21 Days" or "Python Programming for the Absolute Beginner" and beat the script author over the head with it until they promise to never code PERL again.

    But seriously, there's a discussion of this here:

    Re^3: Running perl from java

    and here:

    java - How to embed a perl script inside a jar file for execution? - Stack Overflow

    You should be able to call the script directly, as long as it's in the classpath. I tend to like the approach within the Stack discussion better. Note that System process execution from Java is not for the faint of heart and could, depending on the facility of the PERL script, be prone to threading issues. Make sure you fully understand the behavior of the PERL script and interpreter as well as the method you choose for calling the process, capturing the output and ensuring a clean exit.

  6. #6
    aswinkavali is offline Member
    Join Date
    May 2014
    Posts
    10
    Rep Power
    0

    Default Re: Executing perl file from resource folder

    Quote Originally Posted by Norm View Post
    Why not?
    Hi Norm,

    i tried it but didnt work. i tried to read a text file also, still the same issue. Later i got this method from one video in youtube.

    Thanks,
    Aswin kavali

  7. #7
    aswinkavali is offline Member
    Join Date
    May 2014
    Posts
    10
    Rep Power
    0

    Default Re: Executing perl file from resource folder

    Quote Originally Posted by thomasboyles View Post
    OK. Here's what you have to do:

    Open up the PERL file, determine it's inputs and outputs and/or downstream process points. Next, recode the entire thing as a Java class or classes, then call it where you were trying to invoke the PERL script. Finally, get a copy of either "Sams Teach Yourself Java in 21 Days" or "Python Programming for the Absolute Beginner" and beat the script author over the head with it until they promise to never code PERL again.

    But seriously, there's a discussion of this here:

    Re^3: Running perl from java

    and here:

    java - How to embed a perl script inside a jar file for execution? - Stack Overflow

    You should be able to call the script directly, as long as it's in the classpath. I tend to like the approach within the Stack discussion better. Note that System process execution from Java is not for the faint of heart and could, depending on the facility of the PERL script, be prone to threading issues. Make sure you fully understand the behavior of the PERL script and interpreter as well as the method you choose for calling the process, capturing the output and ensuring a clean exit.

    Thanks thomas! I can even try to run the perl after copying to the local folder right. In the jar what i have created will actually make a directory and copy some text files from jar itself (after some modification from user input) and then this jar will work on the modified files which is on the local folder. I asked this question because i don't need to modify this perl file before execution.

    regards,
    Aswin kavali

  8. #8
    aswinkavali is offline Member
    Join Date
    May 2014
    Posts
    10
    Rep Power
    0

    Default Re: Executing perl file from resource folder

    Hi,

    i found out a way to do this.

    we need to use the process builder for this

    String drive = "some text";
    ProcessBuilder pb = new ProcessBuilder(drive);
    Process process = pb.start();


    thanks,
    Aswin kavali

Similar Threads

  1. Getting a Resource from within a Jar file
    By kammce in forum Advanced Java
    Replies: 4
    Last Post: 09-24-2012, 10:43 AM
  2. Replies: 1
    Last Post: 08-22-2011, 05:23 PM
  3. Resource File
    By dvlchd3 in forum Eclipse
    Replies: 2
    Last Post: 01-13-2010, 10:32 AM
  4. Executing Perl!
    By hakan123 in forum Advanced Java
    Replies: 3
    Last Post: 12-11-2009, 10:20 PM
  5. Replies: 2
    Last Post: 02-10-2009, 02:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •