Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

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,
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Use Runtime.exec to execute the Perl interpreter

http://technojeeves.com/joomla/index.php/free/52-runtimeexec
Avatar of Tolgar
Tolgar

ASKER

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,
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tolgar

ASKER

@CEHJ: How can I capture the out of the perl command and return it in this code?
There are also methods to collect the output into StringBuilder in the same class
(at the link i posted)
Avatar of Tolgar

ASKER

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

Avatar of Tolgar

ASKER

Line 24 in the error message is actually line 22 in the code I posted above.
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
Avatar of Tolgar

ASKER

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?
Avatar of Tolgar

ASKER

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?
You might need to put in the full path to setyourenv. How do you invoke it normally
Avatar of Tolgar

ASKER

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:

https://www.experts-exchange.com/questions/28248134/How-to-convert-environment-variable-map-from-getenv-to-string-array-for-of-name-value.html?anchorAnswerId=39518284#a39518284
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
:)