Running Shell scripts through JAva

Hi,

I tried to execute a Shell script through a Java program.

The code I used is as below:

import java.lang.*;

import java.io.*;

public class sys {

public static void main(String[] args) {

Runtime rt = Runtime.getRuntime();

String[] callAndArgs = { "chgpasswd" };

try {

Process child = rt.exec(callAndArgs);

child.waitFor();

System.out.println("Process exit code is:" + child.exitValue());

}

catch(IOException e) {

System.err.println( "IOException starting process!");

}

catch(InterruptedException e) {

System.err.println( "Interrupted waiting for process!");

}

}

The "chgpasswd" shell script calls the "passwd" command of Unix.

It threw out an IO exception.

Then I gave the parameter as "./chgpasswd". It now gave an exit value of 255.

The "chgpasswd" script exists in the same directory as the .class file.

I checkd up the exit code reference...it says exit value of 255 could mean as exit code out of range........an exit code > 255....which unfortunately has no documentation, I guess.

Can you help ?

[1171 byte] By [jsivaraa] at [2007-9-19]
# 1

This is on a Unix platform, correct? Assuming so...

If I remember correctly, exit codes can be greater than 255. To get the real exit code equates to something like: exitcode & 512 to obtain the real exit code.

Make sure "chgpasswd" shell script has the execute permissions set. Additionally, make sure that the first line of the script (the magic line) has the appropriate shell/interpreter to execute specified. For example, if "chgpasswd" were a Bourne (sh) script, the first like would be:

#!/bin/sh

For perl, it may be something like:

#!/usr/bin/perl

It needs to be the absolute path to the shell/interpreter is the point.

cpolizzia at 2007-7-8 > top of java,Core,Core APIs...
# 2

Yes this is on Unix platform.........AIX to be exact.

It is a Korn Shell and the script has only one line....."passwd".

I tried as per you advice. The result of (child.exitValue() ) & 512 is 0.

This means the process exit code is 512, is'nt it ? What does this code mean ?

The script has 777 permission.

Is there any "magic" line for Korn shell ?

jsivaraa at 2007-7-8 > top of java,Core,Core APIs...
# 3

Well.........my basic objective of doing all this is as below:

My application has got a browser based front end and it connects to the database with a single user-id. The Password for this is taken from a file. Now when the password for this user-id, which is a system Id, is changed , then the password has to be overwritten on the file also.

Is this the way all web based systems work ? Like maintaining the user-id and password in a profile and changing them as and when required.

Or is there a way to invoke the Base OS's password change utility........"passwd", in case of AIX, to change the password of through the web based front end itself ?

jsivaraa at 2007-7-8 > top of java,Core,Core APIs...
# 4

You can try magic lines such as:

#!/bin/ksh

#!/usr/bin/ksh

It's been a long time since I worked on AIX, so, I don't remember where most shells sit, but, one of the two above is pretty much the standard.

As for the exit status, I cannot remember the details, and, the status of 255 just might be right. An exit status of 0 means no error, so, I think I told you wrong about the bitwise anding operation. I know it is there, just been a long time.

cpolizzia at 2007-7-8 > top of java,Core,Core APIs...
# 5

> Is this the way all web based systems work ? Like maintaining the user-id and password in a

> profile and changing them as and when required.

Yep, at least these days. It is usually better, from a security standpoint, to have your web-system use a different set of passwords for authentication than the O/S is using for each user; but of course, this might not be an option for you. In any event, try taking a look at the Tomcat 4.1 HOWTO's for realms at http://jakarta.apache.org/tomcat/tomcat-4.1-doc/realm-howto.html

cpolizzia at 2007-7-8 > top of java,Core,Core APIs...