user411103
user411103

Reputation:

Unable to call Perl script from Java code

I am using JAX-WS in Eclipse (tomcat8 localhost) and I need to call a Perl script from it. Using the code below, I am able to reach the first three lines of Match.pl. However, when it reaches use Stats;, the process returns exitvalue 2.

The Stats.pm file is in the same directory as Match.pl, which has been exported to PERL5LIB with hello.sh script.

What am I doing wrong? Is there a way to use relative paths instead of absolute ones?

@Path("/")
public class MyService {
    @POST
    @Path("/generatePath")
    @Produces(MediaType.TEXT_PLAIN)
    public Response generatePathService(
            @FormParam("url") String fileURL) throws IOException, InterruptedException {
        Process p0 = Runtime.getRuntime().exec("sh /home/me/workspace/MyService/hello.sh");
        p0.waitFor();
        Process p = Runtime.getRuntime().exec("perl /home/me/workspace/MyService/Match.pl");
        p.waitFor();

        return Response.status(200).entity(fileURL).build();
    }
}

Match.pl

#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;
use Stats;

Stats.pm

#!/usr/bin/perl

package Stats;

use strict;
use warnings;

hello.sh

#!/bin/bash

export PERL5LIB=/home/me/workspace/MyService

Upvotes: 0

Views: 240

Answers (1)

Grant McLean
Grant McLean

Reputation: 6998

Try adding:

use FindBin;
use lib $FindBin::RealBin;

This will add the directory that the script is in to the library search path.

Edit: The shell script is not working because it changes the environment of the shell process, not the Java process that spawned it.

Upvotes: 1

Related Questions