By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,467 Members | 1,308 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,467 IT Pros & Developers. It's quick & easy.

How to read Java exit code from PERL

P: 4
Hello everyone,

I am new here and very new to PERL, just reading a book on scripting in PERL.

I have a quick need which I hope anyone can kindly help me.

I need to invoke a Java program from my PERL script ( I don't care what the Java code does, it could be a hello world for all I care ). All I need to do is read the exit code of the Java program I invoke.

1) How do I invoke the program from perl ?

2) How do read the exit code from the Java program ? ( ideally would like to store the value in a scalar variable ).

Thanks for helping this newbee.
Jan 30 '08 #1
Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Hello everyone,

I am new here and very new to PERL, just reading a book on scripting in PERL.

I have a quick need which I hope anyone can kindly help me.

I need to invoke a Java program from my PERL script ( I don't care what the Java code does, it could be a hello world for all I care ). All I need to do is read the exit code of the Java program I invoke.

1) How do I invoke the program from perl ?

2) How do read the exit code from the Java program ? ( ideally would like to store the value in a scalar variable ).

Thanks for helping this newbee.
Well, you can execute system commands by using the system() function, or even just enclose them in back tics:

Expand|Select|Wrap|Line Numbers
  1. `script option1 option2`;
  2.  
  3. system(script option1 options);
  4.  
For the exit code, if you are on a Unix system, then you can use one of the Perl Special Variables, specifically something like $? or $@. They are defined about 1/2 way down the page or so.

Hope this helps.

Regards,

Jeff
Jan 30 '08 #2

P: 4
Thanks, it works.

I used it on a UNIX AIX machine.

For those who want to try it, here is my Java Code ( simple Hello World ) :

/*
This is a simple Java program.
Call this file "Example.java".
*/
class Hello {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("This is a simple Java program to test return code.\n");
System.exit(100);
}
}


Notice that I deliberately exit with a code of 100 to see if perl can capture it.

This is my test perl script :


#!/usr/bin/perl
$command = "java Hello";
print "Command is $command:\n";
@args = ("$command");
system(@args);
$retval = $? >> 8;
print "The return code is $?\n";
print "retval is $retval\n";


Under UNIX,

> perl test.pl

RESULTS IN :

Command is java Hello:
This is a simple Java program to test return code.

The return code is 25600
retval is 100
Jan 30 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Well, you can execute system commands by using the system() function, or even just enclose them in back tics:

Expand|Select|Wrap|Line Numbers
  1. `script option1 option2`;
  2.  
  3. system(script option1 options);
  4.  
For the exit code, if you are on a Unix system, then you can use one of the Perl Special Variables, specifically something like $? or $@. They are defined about 1/2 way down the page or so.

Hope this helps.

Regards,

Jeff
system() is the way to go here. Using backtiks in a void context is not considered good perl programming practice.
Jan 30 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.