开发者

Try to make a process that can TSKILL itself

开发者 https://www.devze.com 2023-01-16 00:38 出处:网络
So I have a problem w开发者_如何学运维ith a process that I am running, whenever I try to stop it using process.destroy(), it does not stop.

So I have a problem w开发者_如何学运维ith a process that I am running, whenever I try to stop it using process.destroy(), it does not stop.

I want to create a file (ProcessHandler) that extends Process and do the following:

ProcessHandler process = (ProcessHandler)Runtime.getRuntime().exec("cmd.exe /c \"java net/com/codeusa/Server 43594\"");

So, my problem is trying to convert Process to ProcessHandler where I can override the destroy() command, to make it TSKILL itself. I have figured out how to do everything but when I try the above like of code, I get a ClassCastException..

Anyone have an idea how I can make these be compatible. BTW the exec(String) command returns an instance of Process.


Firstly, if you are using Java 5 or later, I would recommend using ProcessBuilder instead of Runtime.getRuntime().exec(). For one thing, you don't have to worry about quoting arguments. Each separate command-line argument is a separate parameter. For example:

ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/C", "java net/com/codeusa/Server 43594");
Process process = builder.start();

When starting a process using ProcessBuilder or Runtime.getRuntime().exec(), it's entirely up to the JVM to instantiate and return a subclass of Process of its choice, and there's no way to influence its decision. I assume your ProcessHandler class is one you've written yourself (I can't find a Java API class with that name). It might subclass Process, but even if it does there's no way for the JVM to return an instance of it when you use ProcessBuilder or Runtime.getRuntime().exec(). So your line of code above is guaranteed to throw a ClassCastException, assuming it doesn't throw some other exception.

I have had some experience in the past of processes that didn't respond to destroy() methods. Usually this was because the standard output or standard error being written by the process wasn't being read, and the process had ground to a halt because one or more of its I/O buffers had filled up. Does the process above write anything to its standard output or standard error, and if so, are you reading it?

Reading both the standard output and standard error streams is easier with ProcessBuilder: if you add the line builder.redirectErrorStream(true); between the two lines above, then you only need to read from the process's standard output. If you're stuck with Java 1.4 or earlier and Runtime.getRuntime().exec(), you'll have to set up two different objects in two different threads, one that reads from each stream.

I'm not sure what you are trying to achieve with your ProcessHandler class - you haven't provided the source code for it. Besides, I've never had the need to kill a process more forcibly than by using the destroy() method.


I figured out a whole new thing!! When I call the destroy() method for process, it destroys the cmd.exe process.. but I replaced cmd.exe with "java" and now when I call destroy(), the java.exe process terminates.. HURAY

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号