简体   繁体   中英

Shutting down Java program from Ant?

I have an Ant task that builds an executable JAR file, runs some JUnit tests, and then runs the executable JAR file to see if it was built correctly. However, the problem is that I have to be next to my computer to shut down my Java program. Is there a way to shut it down from Ant after it runs for maybe a few seconds?

<target name="buildAndTest" depends="create-submission-jar">
    <!-- Other stuff happening here -->
    <java fork="true" jar="${jar.executable}" failonerror="true" />
</target>

Likely your application is threaded and has a concept of a main loop. I think the easier approach is letting your app accepting an extra parameter like --timeout=120s , and using this parameter to make a Timer that gracefully ends the main loop (I think you'll have some sort of control variable, a PID file or anything). Then Ant will pass this extra parameter, so after some time the process end by itself.

Obviously, when the parameter is not set, the Timer is not created at all and you'll use your normal way to tell the program to stop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM