Friday Feb 22, 2008

Not as easy as we thought - PowerShell from Java Runtime

This week, one of my office colleagues,Vijay asked me " Can we run command of PowerShell from Java Application ?" Microsoft is coming up with a new shell called PowerShell, commands are very similar to that of our Unix Shell. Yes, this problem looks quite simple, I guess Java Runtime class will do that as it do for cmd commands. And for that we wrote one simple code like this :

import java.io.\*;
class OneMore {
public static void main(String[] args)throws IOException
{
          Runtime runtime = Runtime.getRuntime();
           String cmds[] = {"ls"};
           Process proc = runtime.exec(cmds);
           InputStream inputstream = proc.getInputStream();
           InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
           BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
           String line;
           while ((line = bufferedreader.readLine()) != null) {
               System.out.println(line);
           }
}
}

So, this code will print the output of ls on console. But no we need to execute powershell command. So it is required to pass PowerShell in the String cmds[]. Ok, I don't have PowerShell on my machine but let me try to run the same with command.exe(cmd.exe) in place of powershell.exe. So, I have changed one line in the code:

String cmds[] = {"cmd", "ls"};

But then this is not right as there should be right option with cmd like /c or /k. So further it changed into :

String cmds[] = {"cmd", "\\c", "ls"};

Code is running fine and we are done with job. But he want a file in place of command "ls", not a problem pass a file "file.bat" in place of ls. file.bat contains ls :). So, finally code goes something like:

Runtime runtime = Runtime.getRuntime();
           String cmds[] = {"cmd", "\\c", "file.bat"};
           Process proc = runtime.exec(cmds);
           InputStream inputstream = proc.getInputStream();
           InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
           BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
           String line;
           while ((line = bufferedreader.readLine()) != null) {
               System.out.println(line);
           }


Even if we want to check the process is exiting correctly or not, we can add some more lines checking for proc.waitFor() or proc.ExitValue().

But, this piece of code is not running fine for PowerShell. WHY ? No clue(till today, trying to find out why). Unfortunately the program go for a hang. And till now the conclusion is: its either a problem that we are not able to find the right substitute of \\c in PowerShell, though the document is saying "&" will do the same Or maybe PowerShell is stopping us in writing streams(some security issue).

A bad workaround is working fine.

Process proc = runtime.exec("powershell & filename" > out.txt)

We are able to pass the output of PowerShell process into a file out.txt, now reading the file out.txt and print the output on console. But it can create problem if out.txt will go large enough. Can you give me the answer of my WHY ? Why this code is not running for PowerShell. Is there any bug in PowerShell(according to me, yes) ?

About

Hi, I am Vaibhav Choudhary working in Sun. This blog is all about simple concept of Java and JavaFX.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today