Kristin.Y Kristin.Y - 2 months ago 6
Perl Question

Run as simple perl script in NetBean using Java language

I am in the progress of learning perl language.
I have made a simple perl script (as shown as below).

#!/usr/bin/perl
use strict;
use warnings;

print "hi Lala\n";


I am trying to run this script in NetBean using java language.
My code is as below:

public class javaProgram {
public static void main(String[] args) {
Process process;

try
{
String testFile = "perl C:\\Strawberry\\perl_tests\\hello_world.pl";
process = Runtime.getRuntime().exec(testFile);
process.getOutputStream();

process.waitFor();
if(process.exitValue() == 0)
{
System.out.println("Command Successful");

}
else
{
System.out.println("Command Failure");
}
}
catch(Exception e)
{
System.out.println("Exception: "+ e.toString());
}
}

}


But I got this error


Exception: java.io.IOException: Cannot run program "perl C:\Strawberry\perl_tests\hello_world.pl": CreateProcess error=2, The system cannot find the file specified


I have save the script as hello_world.pl in the directory as shown above. So, I am not sure what I am doing wrong.
Is it NetBean problem? The script problem? But when I run the script using Strawberry IDE, there is no problem or errors.

Answer

perl is not found. in your script you are refer to #!/usr/bin/perl wich is not existence in a Windows path. So you have to add the complete path to perl Interpreter.

String testFile = "<path_to_perl>perl C:\\Strawberry\\perl_tests\\hello_world.pl";

Also add the correct path in your perl script:

#!<path_to_perl>perl
use strict;
use warnings;

print "hi Lala\n";