runtime.exec

Mock Runtime.getRuntime()?

孤人 提交于 2019-11-29 11:46:19
问题 Can anyone make any suggestions about how best to use EasyMock to expect a call to Runtime.getRuntime().exec(xxx) ? I could move the call into a method in another class that implements an interface, but would rather not in an ideal world. interface RuntimeWrapper { ProcessWrapper execute(String command) throws IOException; } interface ProcessWrapper { int waitFor() throws InterruptedException; } I was wondering if anyone had any other suggestions? 回答1: Your class shouldn't call Runtime

Runtime class in java

旧时模样 提交于 2019-11-29 08:06:51
How to execute a java program with the help of Runtime.getRuntime().exec(). For example we shall have the java file path as c:/java/abc.java. Please help me with the code. Assuming that abc.java contains a main method that you want to execute: Runtime.getRuntime().exec("javac c:\java\abc.java -d c:\java\") Runtime.getRuntime().exec("java c:\java\abc") VonC Do not forget that: you may need to read stdout/stderr of a java program you may have to set/update environment variable and PATH before executing your java command CreateProcess: c:\j2sdk1.4.0\bin\helloworld error=2 means Win32's

how to redirect stdin to java Runtime.exec?

旧巷老猫 提交于 2019-11-29 07:29:36
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 StreamGobbler mechanism to get the error and output streams. No problem there. The problem comes in reading

ProcessBuilder vs Runtime.exec()

若如初见. 提交于 2019-11-29 05:55:49
Which one is better? By better I mean which one has better security, etc. (not ease of use). 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.com/questions/5886829/processbuilder-vs-runtime-exec

Taking thread dumps in production

泪湿孤枕 提交于 2019-11-28 17:17:09
I am analyzing the differences between approaches for taking thread dumps. Below are the couple of them I am researching on Defining a jmx bean which triggers jstack through Runtime.exec() on clicking a declared bean operation. Daemon thread executing "ManagementFactory.getThreadMXBean().dumpAllThreads(true, true)" repeatedly after a predefined interval. Comparing the thread dump outputs between the two, I see the below disadvantages with approach 2 Thread dumps logged with approach 2 cannot be parsed by open source thread dump analyzers like TDA The ouput does not include the native thread id

Running shell script from java code and pass arguments

偶尔善良 提交于 2019-11-28 14:31:07
I am executing a shell script from Java program. I have implemented it using Runtime class. Below is the code I implemented final StringBuilder sb = new StringBuilder("test.sh"); sb.append("/path to/my/text file"); final Process p = Runtime.getRuntime().exec(sb.toString()); Here sb is string buffer object where I append my parameters and use it in exec method. But the problem is the parameter I pass "/path to/my/text file" is considered as 4 parameters /path to /my/text file But if run in shell as test.sh "/path to/my/text file" which is taken as single parameter. How can I achieve the same

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

假如想象 提交于 2019-11-28 13:57:06
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 the $PATH variable. Doesn't Java automatically inherit all such variables, if null is passed as Environment? jim proc = Runtime.getRuntime().exec(cmd, null, fwrkDir); should be proc = Runtime.getRuntime().exec(cmd, "PATH=$PATH:/android-sdk-linux_x86/platform-tools", fwrkDir); Note that the second parameter to the exec() call in your example is null.

How do I get the bash command exit code from a Process run from within Java?

爱⌒轻易说出口 提交于 2019-11-28 13:42:21
I have a program which is: import java.io.*; import java.util.*; public class ExecBashCommand { public static void main(String args[]) throws IOException { if (args.length <= 0) { System.err.println("Need command to run"); System.exit(-1); } Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec("./nv0914 < nv0914.challenge"); Process process1 = runtime.exec("echo ${?}"); InputStream is = process1.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; //System.out.printf("Output of running %s is:", Arrays

set windows PATH environment variable at runtime in Java

会有一股神秘感。 提交于 2019-11-28 12:28:30
I have a java program that fires off an executable using the Runtime.exec() method. I'm using the variant that takes in a set of command line params as one argument, and some environment variables as another argument. The environment variable I'm tryign to set is path, so i'm passing in "PATH=C:\some\path". This does not work. Is there some trick to this or any alternatives. I am stuck to Java 1.4 unfortunately. use http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getenv%28java.lang.String%29 to get the environment and fix it up then use a flavour of [ http://java.sun.com/j2se/1.4

java Runtime.exec to run shell script

我的梦境 提交于 2019-11-28 11:18:20
问题 I am using Runtime.getRuntime().exec() to run a shell script from java code. The code works fine when I pass the parameter as string Runtime.getRuntime().exec("sh test.sh") Since I have to pass additional arguments which are paths with spaces, so I replaced String with String array. String[] cmd = {"sh test.sh", "/Path/to my/resource file"}; Runtime.getRuntime().exec(cmd) I also tried with String[] cmd = {"sh test.sh"}; Runtime.getRuntime().exec(cmd) But neither of them worked. Its throwing