Solved

Runtime.execute("Some batch file")

Posted on 2004-09-01
4
545 Views
Last Modified: 2007-12-19
To conform with Sarbanes-Oxley, I am writing a Java program that will allow a user to login to an intranet site and request a password for an "Emergeny login Profile" on an AS400.

My code verifies that the user is authorized to access this profile and then changes the password for that profile and divulges it to the user.

I am having problems when executing the call that changes the password.

//code snippet (in a servlet)
...
...
...
    // generate a random 10 charcter password (first must be alphabetic, rest are alphameric)
    profile.switchPassword(userBean.getUserName(), Integer.parseInt(duration), reason);
 
    // Now actually switch it on the AS400
    Runtime run = Runtime.getRuntime();
    String[] commandLine = {"C:\\WINNT\\rmtcmd.exe","chgusrprf", profile.getProfileName(), profile.getPassword()};

    Process proc = run.exec(commandLine);
           
    int exitCode = proc.waitFor();
    if (exitCode != 0){
        throw new Exception("Error changing password on AS/400. Error Code: " + exitCode);
    }
...
...
...

--Batch file contents..
RMTCMD chgusrprf %1 %2
-- End of Batch File Contents


I have the connection properties set up in AS400 Operations to use a default user and prompt for password when necessary.
I have the same user set up on the windows server with the same password.

When I execute the commandline from either the RUN prompt in window, or in a DOS environment, it works fine.
When called via the java code, the password gets changed successfully, but the code seems to hang at the call to proc.waitFor().

After reading the documentation,it is my understanding that the servlet thread waits until the call to the batch file finishes.  The batch file obviously finished because the password got changed.

I need a way of knowing that the call "RMTCMD chgusrprf profilename password" was successful before I divulge the password to the user.

If more info is needed, please let me know.

Any help would be appreciated.

Thanks
0
Comment
Question by:beermequik
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 250 total points
ID: 11953229
It is probably waiting for you to read some output form it...

class StreamGobbler extends Thread
{
    InputStream is;
    String type;

    StreamGobbler(InputStream is, String type)
    {
        this.is = is;
        this.type = type;
    }

    public void run()
    {
        try
        {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
               System.out.println( type + ">" + line ) ;
            } catch (IOException ioe)
              {
                ioe.printStackTrace();
              }
    }
}

then:

    Process proc = run.exec(commandLine);
    StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
     // any output?
    StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
    // kick them off
    errorGobbler.start();
    outputGobbler.start();
    // any error???
    int exitCode = proc.waitFor();
    if (exitCode != 0){
        throw new Exception("Error changing password on AS/400. Error Code: " + exitCode);
    }
0
 
LVL 13

Expert Comment

by:samic400
ID: 11953320
another possible idea is to create a CL with this command in it (CHGUSRPRF) passing in the necessary parms. After the statement has been executed, send back a return value to your program. Enter the CL as a stored procedure.

0
 
LVL 2

Author Comment

by:beermequik
ID: 11953472
That did it!  Thanks

I was trying to read the output streams at one time, but was unsuccessful (I never used Streams outside of files)
Thanks for the code snippet too.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11953496
Glad I could help :-)

Good luck with it!!

Tim
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 79
why cannot we forward request once the response is committed 2 42
spring jars download 1 26
Android development question 2 34
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now