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
140
votes
8 answers

Running Command Line in Java

Is there a way to run this command line within a Java application? java -jar map.jar time.rel test.txt debug I can run it with command but I couldn't do it within Java.
Ataman
  • 2,530
  • 3
  • 22
  • 34
121
votes
4 answers

How to make pipes work with Runtime.exec()?

Consider the following code: String commandf = "ls /etc | grep release"; try { // Execute the command and wait for it to complete Process child = Runtime.getRuntime().exec(commandf); child.waitFor(); // Print the first 16 bytes of…
poundifdef
  • 18,726
  • 23
  • 95
  • 134
119
votes
12 answers

How do I run a batch file from my Java Application?

In my Java application, I want to run a batch file that calls "scons -Q implicit-deps-changed build\file_load_type export\file_load_type" It seems that I can't even get my batch file to execute. I'm out of ideas. This is what I have in…
Amara
  • 13,839
  • 9
  • 32
  • 28
116
votes
12 answers

process.waitFor() never returns

Process process = Runtime.getRuntime().exec("tasklist"); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); process.waitFor();
user590444
  • 4,252
  • 8
  • 36
  • 43
110
votes
4 answers

Difference between ProcessBuilder and Runtime.exec()

I'm trying to execute an external command from java code, but there's a difference I've noticed between Runtime.getRuntime().exec(...) and new ProcessBuilder(...).start(). When using Runtime: Process p = Runtime.getRuntime().exec(installation_path +…
gal
  • 1,111
  • 2
  • 8
  • 5
68
votes
10 answers

How to solve "java.io.IOException: error=12, Cannot allocate memory" calling Runtime#exec()?

On my system I can't run a simple Java application that start a process. I don't know how to solve. Could you give me some hints how to solve? The program is: [root@newton sisma-acquirer]# cat prova.java import java.io.IOException; public class…
Andrea Francia
  • 9,737
  • 16
  • 56
  • 70
61
votes
8 answers

How to use "cd" command using Java runtime?

I've created a standalone java application in which I'm trying to change the directory using the "cd" command in Ubuntu 10.04 terminal. I've used the following code. String[] command = new String[]{"cd",path}; Process child =…
Antrromet
  • 15,294
  • 10
  • 60
  • 75
56
votes
10 answers

How to run Linux commands in Java?

I want to create diff of two files. I tried searching for code in Java that does it, but didnt find any simple code/ utility code for this. Hence, I thought if I can somehow run linux diff/sdiff command from my java code and make it return a file…
chitresh
  • 867
  • 2
  • 10
  • 17
56
votes
8 answers

Use Java FFmpeg wrapper, or simply use Java runtime to execute FFmpeg?

I'm pretty new to Java, need to write a program that listen to video conversion instructions and convert the video once an new instruction arrives. (Instructions are stored in Amazon SQS, but it's irrelevant to my question) I'm facing a choice,…
Beier
  • 3,127
  • 10
  • 28
  • 24
49
votes
7 answers

Printing Runtime exec() OutputStream to console

I am trying to get the OutputStream of the Process initiated by exec() to the console. How can this be done? Here is some incomplete code: import java.io.BufferedReader; import java.io.File; import java.io.IOException; import…
TheWolf
  • 1,675
  • 4
  • 22
  • 34
48
votes
5 answers

How to Execute Windows Commands Using Java - Change Network Settings

In Java, I want to be able to execute a Windows command. The command in question is netsh. This will enable me to set/reset my IP address. Note that I do not want to execute a batch file. Instead of using a batch file, I want to execute such…
mre
  • 43,520
  • 33
  • 120
  • 170
44
votes
1 answer

Why does Runtime.exec(String) work for some but not all commands?

When I try to run Runtime.exec(String), certain commands work, while other commands are executed but fail or do different things than in my terminal. Here is a self-contained test case that demonstrates the effect: public class ExecTest { static…
that other guy
  • 116,971
  • 11
  • 170
  • 194
42
votes
5 answers

read the output from java exec

Hello i have some question about java. here is my code: public static void main(String[] args) throws Exception { Process pr = Runtime.getRuntime().exec("java -version"); BufferedReader in = new BufferedReader(new…
maxormo
  • 599
  • 1
  • 4
  • 10
41
votes
5 answers

Execute external program

I tried to make an application that calls an external program that I have to pass two parameters. It doesn't give any errors. The program.exe, written in C++, takes a picture and modifies the content of a .txt file. The Java program runs but it does…
sqtd
  • 485
  • 1
  • 4
  • 6
41
votes
2 answers

ProcessBuilder gives a "No such file or directory" while Runtime().exec() works fine

I have an application, running on the Playframework, which needs to encode some video files. I used Process pr = Runtime.getRuntime().exec(execCode) for this (and it works perfectly), but as I need both, the output stream and the error stream, I…
Luuk D. Jansen
  • 4,402
  • 8
  • 47
  • 90
1
2 3
58 59