Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5937925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:36:41+00:00 2026-05-22T15:36:41+00:00

I am looking for a Java tool/package/library that will allow me to force-kill a

  • 0

I am looking for a Java tool/package/library that will allow me to force-kill
a child process.

This tool/package/library must work on Windows platform (mandatory).
Support for Linux/Unix is desired.

My Problem

My Java code creates a child process that will simply not react to the
standard Java way for killing a child process: process.destroy(), and,
since I do not have the child’s source code, I cannot program it to
better handle termination requests.

I have tried closing the child process’ error input and output stream
before calling destroy(), and for no effect.

I have even tried passing ctrlBreak signal (char=3) directly into
child.getOutputStream(), and again have received the same results.

The workaround I have finally managed to find was to:

  1. Obtain the child’s PID upon its creation
    This can be done in Windows by diffing the process lists
    before and after the child’s creation (getRuntime().exec("tasklist /v"))

  2. Use the child’s PID to issue a force kill system command
    in Windows: getRuntime().exec("taskkill /pid " + childPid + " /f")

But – this is complex code I have no desire to debug and maintain, plus the problem
itself, I have no doubt, was previously encountered by many other java developers,
which leads me to the hope that such a Java tool/package/library already exists.

I just don’t know its name…

PS: My child process was created by Runtime.getRuntime().exec(cmd), but
I get the same behaviour using a ProcessBuilder.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:36:42+00:00Added an answer on May 22, 2026 at 3:36 pm

    There is a leaner way to do this using Java JNA.

    This works definitely for Windows and Linux, i assume that you can do the same for other platforms too.

    The biggest problem of Java process handling is the lack of a method to get the process id of the process started with untime.getRuntime().exec().

    Assuming you got the pid of a process, you always can start a kill -9 command in linux, or use similar ways to kill a process in windows.

    Here is a way to get the process id natively for linux (borrowed from the selenium framework, 🙂 ), and with the help of JNA this also can be done for windows (using native Windows API calls).

    For this to work (for Windows) you first have to get the JNA Library at JAVA NATIVE ACCESS (JNA): Downloads or get it from maven

    Look at the following code, which will get the pid of a (in this example windows) program (most of the code is actually debris to get a working java program going):

    import com.sun.jna.*;
    import java.lang.reflect.Field;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Main {
    
    static interface Kernel32 extends Library {
    
        public static Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
    
        public int GetProcessId(Long hProcess);
    }
    
    public static void main(String[] args) {
        try {
            Process p;
    
            if (Platform.isWindows())
                p = Runtime.getRuntime().exec("cmd /C ping msn.de");
            else if (Platform.isLinux())
                p = Runtime.getRuntime().exec("cmd /C ping msn.de");
    
            System.out.println("The PID: " + getPid(p));
    
            int x = p.waitFor();
            System.out.println("Exit with exitcode: " + x);
    
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static int getPid(Process p) {
        Field f;
    
        if (Platform.isWindows()) {
            try {
                f = p.getClass().getDeclaredField("handle");
                f.setAccessible(true);
                int pid = Kernel32.INSTANCE.GetProcessId((Long) f.get(p));
                return pid;
            } catch (Exception ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (Platform.isLinux()) {
            try {
                f = p.getClass().getDeclaredField("pid");
                f.setAccessible(true);
                int pid = (Integer) f.get(p);
                return pid;
            } catch (Exception ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else{}
        return 0;
    }
    }
    

    Hope this helps, ;)…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a tool that will reverse engineer Java into a sequence diagram
I'm looking for a Java library that would allow me to marshal XML to
Looking to develop server-side application that will process documents. The source documents are mostly
I'm looking for a good tool to profile a java webapp. I'd like to
I'm looking for Java code that can be used to generate sound at runtime
I was looking at the Java code for LinkedList and noticed that it made
I'm looking for a Java profiler that works well with the JVM coming with
I'm not looking for java-web-start, I'm looking for a thick-client application installation toolkit. I've
I am looking to use Java to get the MD5 checksum of a file.
I am looking for a Java Rules / Workflow engine. Something similar to Microsoft

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.