I use Java Runtime.getRuntime().exec(command) to create a subprocess and print its pid as follows:
public static void main(String[] args) { Process p2; try { p2 = Runtime.getRuntime().exec(cmd); Field f2 = p2.getClass().getDeclaredField("pid"); f2.setAccessible(true); System.out.println( f2.get( p2 ) ); } catch (Exception ie) { System.out.println("Yikes, you are not supposed to be here"); } }
I tried both the C ++ executable and the Java executable (.jar file). Both executables will continuously print "Hello World" to stdout.
When cmd is C ++ executable, pid is pid to the console, but the subprocess is killed as soon as main() returned. However, when I call the .jar executable in cmd , the subprocess is not killed, which is the desired behavior.
I donβt understand why the same Java code with different executables can behave differently. How do I change my code so that I can have persistent subprocesses in Java?
PS: I am using Ubuntu 9.10 and OpenJDK-1.6. (Not sure if they are important ~)
New to this area. Any suggestions are welcome.
Lily
source share