|
查看: 893|回复: 2
|
可以帮忙解决java的问题吗?
[复制链接]
|
|
|
我正在写一个program可以打开一些applicantion,好像paint,caculator之类!已经写好了,但是有一些问题。
就是我call了第一个app出来之后,就不能再call了,会有error!然后我还要写一个function可以关掉我刚刚call出来的application(paint和caculator),要怎样call呢?下面是我的coding!
//RunCommand.java//
import java.io.*;
public class RunCommand {
public RunCommand(){}
public void RunApp(String app) {
String s = null;
try {
// run the Unix "ps -ef" command
Process p = Runtime.getRuntime().exec(app);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
}
}
}
//TestRunApp.java//
import java.io.*;
import java.util.*;
public class TestRunApp{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
RunCommand rc = new RunCommand();
System.out.println("1. Calculator");
System.out.println("2. Paint");
System.out.println("3. Notepad");
int x;
x=input.nextInt();
switch(x){
case 1:
rc.RunApp("calc");
case 2:
rc.RunApp("mspaint");
case 3:
rc.RunApp("notepad");
default: System.out.println("Invalid .");break;
}
}
}
[ 本帖最后由 ahjing 于 4-10-2008 09:12 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 5-10-2008 02:31 AM
|
显示全部楼层
原帖由 SotongJiang 于 4-10-2008 11:11 PM 发表 
TestRunApp.java方面,加do..while进去,把选项控制在1到3,其他value就exit. switch case那边你要加break在每一个case后面。
至于RunCommand.java,commented out两个while loop。
酱子过后,就能要多少calcul ...
太感谢你了!
请问要如何add多一个function去关掉这些app? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 6-10-2008 10:57 PM
|
显示全部楼层
谢谢你,得空请你吃饭!! |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|