Questions tagged [runtime.exec]

The Runtime.exec() method allows Java apps. to create a new OS Process.

The Runtime.exec(...) method (which has 6 overloaded variants) allows Java apps. to create a new system (represented in Java as a Process).

There are common mistakes made when creating new processes - as detailed in When Runtime.exec() won't. The article is the first thing to check if a process fails. Implement all the tips, and even if doing so does not make the process work, it will provide much more detailed information on why it failed.

The ProcessBuilder class introduced in Java 1.5 makes it easier to create and use a process correctly. For example, the redirectErrorStream(boolean) convenience method will merge the System.out & System.err streams, which allows both streams to be consumed in one Thread.

881 questions
6
votes
5 answers

Runtime.getRunTime().exec not behaving like C language "system()" command

In "C", I can run a long blocking process in the background (AND HAVE IT CONTINUE TO RUN) after the starting process has exited. void main(void) { system("some_long_blocking_process &"); exit(); } // "some_long_blocking_process" is…
casio car
  • 129
  • 1
  • 8
6
votes
4 answers

In java determine if a process created using Runtime environment has finished execution?

Runtime.getRuntime.exex("abc.exe -parameters"); using .waitFor() does not help to determine the completion of process.
devashish jasani
  • 413
  • 1
  • 4
  • 15
6
votes
1 answer

How do I get the bash command exit code from a Process run from within Java?

I have a program which is: import java.io.*; import java.util.*; public class ExecBashCommand { public static void main(String args[]) throws IOException { if (args.length <= 0) { System.err.println("Need command to…
Maverick
  • 2,738
  • 24
  • 91
  • 157
6
votes
3 answers

Android Runtime.getRuntime().exec() to nav through directories

So I want to be able to write an app that can turn on and display logcat messages, dmesg, and also be able to run commands like 'ls' 'cat' 'echo' 'cd.' If I do the following: nativeProc = Runtime.getRuntime().exec("ls\n"); BufferedWriter out…
Andi Jay
  • 5,882
  • 13
  • 51
  • 61
6
votes
2 answers

android 7: NullPointerException on APK installing with Runtime.getRuntime().exec

I'm trying to install APK in android 7 (samsung and sony) using regular Runtime.getRuntime.exec() routine. The installation fails with the following exception in the logcat: 09-04 14:14:33.932 16623-16623/? D/AndroidRuntime: Calling main entry…
6
votes
2 answers

Execute command with prompt YES input

public static String executeCommand(String command) { StringBuffer sb = new StringBuffer(); Process p; try { p = Runtime.getRuntime().exec(command); p.waitFor(); } BufferedReader reader = new BufferedReader(new…
Kishore
  • 5,761
  • 5
  • 28
  • 53
6
votes
2 answers

Java Runtime.getRuntime().exec() with quotes

I am trying to run ffmpeg via the exec call on linux. However I have to use quotes in the command (ffmpeg requires it). I've been looking through the java doc for processbuilder and exec and questions on stackoverflow but I can't seem to find a…
Slava Markeyev
  • 317
  • 1
  • 4
  • 13
6
votes
2 answers

Redirection with Runtime.getRuntime().exec() doesn't work

I need to execute a command from a program. The command line is ok, I tried it in the terminal, but it doesn't work in the program. I add a copy from my code: File dir = new File("videos"); String[] children = dir.list(); if (children ==…
Blanca Hdez
  • 3,513
  • 19
  • 71
  • 93
6
votes
4 answers

set windows PATH environment variable at runtime in Java

I have a java program that fires off an executable using the Runtime.exec() method. I'm using the variant that takes in a set of command line params as one argument, and some environment variables as another argument. The environment variable I'm…
hhhh
  • 61
  • 1
  • 1
  • 2
6
votes
2 answers

how to redirect stdin to java Runtime.exec?

I want to execute some sql scripts using Java's Runtime.exec method. I intend to invoke mysql.exe / mysql.sh and redirect the script file to this process. From the command prompt I can run the command
user307227
  • 71
  • 1
  • 1
  • 4
6
votes
3 answers

Get output from BAT file using Java

I'm trying to run a .bat file and get the output. I can run it but I can't get the results in Java: String cmd = "cmd /c start C:\\workspace\\temp.bat"; Runtime r = Runtime.getRuntime(); Process pr = r.exec(cmd); BufferedReader stdInput = new…
Muath
  • 4,351
  • 12
  • 42
  • 69
6
votes
1 answer

Perl script runs in terminal but doesn't run when invoked from Java program

I was running a Perl script that replaces a string with another: perl -pi.back -e 's/str1/str2/g;' path/to/file1.txt When I run this command from terminal it well replaces all the occurrences of str1 in the given file to str2. When I run this…
a90
  • 63
  • 5
5
votes
7 answers

Runtime.getRuntime().exec(), hide the console screen

I am executing a batch file using Java code. The code is given below: Process proc = null; proc = Runtime.getRuntime().exec("cmd /c start somebat.bat"); With this, the normal command prompt screen gets open. Now I want to suppress/hide the command…
Anish
5
votes
4 answers

External command does not execute completely - Java

So, I am building a program that converts .flv files to other formats. For that I'm using ffmpeg which does its job perfectly when executing it via command line. For example: ffmpeg -i C:\test.flv -acodec libmp3lame -y C:\test.mp3 This example…
5
votes
1 answer

Using quotes and double quotes in Java Runtime.getRuntime().exec(...)

I am trying to start a Lisp Image from Java in Mac OSX. Using the Image from my console I type the following: lisp_image --eval '(package::method "some_argument")' everything runs fine. In Java I have the problem to pass the quotes and double…
svendeswan
  • 75
  • 1
  • 9