473,750 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to read Java exit code from PERL

4 New Member
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
3 7195
numberwhun
3,509 Recognized Expert Moderator Specialist
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
limnath
4 New Member
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.ja va".
*/
class Hello {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.prin tln("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
4,059 Recognized Expert Specialist
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

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

Similar topics

6
5950
by: John Smith | last post by:
Hello, I have a rather odd question. My company is an all java/oracle shop. We do everything is Java... no matter what it is... parsing of text files, messaging, gui you name it. My question is this... is Perl so much better at parsing text files and outputing that we would see a substantial speed increase? We process about 10 million records in flat files a day for reformatting before putting them in a DB. Also, when it comes to...
1
3033
by: bezeee | last post by:
At my work we are in the process of building a tool to test an XML based API. Basically, XML in and XML out over http. Currently, there are two engines that do all of the schema validations, xml diffs, sending/receiving, etc. One of these engines in implemented in C# and the other in Java. Now the choice comes down to which scripting language we choose (Perl, Python or Jython) to tie into one of these engines. The scripting language...
3
8172
by: John Smith | last post by:
Hello, I have a rather odd question. My company is an all java/oracle shop. We do everything is Java... no matter what it is... parsing of text files, messaging, gui you name it. My question is this... is Perl so much better at parsing text files and outputing that we would see a substantial speed increase? We process about 10 million records in flat files a day for reformatting before putting them in a DB. Also, when it comes to...
14
3773
by: spike | last post by:
Im trying to write a program that should read through a binary file searching for the character sequence "\name\" Then it should read the characters following the "\name\" sequence until a NULL character is encountered. But when my program runs it gets a SIGSEGV (Segmentation vioalation) signal. Whats wrong? And is there a better way than mine to solve this task (most likely)
92
9358
by: quickcur | last post by:
Hi, can anyone point me a library that can read image to memory from a URL? It is very easy in Java, but it is hard to find an complete solution in c/c++. Thanks,
2
2827
by: ramasubramanian.rahul | last post by:
hi i am trying to call some java APIs from c . i use the standatd JNI calls to load the JVM from a c program and call all java functions by using a pointer to the jvm which was returned by the JNI call the source code is given below and also the errors .. Plz help in resolving these. This is a code being (slightly modified ) which was downloaded from SUN website :
0
5792
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read 17640 every times., and i set byte read=new byte, i have the wav format paper, but i don't know how to read the data correctly into the byte array, and used for doing DFT.... here's my code, and i am little hurry...thanks for the helps first~
8
2149
by: andrew.jefferies | last post by:
Hi, I'm trying to write a simple log parsing program. I noticed that it isn't reading my log file to the end. My log is around 200,000 lines but it is stopping at line 26,428. I checked that line and there aren't any special characters. This is the file reading code segment that I'm using: sysFile=open(sysFilename,'r')
6
5437
by: aureao4 | last post by:
I'm new to Java and programming. I'm trying to code a payroll program and continue getting errors. The program worked last week, this week I have to add set, get and a class. I've written the class but when I compile the program I get 13 errors. Can anyone assist me with this? Thank you! Errors I get: C:\Java>javac *.java Payroll3.java:18: illegal start of type while ( !exit ) ^ Payroll3.java:18: <identifier>...
0
9000
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9339
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.