How to call Perl in Java?

Hi,
Can you please help me in calling Perl inside a Java code?

It should be in such a way that I should be able to get the output of Perl back into Java.

In Java, call Perl with some input arguments
In Perl, run the command that comes from Java and return an output
In Java, get the output from Perl.

Open in new window



Thanks,
TolgarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Use Runtime.exec to execute the Perl interpreter

http://technojeeves.com/joomla/index.php/free/52-runtimeexec
0
TolgarAuthor Commented:
Can you please send me a simple example? such as a perl code that prints "hello world" which is called in a Java code.

Thanks,
0
CEHJCommented:
Sure. Following works fine for me

import net.proteanit.io.IOUtils;

public class PerlPrinter {
    public static void main(String[] args) throws Exception {
        final String[] COMMAND = { "perl", "-e", "print \"Hello World!\n\"" };
        Process p = Runtime.getRuntime().exec(COMMAND);
        IOUtils.outputProcessStreams(p);
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

TolgarAuthor Commented:
@CEHJ: How can I capture the out of the perl command and return it in this code?
0
CEHJCommented:
There are also methods to collect the output into StringBuilder in the same class
(at the link i posted)
0
TolgarAuthor Commented:
So, why do I get null Pointer exception error in this code then:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class RunPerl {

        public static void runcheck() throws IOException {
            String[] perlCmd = { "which perl" };
            Runtime check_runtime = Runtime.getRuntime();
            Process check_process = null;

            try {
                check_process = check_runtime.exec(perlCmd);
                check_process.waitFor();
            } catch (Exception e) {
                System.out.println("error executing " + perlCmd[0]);
            }
            

            /* dump output stream */
            BufferedReader is = new BufferedReader
                ( new InputStreamReader(check_process.getInputStream()));
            String sLine;
            while ((sLine = is.readLine()) != null) {
                System.out.println(sLine);
            }
            System.out.flush();

            /* print final result of process */
            System.err.println("Exit status=" + check_process.exitValue());
            return;
        }
}

Open in new window


And I get this error:

error executing which perl
Exception in thread "main" java.lang.NullPointerException
	at com.xxx.xxx.xxx.xxx.RunPerl.runcheck(RunPerl.java:24)

Open in new window

0
TolgarAuthor Commented:
Line 24 in the error message is actually line 22 in the code I posted above.
0
CEHJCommented:
Because your command array should have two elements, not one. The first is the name of the app (which [must be in $PATH if it's invoked with one word like that]), the second is the argument to that app
0
TolgarAuthor Commented:
ok. I tried a very basic command. Instead of "which perl", I put "dir". I am on a Windows platform and "dir" works on the command line

But I am getting the following error in java:

error executing dir
Exception in thread "main" java.lang.NullPointerException
	at com.xxx.xxx.xxx.xxx.RunPerl.runCheck(RunPerl.java:24)

Open in new window


Line 24 has this in my code:

                ( new InputStreamReader(mwcheck_process.getInputStream()));

Open in new window


do you have any idea why this does not work?
0
TolgarAuthor Commented:
Well, I tried cmd.exe instead of dir and it did not return this error.

So , I guess my code is right. But now, I need to set the env variables and we have an internal command like "setyourenv" and I can run this command on the command line. But this does not work when I call it inside java.

How can I run any command inside Java that I run on command line?
0
CEHJCommented:
You might need to put in the full path to setyourenv. How do you invoke it normally
0
TolgarAuthor Commented:
I would not prefer to put the full path hard coded. Therefore, I want to get the whole environment variable. But I have some problem with it. My other open question is about that problem:

http://www.experts-exchange.com/Programming/Languages/Java/Q_28248134.html#a39518284
0
CEHJCommented:
As long as the command is in the path, then it shouldn't be necessary. As far as the other q is concerned, it's the same solution as your q previous to it - you must use the correct number of array elements to the command
0
CEHJCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.