Reputation: 1145
I need to write a shell script that closes a running .jar and reopens it in terminal. I've never written a shell script and don't really know where to start.
Here's the command I use to run the app in terminal.
java -jar '/home/fdqadmin/NetBeansProjects/dbConvert2/dist/dbConvert2.jar' --Terminal=true
I'm running Ubuntu 10.04 LTS.
Upvotes: 2
Views: 4176
Reputation:
I've commented on here before, but here is an interesting document from developerworks on Java Signal handling which might be of interest if you wish to proceed with this method.
I still believe, however, that as in any case of shutting down a process foreign to your current code module, even if you wrote it, is to ask it to stop via one of the many inter-process communication mechanisms.
Upvotes: 0
Reputation: 28293
What do you by "closing a running .jar"? You're on a Un*x system and hence a "kill -9" is guaranteed to kill the process AND release all the resources the process is using (or your Un*x system isn't compliant, but I've never seen one).
So as soon as you send a kill -9, the Java program, all its threads, everything, will be done. No shutdown hook, no nothing. It's like if you yanked the power cord.
Would a "kill -9" be an acceptable way for you to kill the running Java program? Will the Java program restart gently after having been "kill -9'ed"? (It should restart nicely, but it's not always the case).
If a real kill is fine and if you know the .jar is always launched using that command line and if you know there shall only be one such .jar launched by the user, you could something like that:
#!/bin/bash
kill -9 $(ps aux | grep java | grep dbConvert2 | awk '{print $2}')
java -jar '/home/fdqadmin/NetBeansProjects/dbConvert2/dist/dbConvert2.jar' --Terminal=true
You could also go into java-idiosynchratic land and use "smart" command like 'jps' but I'd rather stay with powerful Un*x tools, thanks a lot :)
Upvotes: 2