Jun 11

Running a java program from your shell script is commonplace for Batch operations.  And the wrapper shell script doesnt know about the success/failure of the wrapped Java program unless we return an exit code. 

In most cases, we have the Java source on hand. For others who use a different mechanism to achieve the functionality of the batch operation, I am not sure how far this will be useful to you.

The only possible mechanism (at least that’s what i know) is to give out your exit code using the System.exit (int) method call.  I would suggest you to keep off status codes 0-255 because that is already reserved in Unix platforms (Solaris inclusive).

For the DOS, you use the ERRORLEVEL to catch your exit status.

echo %ERRORLEVEL% should give you a number indicating the exit code of your last executed program.

For bash and ksh, it is $?  as in “echo $?”

For cshell, it is $status as in “echo $status” which returns.  I dont see $? working on cshell (at least on my Solaris box).

When you are calling a Java program from inside a Java program (using Runtime.exec()), the code will look something like this (more elegant, of course) :

Assume the following program is the one which is called

public class WaitAndTerminate {
public static void main(String[] args) {
System.out.println(”Program begin”);
System.exit(1000);
System.out.println(”Program exit”);
}
}

and this is your caller program

 
public class Caller {
public static void main(String[] args) {
BufferedReader inStream=null;
try {
System.out.println(”Caller start”);
Process process=Runtime.getRuntime().exec(”java WaitAndTerminate”);
inStream = new BufferedReader(new InputStreamReader( process.getInputStream() ));
//the waitFor waits for the called program to exit
process.waitFor();
System.out.println(process.exitValue());
} catch (IOException e) {
e.printStackTrace();
}
catch (InterruptedException ie){
ie.printStackTrace();
}
}
}

The getInputStream() method of the Process, as you can see, grabs the output of the called program so that it could be logged.

One other main utility of this mechanism is that we can get to know whether the Java program succeeds completely or not.  In which case, make the last line of your main program return a System.exit(1000) (which indicates that the program is terminated successfully). Any termination in the middle (assuming a system restart or assuming that the called program is a webservice client making a call to a service hosted on a application server which is restarting - My God) will not return a status code of 1000. Ideally, it would return 2 or something like that.

Lets code that into Java now with a bit of modification into our previously written code.


public class WaitAndTerminate {
public static void main(String[] args) {
System.out.println(”Program begin”);
Object object=new Object();
synchronized (object){
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.exit(1000);
System.out.println(”Program exit”);
}
}

The Caller Program is this :
 The destroy() method forcefully terminates the application, giving out a status code of 1.

public class Caller {
public static void main(String[] args) {
BufferedReader inStream=null;
try {
System.out.println(”Caller start”);
Process process=Runtime.getRuntime().exec(”java WaitAndTerminate”);
inStream = new BufferedReader(new InputStreamReader( process.getInputStream() ));
System.out.println(inStream.readLine());
//forceful termination of the called program
process.destroy();
process.waitFor();
System.out.println(process.exitValue());
} catch (IOException e) {
e.printStackTrace();
}
catch (InterruptedException ie){
ie.printStackTrace();
}
}
}

Output :
Caller start
Program begin
1

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis

Tags: , ,

Jun 10

A lot many things in the recent past made me wonder if Adobe is becoming the next Google?  If not in ads, at least on the innovation front.  The beta release of Acrobat.com startled me and finding its features cutting edge almost kept me dumstruck. 

Impressed by the non-proprietory webtop office application Google docs,  a year back, I give it a shot once a month (till now) and I log off with the same feeling everytime that it still has a long way to go.  But everybody knows that it was not google which started it all.

On the contrary,  Buzzword looks more “funky” (apologies for my natural bent over flash applications) and fast (unlike “flash” !!!).  Learnt that there could be a collaborative editing on the documents.

Gave web conferencing a try too. (Adobe Brio what they call it as).  Cool and very much friendly. Features look similar to webex and best of all, its free. Three is the max number of users in the conference.  But I dont personally mind because I dont need to ask my friend to install VNC for helping him out for an issue with his computer.

I dont want to give out a detailed feature list because thats what experts are for

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis

Tags: , , ,

Jun 08

I know I am late to blog about this. But its never too late to do this.

Save the Internet. Save the future.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis

Tags: ,

Jun 04

I almost cried.

http://www.jpboodhoo.com/blog/StayingHumble.aspx

I went through the whole paper provided in the blog link. Never could believe that it was written in 1972.  I am really made humble.

 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis

Tags: ,

Jun 02

The google code project released its RC2 two weeks back and boasts that the framework is currently being used on many live projects.  From what I see, JCatapult, just like Spring, is a collection of frameworks.   And the good thing in it is that it has the power to cater to the technological needs of even a large scale project.

Webwork has always proved that it is the best framework. Struts 2.0, therefore, cant be any better.  Hibernate for ORM, Guice for dependency injection, Freemarker for templating, a brand new feature “Modules” (yet to give it a try) which enables plugging of features into the main webapplication — all sounds very good.

IMHO, there are a couple of notable differences between Spring and JCatapult though JCatapult might have an altogether different vision.

1) Spring gives more flexibility. So, you get to choose whatever set of tools/frameworks you need to use.  I am really not sure how hard will it be now to switch the front end to a desktop application than Struts 2.0.  I dont personally like Spring Webflow but would prefer Struts 2.0 for that. But if i really wanted to use something totally different, Spring will still make sense.

2) JCatapult should be very good for new projects considering the fact that the whole list of frameworks extensively use annotations. The ground reality is that many companies still use Java 1.4 compliant web/application servers. Spring, on the contrary, still supports 1.4 and becomes a first class candidate for “pluggable” modules of the already existing applications.

But still, JCatapult is just in its release candidate and it is not fair enough to compare it with a more mature framework like Spring.  The documentation also gives us clues that it will support more and more frameworks in the future.

But isn’t it “reinventing the wheel”?

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis

Tags: , ,

Give your best to the world.