简体   繁体   中英

java killing process after a period of time

my code goes like this:

        Runtime rt = Runtime.getRuntime();
        // cmd == "cmd.exe /C java =Xms2M =Xmx16M Sth" 
        Process proc = rt.exec(cmd);

Now i want to kill this process after a second, and get his output and error into a string variables.

How to do that ?

In my case: I have a infinitive loop in compiled class Sth. I'm starting it and then, after one second i want to kill it.

//I'm testing it on Win XP then Debian.

thx Tzim

It will be a little tricky because once you terminate the process, its output is no longer available, so you have to grab the output before you kill it.

Thread.sleep(1000); 
InputStream in = proc.getInputStream();
byte[] data = new byte[in.available()];
in.read(data);
proc.destroy();

This is assuming that the process doesn't close itself in the meantime. If it does, the InputStream will not be available for use, and you'll wish you had been reading the data instead of twiddling your thumbs.

You'll definitely want to make this exception-safe — put the call to proc.destroy in a finally handler to ensure that the child process gets terminated.

Runtime rt = Runtime.getRuntime();
// cmd == "cmd.exe /C java =Xms2M =Xmx16M Sth" 
Process proc = rt.exec(cmd);

(You have a typo in your cmd, you wrote "=Xms2M" instead of "-Xms2M", same for "-Xmx16M").

Never call Runtime.exec like that: you must split your 'cmd' in an array or you'll encounter lots of issues. Do it like this:

String[] sar = { "cmd.exe", "/C", "java", "-Xms2M", "-Xmx16M", "Sth" };
Runtime.getRuntime().exec( sar );

This might also help you out:

private static String runCommand(boolean debug, String... script) throws IOException, InterruptedException {
    Runtime rt = Runtime.getRuntime();
    Process process = rt.exec(script);
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StreamReaderUtil.UTF8));
        Thread.sleep(1000);
        StringBuilder queryExe = new StringBuilder();
        try {
            String str;
            while(reader.ready() && (str = reader.readLine()) != null) {
                queryExe.append(str).append("\n");
            }
        } catch(IllegalThreadStateException e) {
            Thread.sleep(250);
        }
        // FIXME: Replace with a Logger
        if(debug) {
            System.out.println("Executing : " + script);
            System.out.println("Result    : " + queryExe);
        }
        return queryExe.toString();
    } finally {
        if(process != null) {
            process.destroy();
        }
        if(reader != null) {
            reader.close();
        }
    }
}

You can use ProcessBuilder which returns a Process object and use it's destroy() method.

Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM