I have looked for solution in stackoverflow and google for this problem however I have not found anything solid that can help me to solve it.
I'm trying to execute a bat file with powershell command using java and then storing the response in StringBuilder but the application just hangs. Here is what I have done
------BATCH FILE------------
Powershell /C "Get-WMIObject -class Win32_ComputerSystem | select username"
------JAVA FILE-------------
import java.io.InputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.*;
//import org.apache.commons.exec.CommandLine;
//import org.apache.commons.exec.DefaultExecutor;
public class Test
{
public static void main(String[] args)
{
try {
String command = "cmd /C .\\scripts\\Get-Username.bat";
System.out.println(executeCommand(command).toString());
}catch(Exception e) {
e.printStackTrace();
}
}
public static StringBuilder executeCommand(String command)
{
String line = null;
StringBuilder responseBuffer = new StringBuilder();
try {
Process process = Runtime.getRuntime().exec(command);
BufferedReader inStreamReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
while((line = inStreamReader.readLine()) != null){
//System.out.println(line);
responseBuffer.append(line + "\n");
}
process.getOutputStream().close();
}catch(IOException ex) {
ex.printStackTrace();
}
return responseBuffer;
}
}
Thank you in advance