Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Is there a way to suspend an external process started by Java?

843829Dec 28 2007 — edited Dec 28 2007
I want to write a method like this:
void suspendProcess(java.lang.Process p);
and use it like this:
ProcessBuilder pb = new ProcessBuilder(new String[]{"notepad"});
Process p = pb.start();
suspendProcess(p); // The process will be suspended here
I use reflection to retrieve ProcessImpl#handle and pass it to a native method, which invokes win32 system calls to suspend the process. The process does be suspended, but unfortunately, the JVM crashes. I don't know why. The source code (for Windows platform only) is listed below.

Any ideas? Or is there a way to suspend an external process? Thanks in advance.

SuspendProcess.java
import java.io.IOException;
import java.lang.reflect.Field;

public class SuspendProcess {
    static {
        System.loadLibrary("SuspendProcess");
    }

    public static void main(String[] args)
    throws IOException, InterruptedException, SecurityException,
    NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        ProcessBuilder pb = new ProcessBuilder(new String[]{"notepad"});
        Process p = pb.start();
        Class<?> c = p.getClass();
        Field f = c.getDeclaredField("handle");
        f.setAccessible(true);
        Thread.sleep(2000);
        suspendProcess((Long)f.get(p));
    }

    native static void suspendProcess(long handle);
}
Suspend.h is omitted.

Suspend.c
//This program uses an unpublished api NtSuspendProcess, which is in ntdll.dll.
#include "SuspendProcess.h"
#include <windows.h>

DWORD NtSuspendProcess(HANDLE hProcess);

#pragma argsused
JNIEXPORT void JNICALL Java_SuspendProcess_suspendProcess
    (JNIEnv *env, jclass clazz, jlong handle) {
    NtSuspendProcess((void *)handle);
}
Edited by: Guang on Dec 28, 2007 6:06 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 25 2008
Added on Dec 28 2007
7 comments
254 views