File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Languages and the fly likes Killing  process using C language Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "Killing  process using C language" Watch "Killing  process using C language" New topic
Author

Killing process using C language

karthik deepak
Greenhorn

Joined: Nov 19, 2008
Posts: 6
I need to write code to kill a process using C language.
I should write C code and use it in java using JNI
Is there an API in C that kills a process given its name or something like pid in unix?

<Karthik> Future SCJP
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42264
    
  64
You don't need to use JNI or C. You can use Runtime.exec or ProcessBuilder to launch whatever Unix utility you prefer to handle this (most likely ps and kill).


Ping & DNS - my free Android networking tools app
karthik deepak
Greenhorn

Joined: Nov 19, 2008
Posts: 6
I need to write code to kill a process on a Windows(Win 2000 server, Win 2003 server) and unix systems.

1) For unix systems i wrote a shell script which did the job ->

procID=`ps -e | grep $1 | grep -v grep | awk '{print $1}'`
kill -9 $procID

But i was asked to integrate this into java code and creat an APi, how do i do that.

2) I was given a specification saying that we cant kill a process on server cleanly using java, so write native code in C and intgrate into java using JNI.
But how to kill a process using C?
Is there a way to kill it cleanly in java?
will destroy() method kill a process cleanly?

HELP ME OUT PLEASE...

Im a fresher into the company, so facing an uphill task understanding things..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42264
    
  64
#1 - by using Runtime.exec or ProcessBuilder

For Windows, if you want to use JNI -for which there is no need- then you need to find a C/C++ API that can terminate processes. Some rudimentary code for that is mentioned in http://www.coderanch.com/t/385840/Java-General-intermediate/java/Relaunch-program-if-it-crashes
You can also use Windows' tasklist utility (for which the Runtime.exec/ProcessBuilder approach should also work).
Abhinav Trivedi
Greenhorn

Joined: Mar 29, 2009
Posts: 10
karthik deepak wrote:I need to write code to kill a process using C language.
I should write C code and use it in java using JNI
Is there an API in C that kills a process given its name or something like pid in unix?



Actually you can do it with taskkill command that default shipps with windows. You can check the syntax. And in C language and if you are using borland editor import dos.h and use int86 interrupt handler to kill a process. There is also the unixtodos function. Hope you will get the idea.


Abhinav Trivedi
Accenture PVT limited, Pune, IDC, AIM- TrivediAcc@aol.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Killing process using C language