主题:[讨论]可以帮忙解决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;
}
}
}
就是我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;
}
}
}