runtime.exec

Runtime's exec() method is not redirecting the output

删除回忆录丶 提交于 2019-11-26 11:50:36
Process p = Runtime.getRuntime().exec("sh somescript.sh &> out.txt"); I am running this command using Java. The script is running but it's not redirecting its stream to the file. Moreover, the file out.txt is not getting created. This script runs fine if I run it on shell. Any ideas? You need to use ProcessBuilder to redirect. ProcessBuilder builder = new ProcessBuilder("sh", "somescript.sh"); builder.redirectOutput(new File("out.txt")); builder.redirectError(new File("out.txt")); Process p = builder.start(); // may throw IOException Peter Lawrey When you run a command, there is no shell

how to compile & run java program in another java program?

拟墨画扇 提交于 2019-11-26 11:46:24
I have a Main.java and Test.java classes that I want to compile and run Main.java in Test.java code. Here is my code Process pro1 = Runtime.getRuntime().exec("javac Main.java"); pro1.waitFor(); Process pro2 = Runtime.getRuntime().exec("java Main"); BufferedReader in = new BufferedReader(new InputStreamReader(pro2.getInputStream())); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } I just print "ok" in Main.java but this code doesn't print anything. What is the problem ? I have modified the code to include some checks: public class Laj { private static

Runtime.getRuntime().exec()

北战南征 提交于 2019-11-26 11:27:14
问题 I can not read a file only when database name contains like (new database (myid) etc. I give a following example code: dumpCommand = \"C:/Program Files/MySQL/MySQL Server 5.0/bin/mysqldump -h\"+hostName+user+databaseName; Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(dumpCommand); InputStream in = proc.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(in)); String line =null; while((line=br.readLine())!=null) { //able to read line only when database name

java Runtime.getRunTime().exec & wildcards?

落爺英雄遲暮 提交于 2019-11-26 11:15:08
问题 i\'m trying to remove junk files by using Process p = Runtime.getRuntime().exec(); it works fine as long as i do not use wildcards, i.e. this works: Process p = Runtime.getRuntime().exec(\"/bin/rm -f specificJunkFile.java\"); while the following throws back \"No such file or directory\": Process p = Runtime.getRuntime().exec(\"/bin/rm -f *.java\"); i should be able to do all the nice things as outlined here, right? 回答1: Might I suggest that you let Java do this for you? Use file.listFiles()

Java Runtime.getRuntime().exec() alternatives

和自甴很熟 提交于 2019-11-26 10:57:53
问题 I have a collection of webapps that are running under tomcat. Tomcat is configured to have as much as 2 GB of memory using the -Xmx argument. Many of the webapps need to perform a task that ends up making use of the following code: Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec(command); process.waitFor(); ... The issue we are having is related to the way that this \"child-process\" is getting created on Linux (Redhat 4.4 and Centos 5.4). It\'s my understanding that an

Reading streams from java Runtime.exec

独自空忆成欢 提交于 2019-11-26 09:12:58
问题 I have the following snippet of code: Process proc = runtime.exec(command); errorGobbler = new ErrorStreamGobbler(proc.getErrorStream(), logErrors, mdcMap); outputGobbler = new OutputStreamGobbler(proc.getInputStream(), mdcMap); executor.execute(errorGobbler); executor.execute(outputGobbler); processExitCode = proc.waitFor(); where the gobblers are Runnable s which use a BufferedReader to read the input and error streams of the executing process. While this works most of the time, I get the

How to use “cd” command using Java runtime?

ⅰ亾dé卋堺 提交于 2019-11-26 06:42:51
问题 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

How to Execute Windows Commands Using Java - Change Network Settings

喜欢而已 提交于 2019-11-26 05:22:17
问题 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 commands directly. Is this possible? Here is my implemented Solution for Future Reference: public class JavaRunCommand { private static final String CMD = \"netsh int ip set address name = \\\"Local Area Connection\\\" source = static addr = 192.168.222

Running a java program from another java program

。_饼干妹妹 提交于 2019-11-26 04:55:46
问题 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

how to compile & run java program in another java program?

白昼怎懂夜的黑 提交于 2019-11-26 03:35:11
问题 I have a Main.java and Test.java classes that I want to compile and run Main.java in Test.java code. Here is my code Process pro1 = Runtime.getRuntime().exec(\"javac Main.java\"); pro1.waitFor(); Process pro2 = Runtime.getRuntime().exec(\"java Main\"); BufferedReader in = new BufferedReader(new InputStreamReader(pro2.getInputStream())); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } I just print \"ok\" in Main.java but this code doesn\'t print