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
5
votes
3 answers

Java command Runtime.getRuntime().exec() in Mac OS

I´m using Mac OS Lion, with java version 1.6.0_26 I'm making a small app for Mac in Java with a main menu for the user, so he can choose several options. One of them is install an app using a .pkg Everything was working fine with these…
user897013
  • 165
  • 1
  • 3
  • 9
5
votes
7 answers

Calling GnuPG in Java via a Runtime Process to encrypt and decrypt files - Decrypt always hangs

NOTE: Coming back to this later as I've been unable to find a working solution. Draining the input streams manually instead of using BufferedReaders doesn't seem to help as the inputStream.read() method permanently blocks the program. I placed the…
framauro13
  • 797
  • 2
  • 8
  • 18
5
votes
6 answers

Using Java's exec command when you don't know if there's be spaces

I'm fighting the spaces bug in Java's Runtime exec method. Here's what's unique about this problem: the command I'm trying to execute is an incoming string and may or may not have spaces and is not necessarily in any specific format. Either way, I…
Dave
  • 4,050
  • 6
  • 30
  • 35
5
votes
3 answers

Get output of cmd command from java code

I have a program where I was able to successfully execute cmd commands from my code, but I want to be able to get the output from the cmd command. How can I do that? So far my code is: Second.java: public class Second { public static void…
hakuna matata
  • 3,243
  • 13
  • 56
  • 93
5
votes
2 answers

Efficient execution and output stream redirection of process spawned with Runtime.exec()

I have a script which executes a program several times, producing about 350 lines of output to both STDERR and STDOUT. Now, I need to execute the script in Java, thereby printing the output streams to their original destinations. So, basically, I…
dsteinhoefel
  • 688
  • 4
  • 13
5
votes
2 answers

Java code to run .exe shortcuts

Is there any way that I can open notepad or other application from shortcuts? Here is my code: import java.io.File; import java.io.IOException; public class acrobat { public static void main(String[] args) throws IOException,…
Jayraj Patel
  • 79
  • 1
  • 10
5
votes
5 answers

Passing a string to the Windows command line

Please see the code below Runtime rt = Runtime.getRuntime(); rt.exec("cmd /c start"); String[] cmd = {"LogParser", "Select top 10 * into c:\temp\test9.csv from application" }; rt.exec(cmd); It opens the command window but the strings are not…
5
votes
2 answers

what is runtime.getruntime().exec("domain specific equivalent of cls") for windows 7

I want to clear screen in my java application, after reading many questions and googling, I found the below code runtime.getruntime().exec("cls") or Runtime.getRuntime().exec("cmd /c cls"); but the above code doesn't work in windows 7. I am…
Radan
  • 1,630
  • 5
  • 25
  • 38
4
votes
2 answers

Java Runtime.getRuntime().exec() fails after calling it several hundred times

I have a Java program that executes Runtime.getRuntime().exec("ls -l"); many times, once for each directory in the system. My test system has more than 1,000 directories and Runtime.getRuntime().exec("ls -l"); seems to error out after 480…
ytw
  • 1,335
  • 2
  • 20
  • 42
4
votes
1 answer

Call python script within java code (runtime.exec)

I'm trying to run a python script in java but I'm having some troubles. I'm using the command bellow to execute the python script which is inside a folder called python in my java project: Runtime r = Runtime.getRuntime(); Process p = r.exec("cmd /c…
Paulo
  • 267
  • 4
  • 14
4
votes
3 answers

$PATH variable isn't inherited through getRuntime().exec

I'm trying to start a script by the following command in Java: proc = Runtime.getRuntime().exec(cmd, null, fwrkDir); The command, typed in a console, works flawlessly. But here it doesn't seem to find the script, even though it's path is added to…
panmari
  • 3,627
  • 3
  • 28
  • 48
4
votes
1 answer

Change process groups on Runtime.getRuntime().exec processes

I need to be able to start and stop an external program from inside java. I have the starting working just fine but when I stop it, it kills its parent. It turns out that the process I'm starting is killing its entire process group with a kill 0. …
tharris
  • 113
  • 1
  • 6
4
votes
2 answers

Runtime.exec() can't run "su - postgres -c 'pg_dump ...'"

This is the command I want to run: su - postgres -c "pg_dump ....." to backup the postgres database. If I am now in linux shell, as a root, it works great. But now, I want to run it from a java application, as: String cmd = "su - postgres -c…
Freewind
  • 193,756
  • 157
  • 432
  • 708
4
votes
4 answers

Reboot programmatically Android Things

I want to use this code in order to reboot my RPI3 running Android Things: public static void Reboot() { try { Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "reboot"}); proc.waitFor(); } catch (Exception…
Daniele
  • 668
  • 2
  • 10
  • 25
4
votes
6 answers

Runtime.getRuntime().exec(), executing Java class

I am executing Java class from inside my application. proc = Runtime.getRuntime().exec("java Test"); How can I recognize whether Test executed successfully or not (i.e. no exceptions)? Redirecting output / error: proc =…
Little Jeans
  • 286
  • 2
  • 4
  • 11