Justin Wojciechowski
Justin Wojciechowski

Reputation: 35

Java - Destroying a process made in one method from another method

I am trying to destroy a process I make in one method from a button action performed method. Once the button is clicked I want the process to close. I tried to research this and have tried

Runtime.getRuntime().exec("TASKKILL /F /IM tabtip.exe");

This wouldn't close the process. It's an on screen keyboard on my computer that I call to pop up, but I want it to close on the button click. Here is the call of the process.

public void openProcess()
   {
           try {
            proc = Runtime.getRuntime().exec("cmd /c C:\\Windows\\System32\\tabtip.exe");
        } catch (IOException ex) {
            Logger.getLogger(CustomerEnterName.class.getName()).log(Level.SEVERE, null, ex);
        }
   }

Here is the button press action method

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   try {
       Runtime.getRuntime().exec("TASKKILL /F /IM TabTip.exe");
   } catch (IOException ex) {
       Logger.getLogger(CustomerEnterName.class.getName()).log(Level.SEVERE, null, ex);
   }

I want to destroy the process within this jButton method.. am I either doing the TASKKILL wrong or can I somehow call the destroy method from within this class? I've tried everything I could find. Thanks.

Upvotes: 0

Views: 223

Answers (1)

Jigar Joshi
Jigar Joshi

Reputation: 240908

pass on the proc reference to second method or make it safely accessible and then

process.destroy();

in platform independent way


Upvotes: 4

Related Questions