Skip to content
Advertisement

Exit java program to linux command

I’m making a little tool that handles a sort of exotic device, with lots of options to manage it etc. One of them would be to scan for tty ports, find the right one then throw the user to “minicom” with some parameters for example.

How can I make java completely exit while running a specific command, under certain condition, after such exit ?

Initial thoughts would be to use a bash script, scan the return value, communicate via files etc.A fully functional interactive serial console in Java would be the dream, but the ones I try right now can’t seem to even find tty ports now.

Advertisement

Answer

Most processes on linux follow a call stack where process A calls process B which calls process C. Wen process C terminates, the control goes back to process B, and so on.

It sounds like in this case you want java to call minimum, but when java is finished, return to the parent shell.

I am not aware of any way you can terminate a JVM upon a call to another process (returning to the JVM’s parent when it terminates). Perhaps with some clever C calls using JNI, but that isn’t really java anymore and could create new problems.

You could have the JVM wrap the target process and pass through the user inputs and outputs. Alternatively, use file communication, e.g. the java program writes the command-line to a file, that the parent bash script executes after the JVM terminates, but that is a bit of a kludge.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement