Java starts a process, kills a process, and displays the current process

Source: Internet
Author: User

Package com. ffshi. util;

Import java. Io. ioexception;
Import java. util. collections;

Public class javawindowscommandutil {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub

// Starttask ("E: // fetion // fetion.exe ");
Killtask ("javaw ");

}

/**
* Killing a process
*
* @ Param task
*/
Public static void killtask (string task ){

Try {
Process = runtime.getruntime(cmd.exe C ("tasklist ");
Processing in = New Processing (process. getinputstream ());
Int COUNT = 0;
While (in. hasnextline ()){
Count ++;
String temp = in. nextline ();

If (temp. Contains (task )){
String [] T = temp. Split ("");
// Determine whether the memory occupied by the process is greater than 20 mb
If (integer. parseint (T [T. Length-2]. Replace (",", "")> 20000 ){
Temp = temp. replaceall ("","");
// Obtain the PID
String pid = temp. substring (9, temp. indexof ("console "));
Runtime.getruntime(cmd.exe C ("tskill" + PID );

// Open the CMD window ntsd-C q-p pid in DOS
// Runtime.getruntime(cmd.exe C ("ntsd-C q-P 1528 ");
}
}
// System. Out. println (count + ":" + temp );
}
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}

/**
* Display all processes of the Current Machine
*/
Public static void showtasklist (){

Try {
Process = runtime.getruntime(cmd.exe C ("tasklist ");
Processing in = New Processing (process. getinputstream ());
Int COUNT = 0;
While (in. hasnextline ()){
Count ++;
System. Out. println (count + ":" + in. nextline ());
}
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}

/**
* Start a process
*
* @ Param task
*/
Public static void starttask (string task ){
Try {
Runtime.getruntime(cmd.exe C (task );

} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.