runtime.exec

Running a .exe file using Java

六月ゝ 毕业季﹏ 提交于 2019-11-26 21:45:53
问题 How to run an exe file using java code?The .exe file is already there. The plan is to write a Java code for running the same. Any tutorial or reference for the same? 回答1: Try the following code: try { Runtime rt = Runtime.getRuntime() ; Process p = rt.exec("Program.exe") ; InputStream in = p.getInputStream() ; OutputStream out = p.getOutputStream (); InputStream err = p.getErrorStream() ; //do whatever you want p.destroy() ; } catch(Exception exc) { /*handle exception*/ } 回答2: You need to

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

时间秒杀一切 提交于 2019-11-26 20:58:26
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 == null) { // Either dir does not exist or is not a directory System.out.print("No existe el directorio\n"); } else { for (int i=0; i<children.length; i++) { // Get filename of file or directory String filename = children[i]; //Recojo el momento exacto System.out.print("\n" +filename); Process p = Runtime.getRuntime().exec("exiftool -a -u -g1 -j videos/"+filename

How to use “cd” command using Java runtime?

蓝咒 提交于 2019-11-26 18:57:37
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 = Runtime.getRuntime().exec(command, null); But the above code gives the following error Exception in thread "main" java.io.IOException: Cannot run program "cd": java.io.IOException: error=2, No such file or directory Can anyone please tell me how to implement it? Joachim Sauer There is no executable called cd , because it can't be implemented in a separate process. The

Running Bash commands in Java

不羁岁月 提交于 2019-11-26 18:13:41
问题 I have the following class. It allows me to execute commands through java. public class ExecuteShellCommand { public String executeCommand(String command) { StringBuffer output = new StringBuffer(); Process p; try { p = Runtime.getRuntime().exec(command); p.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; while ((line = reader.readLine())!= null) { output.append(line + "\n"); } } catch (Exception e) { e.printStackTrace(); }

Running a java program from another java program

百般思念 提交于 2019-11-26 16:45:07
I am working on a simple java program. It simply compiles and executes another java program. I am using Runtime.exec() function to compile and run. There is no problem with compilation. but when it runs, if the second program needs an input to read from keyboard, I can't give it from the master process. I used getOutputStream() function. but it couldn't help. I will provide my code. public class sam { public static void main(String[] args) throws Exception { try { Process p = Runtime.getRuntime().exec("javac sam2.java"); Process p2 = Runtime.getRuntime().exec("java sam2"); BufferedReader in =

Runtime.getRuntime().exec(cmd) hanging

巧了我就是萌 提交于 2019-11-26 16:30:49
问题 I am executing a command which returns me the Revision number of a file; 'fileName'. But if there is some problem executing the command, then the application hangs up. What can I do to avoid that condition? Please find below my code. String cmd= "cmd /C si viewhistory --fields=revision --project="+fileName; Process p = Runtime.getRuntime().exec(cmd) ; BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while ((line = in.readLine()) != null) {

Having spaces in Runtime.getRuntime().exec with 2 executables

天涯浪子 提交于 2019-11-26 14:52:28
问题 I have a command that I need to run in java along these lines: C:\path\that has\spaces\plink -arg1 foo -arg2 bar "path/on/remote/machine/iperf -arg3 hello -arg4 world" This command works fine when the path has no spaces, but when I have the spaces I cannot seems to get it to work. I have tried the following things, running Java 1.7 String[] a = "C:\path\that has\spaces\plink", "-arg1 foo", "-arg2 bar", "path/on/remote/machine/iperf -arg3 hello -arg4 world" Runtime.getRuntime().exec(a); as

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

邮差的信 提交于 2019-11-26 12:49:11
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 prova { public static void main(String[] args) throws IOException { Runtime.getRuntime().exec("ls"); } } The result is: [root@newton sisma-acquirer]# javac prova.java && java -cp . prova Exception in thread "main" java.io.IOException: Cannot run program "ls": java.io.IOException: error=12, Cannot allocate memory at java.lang.ProcessBuilder.start

read the output from java exec

断了今生、忘了曾经 提交于 2019-11-26 12:28:38
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 InputStreamReader(pr.getInputStream())); String line; while ((line = in.readLine()) != null) { System.out.println(line); } pr.waitFor(); System.out.println("ok!"); in.close(); System.exit(0); } in that code i'am trying to get a java version command execute is ok, but i can't read the output it just return null. Why? adatapost Use getErrorStream() . BufferedReader in = new

Printing Runtime exec() OutputStream to console

拜拜、爱过 提交于 2019-11-26 12:26:53
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 java.io.OutputStream; import java.io.PrintStream; import java.io.Reader; public class RuntimeTests { public static void main(String[] args) { File path = new File("C:\\Dir\\Dir2"); String command = "cmd /c dir"; Reader rdr = null; PrintStream prtStrm = System.out; try { Runtime terminal = Runtime.getRuntime(); OutputStream rtm = terminal.exec(command, null, path)