FAQ
I'm doing one interface in java that will call some perl scripts. I need to
catch the output and I don't know how to do it.

If I execute the 'ls' command my java program finnish if I call the perl my
program don't finnish. What am I missing?

import java.io.DataInputStream;
import java.io.IOException;

public class Test {
public static void main(String[] args) {
try {
Runtime r = Runtime.getRuntime();

String[] aStr = {"-e", "'print(\"Ola\n\") foreach (1..100);'"};

Process p = r.exec("perl", aStr);
//Process p = r.exec("ls");

DataInputStream strm = new DataInputStream(p.getInputStream());

String str;
while ((str = strm.readLine()) != null){
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Thanks
Marcos

Search Discussions

  • R. Joseph Newton at Mar 11, 2004 at 2:43 pm

    Marcos.Rebelo@eurocopter.com wrote:

    I'm doing one interface in java that will call some perl scripts. I need to
    catch the output and I don't know how to do it.

    If I execute the 'ls' command my java program finnish if I call the perl my
    program don't finnish. What am I missing?
    What are you expecting from your call to Perl? Note that shelling out has the
    same dangers with Java as with Perl.

    import java.io.DataInputStream;
    import java.io.IOException;

    public class Test {
    public static void main(String[] args) {
    try {
    Runtime r = Runtime.getRuntime();

    String[] aStr = {"-e", "'print(\"Ola\n\") foreach (1..100);'"};

    Process p = r.exec("perl", aStr);
    You told the system to open the perl compiler here. You may also have Offered
    it 100 lines like this:
    "Ola
    ""Ola
    Which Perl would have a very hard time interpreting as code.
    //Process p = r.exec("ls");

    DataInputStream strm = new DataInputStream(p.getInputStream());

    String str;
    while ((str = strm.readLine()) != null){
    System.out.println(str);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    Thanks
    Marcos
    The code above is straight Java. Although it does inicdentally call the Perl
    interpreter, it is not presented with any further directions, and therefore does
    nothing. It is probably not the right place to discuss the Java Runtime::exec
    function. That would be more appropriate on a Java list [use Google to find
    one] For what it is worth, perl is generally called with a script name or code
    as the first parameter..

    Please learn how to use each language well on its own before you try to develop
    cross-platform programs.

    Joseph
  • Guay Jean-Sébastien at Mar 11, 2004 at 2:54 pm
    Hello Joseph,
    String[] aStr = {"-e", "'print(\"Ola\n\") foreach
    (1..100);'"};
    Process p = r.exec("perl", aStr);
    You told the system to open the perl compiler here. You may also have Offered
    it 100 lines like this:
    "Ola
    ""Ola
    Which Perl would have a very hard time interpreting as code. [...]
    For what it is worth, perl is generally called with a script name or code
    as the first parameter..
    I think you misread. The above calls the following :

    perl -e 'print("Ola\n") foreach (1..100);'

    which is a valid invocation of perl.

    I have no useful information to add, since I do not know why the OP's
    program doesn't work. It's been a long time since I've done Java... And I do
    agree this is probably not the right place to post this, because the perl
    runs well, so the problem is the Java. Still, I thought I'd set the records
    straight.

    J-S
  • Marcos Rebelo at Mar 11, 2004 at 4:18 pm
    I did this question in here because I was aspecting that I was not the first
    crazy man doing something like this. I'm a perl programmer not a java
    programmer.

    I know that I'll have problems calling one from the other like this.
    Probabli the final release will be a Server/Client application to avoid this
    issue and give me the fridum to have interfaces in diferent machines in
    diferent countries, but for now I would be glade to have it simple.

    What I give was a test script, in the real world the script takes 15 minutes
    to 3 hours at least to run.

    I tryed to do the interface with perlTK and the results was not very OS
    like.

    I would be hapy if someone can help me.

    I need to run something like: perl -e 'print "ola\n" foreach (1..100)'.

    Thanks
    Marcos

    -----Original Message-----
    From: R. Joseph Newton
    Sent: Thursday, March 11, 2004 3:48 PM
    To: Marcos.Rebelo@eurocopter.com
    Cc: beginners@perl.org
    Subject: Re: Some java some perl


    Marcos.Rebelo@eurocopter.com wrote:
    I'm doing one interface in java that will call some perl scripts. I need to
    catch the output and I don't know how to do it.

    If I execute the 'ls' command my java program finnish if I call the perl my
    program don't finnish. What am I missing?
    What are you expecting from your call to Perl? Note that shelling out has
    the
    same dangers with Java as with Perl.

    import java.io.DataInputStream;
    import java.io.IOException;

    public class Test {
    public static void main(String[] args) {
    try {
    Runtime r = Runtime.getRuntime();

    String[] aStr = {"-e", "'print(\"Ola\n\") foreach
    (1..100);'"};
    Process p = r.exec("perl", aStr);
    You told the system to open the perl compiler here. You may also have
    Offered
    it 100 lines like this:
    "Ola
    ""Ola
    Which Perl would have a very hard time interpreting as code.
    //Process p = r.exec("ls");

    DataInputStream strm = new
    DataInputStream(p.getInputStream());
    String str;
    while ((str = strm.readLine()) != null){
    System.out.println(str);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    Thanks
    Marcos
    The code above is straight Java. Although it does inicdentally call the
    Perl
    interpreter, it is not presented with any further directions, and therefore
    does
    nothing. It is probably not the right place to discuss the Java
    Runtime::exec
    function. That would be more appropriate on a Java list [use Google to find
    one] For what it is worth, perl is generally called with a script name or
    code
    as the first parameter..

    Please learn how to use each language well on its own before you try to
    develop
    cross-platform programs.

    Joseph

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMar 11, '04 at 12:55p
activeMar 11, '04 at 4:18p
posts4
users3
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase