Example usage for java.lang Process getClass

List of usage examples for java.lang Process getClass

Introduction

In this page you can find the example usage for java.lang Process getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.dawnsci.commandserver.core.process.ProgressableProcess.java

protected static int getPid(Process p) throws Exception {

    if (Platform.isWindows()) {
        Field f = p.getClass().getDeclaredField("handle");
        f.setAccessible(true);/*from  w  ww  . j a va 2s . co  m*/
        int pid = Kernel32.INSTANCE.GetProcessId((Long) f.get(p));
        return pid;

    } else if (Platform.isLinux()) {
        Field f = p.getClass().getDeclaredField("pid");
        f.setAccessible(true);
        int pid = (Integer) f.get(p);
        return pid;

    } else {
        throw new Exception("Cannot currently process pid for " + System.getProperty("os.name"));
    }
}

From source file:org.neo4j.server.enterprise.ArbiterBootstrapperIT.java

private static void kill(Process process)
        throws NoSuchFieldException, IllegalAccessException, IOException, InterruptedException {
    if (SystemUtils.IS_OS_WINDOWS) {
        process.destroy();/*from www  .  j  a  va  2 s  .  c  o m*/
    } else {
        int pid = ((Number) accessible(process.getClass().getDeclaredField("pid")).get(process)).intValue();
        new ProcessBuilder("kill", "-9", "" + pid).start().waitFor();
    }
}

From source file:com.streamsets.pipeline.stage.executor.shell.ShellExecutor.java

/**
 * Attempts to retrieve PID from internal JVM classes. This method is not guaranteed to work as JVM is free
 * to change their implementation at will. Hence the return value should be only used for troubleshooting or
 * debug and not for main functionality.
 *///  w  w  w.  ja  va2  s  .com
private static int retrievePidIfFeasible(Process process) {
    if (unixProcessClass == null) {
        return UNDETERMINED_PID;
    }

    if (!unixProcessClass.isInstance(process)) {
        LOG.debug("Do not support retrieving PID from {}", process.getClass().getName());
        return UNDETERMINED_PID;
    }

    try {
        return (int) pidField.get(process);
    } catch (IllegalAccessException e) {
        LOG.debug("Can't retrieve PID value from the field", e);
        return UNDETERMINED_PID;
    }

}

From source file:eu.vital.vitalcep.restApp.cepRESTApi.CEPICO.java

public static int getPid(Process process) {
    try {/*from   w  w  w  . j  av a2s. com*/
        Class<?> cProcessImpl = process.getClass();
        java.lang.reflect.Field fPid = cProcessImpl.getDeclaredField("pid");
        if (!fPid.isAccessible()) {
            fPid.setAccessible(true);
        }
        return fPid.getInt(process);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        return -1;
    }
}

From source file:eu.vital.vitalcep.restApp.alert.Alerts.java

public static int getPid(Process process) {
    try {//from   w  w w .ja v  a2s  . c o  m
        Class<?> cProcessImpl = process.getClass();
        java.lang.reflect.Field fPid = cProcessImpl.getDeclaredField("pid");
        if (!fPid.isAccessible()) {
            fPid.setAccessible(true);
        }
        return fPid.getInt(process);
    } catch (Exception e) {
        return -1;
    }
}

From source file:net.ftb.util.OSUtils.java

public static long getPID(Process process) {
    // windows/*ww w . j  av  a 2 s .  com*/
    if (process.getClass().getName().equals("java.lang.Win32Process")
            || process.getClass().getName().equals("java.lang.ProcessImpl")) {
        long pid = -1;
        try {
            Field f = process.getClass().getDeclaredField("handle");
            f.setAccessible(true);
            pid = Kernel32.INSTANCE.GetProcessId((Long) f.get(process));

        } catch (Exception e) {
            pid = -1;
            Logger.logDebug("failed", e);
        }

        return pid;
    }

    if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
        /* get the PID on unix/linux systems */
        long pid = -1;
        try {
            Field f = process.getClass().getDeclaredField("pid");
            f.setAccessible(true);
            pid = f.getInt(process);

        } catch (Throwable e) {
            pid = -1;
            Logger.logDebug("failed", e);
        }
        return pid;
    }

    Logger.logWarn("Unable to find getpid implementation");
    return -1;
}

From source file:com.linkedin.pinot.integration.tests.ChaosMonkeyIntegrationTest.java

private int getProcessPid(Process process) {
    Class<? extends Process> clazz = process.getClass();
    try {/*from w  w w  .j ava  2  s  .  co m*/
        Field field = clazz.getDeclaredField("pid");
        field.setAccessible(true);
        return field.getInt(process);
    } catch (NoSuchFieldException e) {
        return -1;
    } catch (IllegalAccessException e) {
        return -1;
    }
}

From source file:eu.vital.vitalcep.cep.CepProcess.java

private int getPid(Process process) {
    try {/*www .j av  a  2 s.c  o  m*/

        Class<?> cProcessImpl = process.getClass();
        java.lang.reflect.Field fPid = cProcessImpl.getDeclaredField("pid");

        if (!fPid.isAccessible()) {
            fPid.setAccessible(true);
        } else {
            return -1;
        }

        return fPid.getInt(process);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        return -2;
    }
}

From source file:com.netflix.genie.agent.execution.services.impl.LaunchJobServiceImpl.java

private long getPid(final Process process) {
    long pid = -1;
    final String processClassName = process.getClass().getCanonicalName();
    try {//from   w  w w.  j  a v a2 s.co m
        if ("java.lang.UNIXProcess".equals(processClassName)) {
            final Field pidMemberField = process.getClass().getDeclaredField("pid");
            final boolean resetAccessible = pidMemberField.isAccessible();
            pidMemberField.setAccessible(true);
            pid = pidMemberField.getLong(process);
            pidMemberField.setAccessible(resetAccessible);
        } else {
            log.debug("Don't know how to access PID for class {}", processClassName);
        }
    } catch (final Throwable t) {
        log.warn("Failed to determine job process PID");
    }
    return pid;
}

From source file:com.intel.cosbench.driver.handler.TriggerHandler.java

private void getPID(Process process) {
    Field field = null;// w  ww . j  av  a  2  s .  c o  m
    if (!process.getClass().getName().equals("java.lang.UNIXProcess")) {
        LOGGER.error("failed to get PID by {}", process.getClass().getName());
        return;
    }
    try {
        field = process.getClass().getDeclaredField("pid");
        field.setAccessible(true);
        currPID = (Integer) field.get(process);
    } catch (Exception e) {
        LOGGER.error("get PID failed!");
    }
    if (isEnable)
        xferPID = currPID;
    LOGGER.debug("current PID is: {}", currPID);
}