如题,想通过界面按钮启动另外一个后台进程。目前的做法是Process process = Runtime.getRuntime().exec(".\\x.bat");x.bat是用来启动进程的批处理文件,我想问的是,是否可以直接调用java.exe 指明mainclass来启动子进程,我试来试去都没成功,大家帮忙。
手动在命令行运行一下 java test.Test 看看有无问题import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;public class Test { public static void main(String arg[]) { String s = null; try { Process process = Runtime.getRuntime().exec("java test.Test1"); // Process process = Runtime.getRuntime().exec(".\\test.bat"); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream())); while ((s = bufferedReader.readLine()) != null) System.out.println(s); process.waitFor(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("finish"); } }/* Hello World!!! finish */我运行过 没问题
Process process = Runtime.getRuntime().exec("java -cp ./bin test.Test");试试。
当用exec方法调用java.exe或其它可执行程序的时候,一定要使用下面的格式:Process process = Runtime.getRuntime().exec("cmd /C java -cp ./bin test.Test");如果没有标红色的部分,是不会执行正确的
能解释下么?感觉不加也应该没问题。在XP里打cmd /?得到的解释 [code=BatchFile]CMD命令 Starts a new instance of the Windows XP command interpreter /C Carries out the command specified by string and then terminates [/code]
public class TestRuntime {
public static void main(String arg[]){
String s = null;
try {
Process process = Runtime.getRuntime().exec("java test.Test");
//Process process = Runtime.getRuntime().exec(".\\test.bat");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while((s=bufferedReader.readLine()) != null)
System.out.println(s);
process.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("finish");
}
}
其中test.bat的内容为java -cp ./bin test.Test &
test.Test只是System.out.println("Hello World~~~"); 而已
现在的问题是,执行Process process = Runtime.getRuntime().exec(".\\test.bat");可以得到如下输入D:\program_design\zjcc_project\RuntimeExec>java -cp ./bin test.Test
Hello World~~~
finish而执行Process process = Runtime.getRuntime().exec("java test.Test");只是得到
finish
java test.Test
看看有无问题import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;public class Test {
public static void main(String arg[]) {
String s = null;
try {
Process process = Runtime.getRuntime().exec("java test.Test1");
// Process process = Runtime.getRuntime().exec(".\\test.bat");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
while ((s = bufferedReader.readLine()) != null)
System.out.println(s);
process.waitFor();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("finish");
}
}/*
Hello World!!!
finish
*/我运行过 没问题
[code=BatchFile]CMD命令 Starts a new instance of the Windows XP command interpreter
/C Carries out the command specified by string and then terminates
[/code]
使用了cmd /c 后才能够正确的使用系统的PATH环境变量,找到相应的执行程序。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public class TestRuntime {
public static void main(String arg[]){
String s = null;
try {
Process process = Runtime.getRuntime().exec("java test.Test");
//Process process = Runtime.getRuntime().exec(".\\test.bat");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while((s=bufferedReader.readLine()) != null)
System.out.println(s);
process.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("finish");
}
}
package test;public class Test {
public static void main(String args[]){
System.out.println("Hello World~~~");
}
}
//execl.exe是你的excel进程
Runtime.getRuntime().exec("taskkill /IM execl.exe"); //test.xls是你的文件
Runtime.getRuntime().exec("del D:/test.xls");
}catch(Excepiton e){}