Why do two subprocesses created by Java behave differently?

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

+4
source share
1 answer

C ++ EXE is almost certainly marked as a console application. I think that the jar by default will be considered a graphical application and will perform the standard operation of disconnecting from the main process.

If you have to take the C ++ code and turn it into a graphical application, I think you will see that it behaves similarly to a bank.

+2
source

Source: https://habr.com/ru/post/1307488/


All Articles