Any way to “reboot” the JVM?

后端 未结 8 1025
难免孤独
难免孤独 2020-12-05 17:46

Is there any way to reboot the JVM? As in don\'t actually exit, but close and reload all classes, and run main from the top?

8条回答
  •  旧时难觅i
    2020-12-05 18:01

    Your best bet is probably to run the java interpreter within a loop, and just exit. For example:

    #!/bin/sh
    while true
    do
        java MainClass
    done
    

    If you want the ability to reboot or shutdown entirely, you could test the exit status:

    #!/bin/sh
    STATUS=0
    while [ $STATUS -eq 0 ]
    do
        java MainClass
        STATUS=$?
    done
    

    Within the java program, you can use System.exit(0) to indicate that you want to "reboot," and System.exit(1) to indicate that you want to stop and stay stopped.

提交回复
热议问题