runtime.exec

Java Runtime.exec() arguments on Linux

☆樱花仙子☆ 提交于 2019-11-28 09:56:03
问题 Okay so here is the problem: I have 3 classes MyClass1 and MyClass2 and ExecClass. I go to my command prompt and do this: $java MyClass1 -exec "java MyClass2 arg1 arg2" which works perfectly. Now in ExecClass I have the following line: Runtime.getRuntime().exec("java MyClass1 -exec \"java MyClass2 arg1 arg2\""); Problem is if you print the second string its exactly the same as the first, but when my ExecClass runs it MyClass1 complains: Unrecognized argument arg1 and fails. After a bit of

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

末鹿安然 提交于 2019-11-28 09:33:17
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 execute the script from inside a Java class, maintaining the original behavior for the user. The way I do this is inspired from suggestions like Reading streams from java Runtime.exec and, functionally, works fine. Process p = Runtime.getRuntime().exec(cmdarray); new Thread(new ProcessInputStreamHandler(p.getInputStream(), System.out)).start(); new

How to open the notepad file in java?

為{幸葍}努か 提交于 2019-11-28 06:38:30
I want to open Notepad in my Java program. Suppose that I have one button if I click this button the notepad will appear. I already have a file name and a directory. How can I implement this case? Try if (Desktop.isDesktopSupported()) { Desktop.getDesktop().edit(file); } else { // dunno, up to you to handle this } Make sure the file exists. Thanks to Andreas_D who pointed this out. (assuming you want notepad to open "myfile.txt" :) ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "myfile.txt"); pb.start(); Stephen Assuming you wish to launch the windows program notepad.exe , you are

Runtime.exec().waitFor() doesn't wait until process is done

心不动则不痛 提交于 2019-11-28 05:48:39
I have this code: File file = new File(path + "\\RunFromCode.bat"); file.createNewFile(); PrintWriter writer = new PrintWriter(file, "UTF-8"); for (int i = 0; i <= MAX; i++) { writer.println("@cd " + i); writer.println(NATIVE SYSTEM COMMANDS); // more things } writer.close(); Process p = Runtime.getRuntime().exec("cmd /c start " + path + "\\RunFromCode.bat"); p.waitFor(); file.delete(); What happens is that the file deleted before it actually executed. Is this because the .bat file contains only native system call? How can I make the deletion after the execution of the .bat file? (I don't know

Unable using Runtime.exec() to execute shell command “echo” in Android Java code

我的梦境 提交于 2019-11-28 03:00:53
问题 I can use Runtime.exec() to execute shell commands like " getprop " and " ls system " and they work fine. However, when I use " echo $BOOTCLASSPATH ", " echo \\$BOOTCLASSPATH " or " echo HelloWorld ", it won't show it in stdout. The logcat shows: I/AndroidRuntime( 4453): VM exiting with result code -1. Here's my code: try { java.lang.Process proc = Runtime.getRuntime().exec("echo -e \\$BOOTCLASSPATH"); String line = null; InputStream stderr = proc.getErrorStream(); InputStreamReader esr = new

java Runtime.exec to run shell script - cannot open file

北战南征 提交于 2019-11-28 02:03:50
问题 I am using Runtime.getRuntime().exec() to run a shell script from java code. String[] cmd = {"sh", "build.sh", "/Path/to my/sh file"}; try{ Process proc = Runtime.getRuntime().exec( cmd ); } catch(Exception e){ System.out.println("Exception is:"+e); } It gives me the following output in console: sh: Can't open build.sh Am I following some wrong approach here? Cannot make out why his happens. EDIT Based on the comment here, I have modified the String[] cmd = {"sh", "build.sh", "/Path/to my/sh

Why does exec() start a ADB daemon? [closed]

╄→гoц情女王★ 提交于 2019-11-28 01:47:00
I am building an app for some set of rooted phones I have. I was wondering if there is any way that I could uninstall a system app which comes with the phone running some code from my app. I have tried running commands like adb shell pm clear COM.PACKAGE.NAME from the phone itself by Runtime.getRuntime().exec() , but the output of the command is as follows: cannot bind 'tcp:5038 * Daemon not running. Starting it now on port 5038* Why? ADB server starts on your host machine ( Unix , Windows ) and, by default, binds to port 5037 . A client (also your host machine) uses the port to send commands

how to redirect stdin to java Runtime.exec?

喜夏-厌秋 提交于 2019-11-28 01:33:59
问题 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 <mysqInstallDir\/bin\mysql.exe -u <userName> -p <password> < scripts\create_tables.sql I can invoke mysql.exe using Runtime.exec but how do I redirect data from sql file to mysql.exe ? I read the article in http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4 and used the

Running a .exe file using Java

爷,独闯天下 提交于 2019-11-28 00:37:09
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? Raoul George 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*/ } You need to execute exec() method of Runtime that returns Process instance or use ProcessBuilder class methods.

ProcessBuilder vs Runtime.exec()

寵の児 提交于 2019-11-27 23:28:11
问题 Which one is better? By better I mean which one has better security, etc. (not ease of use). 回答1: Ease of use is the only real difference between those two. Note that ease of use can lead to security by helping to avoid mis-use. At least on OpenJDK 6 Runtime.exec() is implemented using ProcessBuilder : public Process exec(String[] cmdarray, String[] envp, File dir) throws IOException { return new ProcessBuilder(cmdarray) .environment(envp) .directory(dir) .start(); } 来源: https://stackoverflow