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:Main.java

public static Integer getPID(Process process) {
    Integer pid = null;//from   ww w .java 2 s  .  c  om
    if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
        /* get the PID on unix/linux systems */
        try {
            Field f = process.getClass().getDeclaredField("pid");
            f.setAccessible(true);
            pid = f.getInt(process);
        } catch (Throwable e) {
        }
    }
    return pid;
}

From source file:Main.java

public static int getPid(Process process) {
    int pid = 0;//  w  w  w.ja v  a  2  s . com
    String pname = process.getClass().getName();
    if (pname.equals("java.lang.ProcessManager$ProcessImpl")) {

        try {
            java.lang.reflect.Field f = process.getClass().getDeclaredField("pid");
            f.setAccessible(true);
            pid = f.getInt(process);
        } catch (Throwable e) {
        }
    }
    return pid;
}

From source file:com.codeabovelab.dm.common.utils.ProcessUtils.java

/**
 * Get system ProcessID of specified process. <p/>
 * Currently it method work only on unix systems.
 * @param process/*from  w  w  w .j  a  v a  2  s  . co m*/
 * @return PID or -1 on fail
 */
public static int getPid(Process process) {
    Class<? extends Process> clazz = process.getClass();
    try {
        Field field = clazz.getDeclaredField("pid");
        if (field.isAccessible()) {
            field.setAccessible(true);
        }
        return (int) field.get(process);
    } catch (IllegalAccessException | NoSuchFieldException e) {
        return -1;
    }
}

From source file:org.apache.hadoop.hdfs.ManualSyncTester.java

public static int getUnixPID(Process process) throws Exception {
    if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
        Class cl = process.getClass();
        Field field = cl.getDeclaredField("pid");
        field.setAccessible(true);/*w  w w . j  a va 2s . c o m*/
        Object pidObject = field.get(process);
        return (Integer) pidObject;
    } else {
        throw new IllegalArgumentException("Needs to be a UNIXProcess");
    }
}

From source file:org.apache.hadoop.ha.ShellCommandFencer.java

/**
 * Attempt to use evil reflection tricks to determine the
 * pid of a launched process. This is helpful to ops
 * if debugging a fencing process that might have gone
 * wrong. If running on a system or JVM where this doesn't
 * work, it will simply return null./* w ww . j av a2  s .c om*/
 */
private static String tryGetPid(Process p) {
    try {
        Class<? extends Process> clazz = p.getClass();
        if (clazz.getName().equals("java.lang.UNIXProcess")) {
            Field f = clazz.getDeclaredField("pid");
            f.setAccessible(true);
            return String.valueOf(f.getInt(p));
        } else {
            LOG.trace("Unable to determine pid for " + p + " since it is not a UNIXProcess");
            return null;
        }
    } catch (Throwable t) {
        LOG.trace("Unable to determine pid for " + p, t);
        return null;
    }
}

From source file:edu.umd.cs.marmoset.utilities.JProcess.java

/**
 * Uses reflection to extract the pid, a private field of the private class UNIXProcess.
 * This will fail on any non-Unix platform that doesn't use UNIXProcess.  It may
 * fail if the UNIXProcess class changes at all.  It may fail anyway for unpredictable
 * reasons.//  w  ww  .java2 s. co  m
 * @param process The process
 * @return the pid of this process
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
public static int getPid(Process process) throws NoSuchFieldException, IllegalAccessException {
    Class<? extends Process> processClass = process.getClass();
    Field pidField = processClass.getDeclaredField("pid");
    pidField.setAccessible(true);
    return pidField.getInt(process);
}

From source file:com.ikanow.aleph2.data_model.utils.ProcessUtils.java

/**
 * Returns the pid for the given process
 * /*  w w  w  . j  a  v  a 2  s. com*/
 * @param px
 * @return
 */
private static String getPid(Process px) {
    try {
        final Class<?> ProcessImpl = px.getClass();
        final Field field = ProcessImpl.getDeclaredField("pid");
        field.setAccessible(true);
        return Integer.toString(field.getInt(px));
    } catch (Throwable t) {
        return "unknown";
    }
}

From source file:org.midonet.util.process.ProcessHelper.java

public static int getProcessPid(Process process) {
    Field field;//ww  w .  j a va  2  s.  c  o m
    try {
        field = process.getClass().getDeclaredField("pid");
        field.setAccessible(true);
        Object o = field.get(process);
        if (o instanceof Integer) {
            return Integer.class.cast(o);
        }

        return -1;
    } catch (NoSuchFieldException e) {
        return -1;
    } catch (IllegalAccessException e) {
        return -1;
    }
}

From source file:net.pms.util.ProcessUtil.java

public static Integer getProcessID(Process p) {
    Integer pid = null;//from   w  w  w . jav a2s  . c  o m

    if (p != null && p.getClass().getName().equals("java.lang.UNIXProcess")) {
        try {
            Field f = p.getClass().getDeclaredField("pid");
            f.setAccessible(true);
            pid = f.getInt(p);
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException
                | IllegalAccessException e) {
            LOGGER.debug("Can't determine the Unix process ID: " + e.getMessage());
        }
    }

    return pid;
}

From source file:edu.umd.cs.marmoset.utilities.MarmosetUtilities.java

/**
 * Uses reflection to extract the pid, a private field of the private class UNIXProcess.
 * This will fail on any non-Unix platform that doesn't use UNIXProcess.  It may
 * fail if the UNIXProcess class changes at all.  It may fail anyway for unpredictable
 * reasons./*from w w w  .  j av  a2 s.  com*/
 * @param process The process
 * @return the pid of this process
 */
public static int getPid(Process process) {
    try {
        Class<? extends Process> processClass = process.getClass();
        Field pidField = processClass.getDeclaredField("pid");
        pidField.setAccessible(true);
        return pidField.getInt(process);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}