Running the following simple program:
ProcessBuilder pb = new ProcessBuilder("powershell.exe", "-Command", "dir");
Process p = pb.start();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while((line = bufferedReader.readLine()) != null){
System.out.println(line);
}
System.out.println("Exit");
Never reaches the "Exit" println - just hangs infinitely on the readLine() method. I understand this is (most probably) caused by the fact that powershell does not output \n in the last line and readLine is not sure whether the end has been reached or not. Is there a way to get over this issue and read the input stream correctly? BTW. inheritIO method on processbuilder resulted in the same issue...
UPDATE
This:
ProcessBuilder pb = new ProcessBuilder("powershell.exe", "-Command", "dir");
pb.redirectErrorStream(true);
Process p = pb.start();
// BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
// String line;
// while((line = bufferedReader.readLine()) != null){
// System.out.println(line);
// }
p.waitFor();
System.out.println("Exit");
Also hangs infinitely...
Powershell isn't ending. I would probably use Java to list a directory, but this should work with your example.
ProcessBuilder pb = new ProcessBuilder("dir");
I had a problem with the processbuilder hanging on readline()
Specifically, windows 10, running a cmd.exe command that starts a bash and runs a script.
The problem was fixed by closing the input of the process:
Process process = pb.start();
process.getOutputStream().flush();
process.getOutputStream().close();
Not sure what the problem is. I tried making a new project with your code (added in a few try-catch and print statements), and it works for me. Here is what I used;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class temp {
public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder("powershell.exe", "-Command", "dir");
Process p;
try {
p = pb.start();
} catch (IOException e) {
System.out.println("Failed to start powershell");
return;
}
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
System.out.println("Begin!");
try {
while((line = bufferedReader.readLine()) != null){
System.out.println("reading...");
System.out.println(line);
}
} catch (IOException e) {
System.out.println("Failed to read line");
return;
}
System.out.println("Exit");
}
}
And here is the resulting console output,
Begin! reading...
reading...
reading...
Directory: C:\Users\AbrahamV\workspace\201_stackoverflow reading...
reading...
reading... Mode LastWriteTime Length Name reading...
---- ------------- ------ ---- reading... d---- 12/10/2013 9:29 PM bin reading... d---- 12/10/2013 9:27 PM src reading...
-a--- 12/10/2013 9:27 PM 232 .classpath reading...
-a--- 12/10/2013 9:27 PM 393 .project reading...
reading...
Exit
The output wasn't instantaneous. Took a few moments before anything was printed out.
Related
I created the below method to execute a Linux command:
public void executeGetLogs(){
try{
Runtime rt = Runtime.getRuntime();
String [] commands = {helper.getPythonPath(), helper.getLogsMainScript(), helper.getLogsManagementUrl(), helper.getLogsManagementUrlPrefix(), helper.getLogsManagementUsername(), helper.getLogsManagementPassword(), helper.getLogsNet(), helper.getLogsStorageUrl(), helper.getLogStorageUsername(), helper.getLogStoragePassword()};
Process proc = rt.exec(commands);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
// read the output from the command
logger.debug("Standard output from execution of get_logs:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
logger.debug(s);
}
// read any errors from the attempted command
logger.debug("Standard error from execution of get_logs (if any):\n");
while ((s = stdError.readLine()) != null) {
logger.debug(s);
}
}
catch(IOException e){
logger.debug("Execution exception: " + e);
}
}
The method appears to start working correctly but then fails.
The debug shows the following output:
2017-04-21 12:27:42,391 DEBUG Standard output from execution of get_logs:
2017-04-21 12:27:44,360 DEBUG 411 new files found
2017-04-21 12:27:44,363 DEBUG Downloading files...
2017-04-21 12:27:44,446 DEBUG Standard error from execution of get_logs (if any):
What I would expect to see is
2017-04-21 12:27:44,360 DEBUG 411 new files found
2017-04-21 12:27:44,363 DEBUG Downloading files...
Downloaded 10 of 447
Downloaded 20 of 447
Downloaded 30 of 447
and so on until Downloaded 447 of 447.
I can also see that nothing gets downloaded.
My command runs when I run it in a terminal.
Is it possible something in the Java causes it to exit? One thing is that it can take a few seconds to process each block of 10. Is it possible the
while ((s = stdInput.readLine()) != null) {
logger.debug(s);
}
just sees a null because the stdInput hasn't appeared yet so it exits the loop? If so how can I fix this?
A much better way of executing code in java is ProcessBuilder. Example:
//Create the command.
ProcessBuilder command = new ProcessBuilder(helper.getPythonPath(), helper.getLogsMainScript(), helper.getLogsManagementUrl(), helper.getLogsManagementUrlPrefix(), helper.getLogsManagementUsername(), helper.getLogsManagementPassword(), helper.getLogsNet(), helper.getLogsStorageUrl(), helper.getLogStorageUsername(), helper.getLogStoragePassword());
//Will show console output in java console.
command .inheritIO();
//Launches de command.
Process process= command .start();
Hope it helps.
Got to the bottom of it. I need to wait for the process to end. This can be done with the below code:
proc.waitFor();
My method now looks like this:
public void executeGetLogs(){
try{
Runtime rt = Runtime.getRuntime();
String [] commands = {helper.getPythonPath(), helper.getLogsMainScript(), helper.getLogsManagementUrl(), helper.getLogsManagementUrlPrefix(), helper.getLogsManagementUsername(), helper.getLogsManagementPassword(), helper.getLogsNet(), helper.getLogsStorageUrl(), helper.getLogStorageUsername(), helper.getLogStoragePassword()};
Process proc = rt.exec(commands);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
try{
proc.waitFor();
}
catch(InterruptedException e){
logger.debug("Interrupted Exception : " + e);
}
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
// read the output from the command
logger.debug("Standard output from execution of get_logs:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
logger.debug(s);
}
// read any errors from the attempted command
logger.debug("Standard error from execution of get_logs (if any):\n");
while ((s = stdError.readLine()) != null) {
logger.debug(s);
}
}
catch(IOException e){
logger.debug("Execution exception: " + e);
}
}
My problem is, after opening cmd from java code, i want user to be able to input like in c++ ms dos applications. When user writes sth such as "dir" or "cd..", i want to execute these codes by java.
The problem is for every command java re-opens cmd again. Also i cannot execute commands. My cmd start code is below ;
final ArrayList<String> commands = new ArrayList<>();
commands.add("cmd.exe");
commands.add("/C");
commands.add("start");
ProcessBuilder pb = new ProcessBuilder(commands);
Process process = pb.start();
Here's some cleaned up code from How to open the command prompt and insert commands using Java?
public static void main(String[] args) {
try {
String ss = null;
Runtime obj = null;
Process p = Runtime.getRuntime().exec("cmd.exe");
//write a command to the output stream
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writer.write("dir");
writer.flush();
//Get the input and stderror
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
System.out.println("Here is the standard output of the command:\n");
while ((ss = stdInput.readLine()) != null) {
System.out.println(ss);
}
System.out.println("Here is the standard error of the command (if any):\n");
while ((ss = stdError.readLine()) != null) {
System.out.println(ss);
}
} catch (IOException e) {
System.out.println("FROM CATCH" + e.toString());
}
}
The uploaded Screenshot conatains the start_client.bat file content, viewed in notepad++ editor.
Currently am invoking start_client.bat on local machine it works fine but when the same bat file is invoked on server it pops up a window on server and it needs manual closure after execution. Any way to force bat file execution on server without window poppping up.
private void invokeSeagull(String flag) throws Exception
{
String path="";
if(flag.equals("Start"))
{
path="cmd /c start D:/Seagull/TIB/start_client.bat";
}
if(flag.equals("Stop"))
{
path="cmd /c start D:/Seagull/TIB/stop_client.bat";
}
try {
String line;
Process p = Runtime.getRuntime().exec(path);
p.waitFor();
BufferedReader bri = new BufferedReader
(new InputStreamReader(p.getInputStream()));
BufferedReader bre = new BufferedReader
(new InputStreamReader(p.getErrorStream()));
while ((line = bri.readLine()) != null) {
System.out.println(line);
}
bri.close();
while ((line = bre.readLine()) != null) {
System.out.println(line);
}
bre.close();
p.waitFor();
System.out.println("Done.");
}
catch (Exception err) {
err.printStackTrace();
}
}
This code snippet should run batch file, of course if you use Windows.
Runtime.getRuntime().exec("cmd /c start {pathToFile}");
As pointed out be ssedano the correct way to execute shell commands in Java is the Process-builder:
// Just the name of an executable is enough
final ProcessBuilder pb = new ProcessBuilder( "test.bat" );
pb.redirectError( Redirect.INHERIT );
pb.redirectOutput( Redirect.INHERIT );
System.out.println( String.format( "***** Running Process %s OUTPUT:", pb.command().toString() ) );
final Process process = pb.start();
process.getOutputStream().close();
final int returnCode = process.waitFor();
System.out.println( "***** Process Exited with Returncode: " + returnCode );
You can just redirect STDERR and STDOUT of the bat-file, so you will get all output in the Server-Output console. And you should close STDIN of the bat-file, so it will exit and not get stuck on the pause command at the end!
You could use the new in Java 7 ProcessBuilder
A simple example:
String[] command = {"CMD", "/C", "dir"};
ProcessBuilder probuilder = new ProcessBuilder(command);
// Set up your work directory
probuilder.directory(new File("c:\\stackoverflow"));
Process process = probuilder.start();
// Read output
try (InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);)
{
String line;
System.out.printf("Output of running %s is:\n", Arrays.toString(command));
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
// Wait to get exit value
int exitValue = process.waitFor();
}
catch (Exception e)
{
// Fail
}
This should run in the silent mode :
Runtime.getRuntime().exec(new String[] {"cmd", "/pathto/start_client.bat"});
The following code snippet i had given is using exec function and executes hello program (simple "hello world" printing java program). But as soon as i execute the main program, print statement of instream.readline() simply returns NULL. Please try to sort out the problem. Hope the explanation is clear.
CODE:
Process process2=null;
BufferedReader inStream=null;
try
{
process2 = Runtime.getRuntime().exec("java hello");
}
catch(IOException e1)
{
System.err.println("Error on exec method");
e1.printStackTrace();
}
try
{
inStream = new BufferedReader(new InputStreamReader(process2.getInputStream() ));
System.out.println(inStream.readLine());
}
catch(IOException e1)
{
System.err.println("Error on inStream.readLine()");
e1.printStackTrace();
}
First of all your hello.java should be already compiled n the class file should present in the current directory where the program is located.
And for getting errors, you can get error stream from process class's object.
BufferedReader stdError = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
String s="";
while ((s = stdError.readLine()) != null)
System.out.println(s);
Working with Eclipse/java7/windows
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ProcessDemo {
public static void main(String[] args) throws Exception {
final String dir = System.getProperty("user.dir");
System.out.println("current dir = " + dir);
Runtime run = Runtime.getRuntime();
Process pr=run.exec("javac -d "+ dir +"\\src "+ dir+"\\src\\HelloDemo.java");
pr.waitFor();
BufferedReader stdError = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
String s;
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null)
System.out.println(s);
//read output
while ((line=buf.readLine()) != null)
System.out.println(line);
pr.destroy();
Runtime run1 = Runtime.getRuntime();
Process pr1=run1.exec("java -cp "+dir+"\\src HelloDemo");
BufferedReader stdError1 = new BufferedReader(new InputStreamReader(pr1.getErrorStream()));
BufferedReader buf1 = new BufferedReader(new InputStreamReader(pr1.getInputStream()));
//interpretting file n executing it line by line :D :P
pr1.waitFor();
String temp;
// read any errors from the attempted command
System.out.println("\n\nHere is the standard error of the command (if any):\n");
while ((temp = stdError1.readLine()) != null)
System.out.println(temp);
//read output
System.out.println(buf1.readLine());
while ((temp=buf1.readLine()) != null)
System.out.println(temp);
}
}
I want to get the IP address of logged in user of the telnet session
So I have written the following code:
Process p1 = Runtime.getRuntime().exec(new String[] {"/usr/bin/who", "-m"});
p1.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p1.getInputStream()));
String line = reader.readLine();
System.out.println("line=" + line);
reader.close();
But I am getting the output as "null".
Where as expected output is:
linus pts/1 Dec 10 03:48 (172.21.235.48)
In this case you shouldn't pass the -m option.
This works for me :
Process p1 = Runtime.getRuntime().exec(new String[] {"/usr/bin/who"});
Try to consume the input stream from the process before calling waitFor().
You can create a shell script which will receive parameter (option) from your java program.
Then you can run your shell script from java like this -
ProcessBuilder pb = new ProcessBuilder("/PATH/test.sh","-m");
String line;
Process process=pb.start();
java.io.InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
return br;
Now in test.sh, you cab grab the argument and run the command -
/usr/bin/who $1 (needs to check, not sure)
Hope this helps.
Thanks guys for the replies.
From the reply of "dystroy" I understood that -m was creating problem.
I tried --m and it worked :)
Please try this code. It's working for me and returns the same output as who -m UNIX command.
import java.io.*;
public class UserPB
{
public static void main(String[] args)
{
try {
// ProcessBuilder pb = new ProcessBuilder("/u01/app/chdir/user.sh");
ProcessBuilder pb = new ProcessBuilder("who");
Process p;
p = pb.start();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println("Your Host Details--->"+input.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}