runtime.exec

ProcessBuilder debugging

梦想的初衷 提交于 2020-01-03 18:14:30
问题 I created an executable jar and executed it using process builder from another java program. Here's my code - public class SomeClass { public static void main(String[] args) { Process p = null; ProcessBuilder pb = new ProcessBuilder("java", "-jar", "src.jar"); pb.directory(new File("/Users/vivek/servers/azkaban-0.10/TestApp/src")); try { p = pb.start(); } catch(Exception ex) { ex.printStackTrace(); } } } I'm trying to now debug the src.jar from eclipse. I provided the project src as external

Get output from BAT file using Java

吃可爱长大的小学妹 提交于 2020-01-03 11:27:58
问题 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 BufferedReader( new InputStreamReader( pr.getInputStream() )); String s ; while ((s = stdInput.readLine()) != null) { System.out.println(s); } The result is null . No idea why I get this. Note that I'm using Windows 7. 回答1: Using "cmd /c start [...]" to

Get output from BAT file using Java

半腔热情 提交于 2020-01-03 11:27:05
问题 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 BufferedReader( new InputStreamReader( pr.getInputStream() )); String s ; while ((s = stdInput.readLine()) != null) { System.out.println(s); } The result is null . No idea why I get this. Note that I'm using Windows 7. 回答1: Using "cmd /c start [...]" to

Java - Run Excel using runtime.getRuntime().exec

大兔子大兔子 提交于 2020-01-01 15:03:35
问题 try { Runtime.getRuntime().exec("excel C:\\file.xls"); } catch (IOException ex) { System.out.println(ex); } Doesn't work. I have to put the full path of excel.exe in order to work. How can I make it generic (For any Excel Folders/Versions)? When I run the same line from OS with Windows Run (Start --> Run) it works. Is there a code in Java to simulate Windows' Run command? 回答1: Why don't you try with the Desktop class (api doc here) introduced in JDK6 that has the method public void open(File

Can Java Runtime.exec another java program that uses stdin?

走远了吗. 提交于 2019-12-31 05:01:42
问题 I have run into an issue where , when using Java Runtime to run another java program, the program freezes because the other program requires stdin . There is a problem with handling the stdin after executing another java program with Runtime exec() . Here is sample code that I can't get to work. Is this even possible? import java.util.*; import java.io.*; public class ExecNoGobble { public static void main(String args[]) { if (args.length < 1) { System.out.println("USAGE: java ExecNoGobble

Run a sub process, provide input and output to it correctly in Java

纵然是瞬间 提交于 2019-12-28 06:22:11
问题 I use Runtime exec() method to create a subprocess in Java. However, since the subprocess is an interactive program, I need to provide input to it as and when required by it. Also I need to show the output of the subprocess. How can I do this in the simplest possible way? I was using a StreamGobbler to show the program output using process.getInputStream(). I, however, do not know how to identify when the program is waiting for input and when to provide it input using proc.getOutputStream.

Java Runtime exec() fails to escape characters properly

荒凉一梦 提交于 2019-12-28 04:07:10
问题 This might already been answered before but that was regarding unicode and I don't think this is unicode (it's in ASCII so...). When I execute this in my terminal there is no problem what so ever vboxmanage setextradata "Test Machine" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" 2222 However when I use the following in Java Runtime.getRuntime().exec("vboxmanage setextradata \"Test Machine\" \"VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort\" 2222"); It returns an error:

Using Quotes within getRuntime().exec

送分小仙女□ 提交于 2019-12-28 02:06:11
问题 I'd like to invoke bash using a string as input. Something like: sh -l -c "./foo" I'd like to do this from Java. Unfortunately, when I try to invoke the command using getRuntime().exec , I get the following error: foo": -c: line 0: unexpected EOF while looking for matching `"' foo": -c: line 1: syntax error: unexpected end of file It seems to be related to my string not being terminated with an EOF. Is there a way to insert a platform specific EOF into a Java string? Or should I be looking

Redirect Runtime.getRuntime().exec() output with System.setOut();

我们两清 提交于 2019-12-28 01:04:18
问题 I have a program Test.java: import java.io.*; public class Test { public static void main(String[] args) throws Exception { System.setOut(new PrintStream(new FileOutputStream("test.txt"))); System.out.println("HelloWorld1"); Runtime.getRuntime().exec("echo HelloWorld2"); } } This is supposed to print HelloWorld1 and HelloWorld2 to the file text.txt. However, when I view the file, I only see HelloWorld1. Where did HelloWorld2 go? Did it vanish into thin air? Lets say I want to redirect

java.lang.Runtime exception “Cannot run program”

回眸只為那壹抹淺笑 提交于 2019-12-25 16:07:05
问题 I am getting an exception like java.io.IOException: Cannot run program cat /home/talha/* | grep -c TEXT_TO_SEARCH": error=2, No such file or directory while executing the command below despite that there are no issues when I execute the same command through the terminal. I need to execute and return the output of the command below: cat /home/talha/* | grep -c TEXT_TO_SEARCH Here is the method used to execute commands using Runtime class: public static String executeCommand(String command) {