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

Posted on 2007-10-11
Medium Priority
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 javawrapper.pl 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
  • 2
LVL 92

Accepted Solution

objects earned 500 total points
ID: 20061891


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

Assisted Solution

CEHJ earned 500 total points
ID: 20063459
You would of course do that by changing your constant to

use constant JAVA_CLASS => 'RunSystemCommand';
LVL 86

Expert Comment

ID: 20065212

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month17 days, 3 hours left to enroll

864 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