*
The moose likes Java in General and the fly likes Implementing runAs in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Implementing runAs in Java" Watch "Implementing runAs in Java" New topic
Author

Implementing runAs in Java

Sumiran Pradhan
Greenhorn

Joined: Mar 10, 2010
Posts: 12
Hello

I need to implement the runAs functionality using Java.The problem is that runAs does not accept password in command line but as a prompt. I have a small code snippet that I tried.Can some one help me with this.

Process p = Runtime.getRuntime ( ) .exec
( "cmd /C cmd.exe" ) ;
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
Thread.sleep(1000);
while (in.ready()) {

String x = in.readLine();
System.out.println("The value is :::" + x);
if(x.contains("password"))
{
Thread.sleep(1000);
System.out.println("Inside loop...");
//OutputStreamWriter op = new OutputStreamWriter(p.getOutputStream());
String cmd="global@123";
p.getOutputStream().write(cmd.getBytes());
p.getOutputStream().flush();
//op.write(cmd, 0, cmd.length());
//op.flush();
Thread.sleep(1000);
}
}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Please UseCodeTags. You can edit your post to add them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sumiran Pradhan
Greenhorn

Joined: Mar 10, 2010
Posts: 12
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
You dont say what problem you encountered so I am going to take a guess.

Carefully review the JavaDocs for java.lang.Process

Pay close attention to the need for consuming the stdOut and stdErr streams generated by the process.

This sort of problem comes up frequently with exec() so search this forum.

Bill
Sumiran Pradhan
Greenhorn

Joined: Mar 10, 2010
Posts: 12
My problem is that I am unable to enter the password through my Java Code for runAs implementation.I have tried the same on a single thread.Then I read this article in the forum regarding consuming error streams and input streams.Here is what I tried and its still not working.


Output is :->
Execing cmd.exe /C runAs /user:testAdmin notepad.exe
OUTPUT>Enter the password for bmcadmin:
ExitValue: 1

Now Exit Value 1 would mean an error but I am unable to debug as to what is the problem in this case.Ne help guys?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Seems to me that runAs is expecting to conduct a conversation - shouldn't you be reading the output and responding with the password after it is requested.

Bill
Sumiran Pradhan
Greenhorn

Joined: Mar 10, 2010
Posts: 12
Okay guys , again I have modified the code and I am still unable to do what I set about doing. I need to implement the runAs command through Java.The problem is that it prompts for a password which one cannot provide in the runAs command.Any help would be great.

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
For one thing:


Reads a "line" as defined by a line terminator - presumably \n BUT the prompt does NOT have a terminator.

I suggest you start fresh, think about what has to be recognized in the process output and the sequence of events. I would even draw some sort of state diagram showing the various states the process can be in and the transitions.

You may well end up with several separate objects run by separate Threads.

Those monster methods are not making good use of Java and you are getting all balled up trying to cram everything into them.

Bill
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The password is read from the raw console device, and doesn't go through stdin at all. Anything along the lines of what's been suggested here will never work.

Here are a few pages that describe available workarounds:

http://blogs.msdn.com/oldnewthing/archive/2004/11/29/271551.aspx
http://www.tek-tips.com/faqs.cfm?fid=2760

[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementing runAs in Java