Получите идентификатор подпроцесса в Java

(Мой другой комментарий проявляет практический подход. Вот теоретическая сторона.)

я искал стандарт ECMA 262 , который является тем, что реализует JavaScript. Их спецификация для isNan:

Применяет ToNumber к его аргументу, затем возвращает true, если результат является NaN, и иначе возвращает false.

Раздел 9.3 определяет поведение ToNumber (который не является вызываемой функцией, а скорее компонентом системы преобразования типов). Для суммирования таблицы определенные входные типы могут произвести NaN. Это тип undefined, тип number (но только значение NaN), любой объект, примитивное представление которого NaN, и любой string, который не может быть проанализирован. Это уезжает undefined, NaN, new Number(NaN), и большинство строк.

Любой такой вход, который производит NaN как вывод, когда передано [1 110], произведет true, когда питается к [1 112]. С тех пор null может успешно быть преобразован в число, оно не производит true.

И именно поэтому.

14
задан Pawka 13 December 2009 в 20:33
поделиться

4 ответа

There is still no public API for this (see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4244896) but there are workarounds.

A first workaround would be to use an external program like ps and to call it using Runtime.exec() to get the pid :)

Another one is based on the fact that the java.lang.Process class is abstract and that you actually get a concrete subclass depending on your platform. On Linux, you'll get a java.lang.UnixProcess which has a private field int pid. Using reflection, you can easily get the value of this field:

Field f = p.getClass().getDeclaredField("pid");
f.setAccessible(true);
System.out.println( f.get( p ) );
28
ответ дан 1 December 2019 в 08:52
поделиться

I tried (and failed) to do this a while back. I ended up wrapping my command in a shell script that dumped the pid to a file. Not the best solution but it got me past this hurdle.

1
ответ дан 1 December 2019 в 08:52
поделиться

From here

public static void main(String[] args) throws IOException {
    byte[] bo = new byte[100];
    String[] cmd = {"bash", "-c", "echo $PPID"};
    Process p = Runtime.getRuntime().exec(cmd);
    p.getInputStream().read(bo);
    System.out.println(new String(bo));
}
1
ответ дан 1 December 2019 в 08:52
поделиться

Well there is no documented way to do this, but it happens that the Process implementation class is UNIXProcess, and it has a pid field. So you could use reflection to access this private field to get the ID. Googling around you will find other tricks of calling another shell to get ps output and that kind of thing. Nothing easy.

1
ответ дан 1 December 2019 в 08:52
поделиться
Другие вопросы по тегам:

Похожие вопросы: