correct way of setting java classpath in Perl script before java program is called

Posted on 2007-10-11
Last Modified: 2008-01-09
I have a Perl script that calls a Java program, but I am not setting the classpath correctly within Perl, and I need to do so to get this working.  Here is my script:


use POSIX 'sys_wait_h', 'strftime';
use constant JAVA_CLASS => '/nethome/gerdesk/scripts/java/RunSystemCommand';

my $command =  $ENV{'JAVA_HOME'}
               . ( $ENV{'JAVA_HOME'} =~ m#/$# ? '' : '/' )
               . 'bin/java -classpath "/nethome/gerdesk/scripts/java" '
               . +JAVA_CLASS . ' ';

my $file = '/nethome/gerdesk/scripts/logs/javawrapper.log';

print "Java command = $command\n";

open( EXPORTER, "|$command 2>$file" ) or die "Cannot start java command: $!";
#while (defined( my $output = <EXPORTER> )) {
#  $broker->getUI()->traceInfo( $output );
close( EXPORTER ) or warn $! ? "Error closing java command pipe: $!"
                   : "Exit status $? from java command";
if (WEXITSTATUS($?)) {
   open( ERRFILE, $file );
   my $errors = join ("", <ERRFILE>);
   die "Unable to run java command: $errors";
   close( ERRFILE );
} else {
   print "Java command $command executed successfully!\n";

The error I get is this, and I know it is because of the classpath:

Java command = /usr/local/java/bin/java -classpath "/nethome/gerdesk/scripts/java" /nethome/gerdesk/scripts/java/RunSystemCommand
Exit status 256 from java command at line 19.
Unable to run java command: Exception in thread "main" java.lang.NoClassDefFoundError: /nethome/gerdesk/scripts/java/RunSystemCommand

How do I set the classpath correctly in the Perl wrapper script before the java command is called?
Question by:texasreddog
    LVL 92

    Accepted Solution



    /usr/local/java/bin/java -classpath "/nethome/gerdesk/scripts/java"  RunSystemCommand
    LVL 86

    Assisted Solution

    You would of course do that by changing your constant to

    use constant JAVA_CLASS => 'RunSystemCommand';
    LVL 86

    Expert Comment


    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Suggested Solutions

    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now