佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 894|回复: 2

可以帮忙解决java的问题吗?

[复制链接]
发表于 4-10-2008 07:03 PM | 显示全部楼层 |阅读模式
我正在写一个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 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 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 | 显示全部楼层
谢谢你,得空请你吃饭!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 23-12-2025 11:35 PM , Processed in 0.123695 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表