B4J Question How to kill java.exe process

le_toubib

Active Member
Licensed User
Longtime User
Hii all
My project is a client For a local phpmysql server ... But also is a push server ....
When i run it from the IDE it works fine.
But When i run it through the compiled jar file, it will run normally in the first time but on the second run , it Will immediately shut down , apparently because java.exe is still running , using the same port... So when i terminate the process via task manager , it will then start normally.
Is there a way to prevent this , or perhaps kill the process programmatically , or am i doing something wrong ?
 

le_toubib

Active Member
Licensed User
Longtime User
I mean you are right ...
May be My question was wrong... I cannot kill the process on the second run ...
_So how to terminate it on exit of the first run .
_or how to free the used port so that i can reuse it again?
_ is there a way to prevent this all from happening ?
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Why is a second instance starting up while a previous instance is still running? I think you should reconsider how the program is designed as this isn't optimal behavior.

However, if this behavior is unavoidable, here's how to work around it:
  1. Upon start up and before opening the port, the program should check for the existence of a file (can be in File.DirApp or File.DirTemp or File.DirData) called PIDs.txt.
  2. If such a file is found, get the process ID (PID) written inside that file.
  3. Delete the PIDs.txt file.
  4. Use the jAWTRobot library to kill the process with that PID.
  5. Open the desired port.
  6. Use the jAWTRobot library to get THIS process' ID and then write it to PIDs.txt
This way, each new instance of your program will kill the previous instance if it is still running, freeing up the port, start the port and write its own PID to the PIDs.txt file (for the next instance to use).
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
Thanks that's a really cool solution...
But you posed an important question .... Why is the previous instance of java.exe is still running after i terminated the app ( through the X button )...
Should i use exitapplication instead ?
And Why is this behaviour occurring only with the .jar file and not through the IDE?
P.S : I'm not using Any forms of TSR inside ... And i assume the push server Should terminate upon exit..
Any ideas ?
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
What are you doing in the MainForm_ClosedRequested event? Merely closing all the forms won't necessarily end the program. Using ExitApplication should do it.

Are you running the program in Release Mode when in the IDE?
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
Its a UI application .
Its a client For a phpmysql apache server. + erel's push server app adapted inside.
I'm not using the jserver library.
Adding exitapplication inside the mainform_closedrequested sub fixed the problem.
 
Upvote 0
Top