Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old April 30th, 2008, 02:58 PM
scryptKiddy scryptKiddy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Location: Hawaii
Posts: 256 scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 2 h 55 m 39 sec
Reputation Power: 10
Send a message via Yahoo to scryptKiddy
Process waitFor Exit Value

Getting exit value on this:

JAVA Code:
Original - JAVA Code
  1. import java.io.*;
  2.  
  3. class StreamGobbler extends Thread
  4. {
  5.     InputStream is;
  6.     String type;
  7.    
  8.     StreamGobbler(InputStream is, String type)
  9.     {
  10.         this.is = is;
  11.         this.type = type;
  12.     }
  13.    
  14.     public void run()
  15.     {
  16.         try
  17.         {
  18.             InputStreamReader isr = new InputStreamReader(is);
  19.             BufferedReader br = new BufferedReader(isr);
  20.             String line=null;
  21.             while ( (line = br.readLine()) != null)
  22.                 System.out.println(type + ">" + line);   
  23.             } catch (IOException ioe)
  24.               {
  25.                 ioe.printStackTrace()
  26.               }
  27.     }
  28. }
  29.  
  30.  
  31. public class WinCmd {
  32.    
  33.  
  34.     /**
  35.      * @param args
  36.      */
  37.     public static void main(String[] args) {
  38.                
  39.                 //String adCmd = "C:\\WINDOWS\\SYSTEM32\\cscript.exe C:\\mailEnable.vbs"; //works
  40.                  // String adCmd = "C:\\WINDOWS\\SYSTEM32\\notepad.exe"; //works
  41.                 String adCmd = "runas /smartcard %WINDIR%\\system32\\notepad.exe"; //ExitValue: -1073741819  what is this?
  42.            
  43.                
  44.                     boolean success = false;
  45.                     int exitVal = 1; //0 is success, so we default to a nonzero.
  46.                     try
  47.                     {           
  48.                         String osName = System.getProperty("os.name" );
  49.                         String[] cmd = new String[3];
  50.                         //specify file to use for NT, XP, and Vista
  51.                         if( osName.equals( "Windows NT" ) || osName.equals( "Windows XP" ) || osName.equals( "Windows Vista" ) || osName.equals("Windows 2003") ){
  52.                             cmd[0] = "cmd.exe";
  53.                         }
  54.                         else if ( osName.equals("Windows 95")){
  55.                             cmd[0] = "command.com";
  56.                         }
  57.                         else {
  58.                             System.out.println("Must execute from Windows 95, NT, XP, 2003, or Vista system.");
  59.                             System.exit(1);
  60.                         }
  61.                         cmd[1] = "/C"; //tells os to terminate process when complete.
  62.                         cmd[2] = adCmd;
  63.                         
  64.                         //Get Runtime and execute process
  65.                         Runtime rt = Runtime.getRuntime();
  66.                         Process proc = rt.exec(cmd);
  67.                         
  68.                         
  69.                         // any error message / output?
  70.                         StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");           
  71.                         StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
  72.                         
  73.                         //start output / error catching
  74.                         errorGobbler.run();
  75.                         outputGobbler.run();
  76.                         
  77.                         // wait for process to finish and output exit value.
  78.                         exitVal = proc.waitFor();
  79.                         System.out.println("ExitValue: " + exitVal);
  80.                         System.out.flush();
  81.                         
  82.                         if (exitVal == 0) {
  83.                             success = true;
  84.                         }
  85.                         else {
  86.                             success = false;
  87.                         }
  88.                         
  89.                     } catch (Exception e) {
  90.                         e.printStackTrace();
  91.                     }
  92.     }   
  93. }


Check the lines that have the adCmd initialization.

If I run this from the command line it works with following output:
LOG Code:
Original - LOG Code
    C:\Program Files\Support Tools>runas /smartcard %WINDIR%\system32\notepad.exe Reading smart cards..... Using the card in reader 1.  Enter the PIN: Attempting to start C:\WINDOWS\system32\notepad.exe as user "Signature Certificate" ...


Notepad then opens...

The only thing I can think of is that on the command line it is waiting for me to enter my PIN number, while I do not have the option to do that in my Java app.

Any ideas what that error code is, or how to fix the issue?
Basically, how do you execute Windows commands in Java that require user input?

thanks,

SK

Last edited by scryptKiddy : April 30th, 2008 at 03:07 PM.

Reply With Quote
  #2  
Old April 30th, 2008, 06:54 PM
Yawmark's Avatar
Yawmark Yawmark is online now
Feelin' Groovy
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Aug 2001
Location: WDSMIA
Posts: 7,617 Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 1 Day 12 h 49 m 55 sec
Reputation Power: 1343
Send a message via ICQ to Yawmark Send a message via MSN to Yawmark
Quote:
Basically, how do you execute Windows commands in Java that require user input?

Get the input first, then pass the input to the process.

~
__________________
Yawmark
class Sig{public static void main(String...args){\u0066or(int
\u0020$:"vÌÈÊ\"¤¾Àʲ¬Æ\"v¤Î¤\"²¤¨¸¬Æ".to\u0043h\u0061rArray()
)System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
+(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}

Reply With Quote
  #3  
Old April 30th, 2008, 07:17 PM
scryptKiddy scryptKiddy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Location: Hawaii
Posts: 256 scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 2 h 55 m 39 sec
Reputation Power: 10
Send a message via Yahoo to scryptKiddy
Thanks Yawmark, however, no luck

I took an easy approach and just added my own PIN to the end, in this case I took the adCmd and appended it from this:
...
JAVA Code:
Original - JAVA Code
  1. String String adCmd = "runas /smartcard %WINDIR\\system32\\notepad.exe";
  2. ...


to this:
JAVA Code:
Original - JAVA Code
  1. String myPin = "01234";
  2. String String adCmd = "runas /smartcard %WINDIR\\system32\\notepad.exe " + myPin;
  3. ...


The problem is that the smartcard pin is not an acceptable argument when executing of "runas /smartcard whatever...". I receive a prompt that requests my PIN after the first part executes.

So if I typed: "runas /smartcard %WINDIR%\system32\notepad.exe 01234" on the command line it errors out saying "Invalid RunAS USAGE".


The current logic is this:

1. execute runas with smartcard optoin
2. execute complete
3. smartcard option requests user input
4. smartcard option receives input
5. smartcard option unlocks privateKey from smartcard with input
6. private key validated with smartcard option, returns success
7. success allows runas to execute notepad.exe "as" the smartcard user

So I need to somehow capture the "request for INPUT" that comes back from the /smartcard option, and THEN ask the user for their input...but how do I do that?

It would almost be the same as if I executed a vb / perl / php script, that halfway through execution wanted user input.

Hope that makes sense...

SK


Reply With Quote
  #4  
Old May 1st, 2008, 07:30 AM
Yawmark's Avatar
Yawmark Yawmark is online now
Feelin' Groovy
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Aug 2001
Location: WDSMIA
Posts: 7,617 Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 1 Day 12 h 49 m 55 sec
Reputation Power: 1343
Send a message via ICQ to Yawmark Send a message via MSN to Yawmark
You can also capture the input/output streams of the Process, but it seems to me like you're actually going about this the hard (not easy) way. There are SmartCard I/O APIs available; you may want to check it out.

~

Reply With Quote
  #5  
Old May 2nd, 2008, 02:00 PM
scryptKiddy scryptKiddy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Location: Hawaii
Posts: 256 scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level)scryptKiddy User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 2 h 55 m 39 sec
Reputation Power: 10
Send a message via Yahoo to scryptKiddy
Hey Yawmark, thanks for the reply,

Please do not follow the smartcard logic, my basic question I'm trying to answer is how to prompt user for input when the EXECUTABLE that Java is running (in this case it happens to be the runas command) wants it.

Lets say I wanted to run a perl script from java, and the perl script prompted for user input, how would I do that? What if it was a batch file, what if it was a vb script file, or a Windows executable like above? ...see where I'm going? No API's or anything really needed, I just wanna know how to tell Java to listen for a "request for input" regardless of what language / program is requiring it and allow user to input something, and send it back.

But it doesn't seem to me that the Runtime.exec method allows for interactive (capturing input from user) DURING execution.... and it should, just wish I knew how

thoughts?

SK

Reply With Quote
  #6  
Old May 2nd, 2008, 07:37 PM
Yawmark's Avatar
Yawmark Yawmark is online now
Feelin' Groovy
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Aug 2001
Location: WDSMIA
Posts: 7,617 Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 1 Day 12 h 49 m 55 sec
Reputation Power: 1343
Send a message via ICQ to Yawmark