"javaw.exe" will not exit after System.exit (0)

I am writing this Java program in which I have JFrame and Thread. Everything goes well, except when I press the "X" button to close the program, the program itself closes (the frame and resources are destroyed), but the "javaw.exe" process does not end. I have to interrupt this manually all the time.

I tried, of course, setDefaultCloseOperation (JFrame. * EXIT_ON_CLOSE *), I even tried listening to the awt window with System.exit (0), but it still didn't work.

Any ideas to help?

This is my code. [JavaCV must be installed on your computer to install it.]

class MyGrabber implements Runnable { final int INTERVAL = 1000;// /you may use interval IplImage image; CanvasFrame frame = new CanvasFrame("Web Cam"); public MyGrabber() { frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); } @Override public void run() { FrameGrabber grabber = new OpenCVFrameGrabber(0); // 1 for next camera int i = 0; try { grabber.start(); while (true) { image = grabber.grab(); if (image != null) { cvSaveImage("test.jpg", image); // show image on window frame.showImage(image); } Thread.sleep(INTERVAL); } } catch (InterruptedException e) { e.printStackTrace(); } catch (com.googlecode.javacv.FrameGrabber.Exception e) { e.printStackTrace(); } } } public class TestGrabber { public static void main(String[] args) { MyGrabber gs = new MyGrabber(); Thread th = new Thread(gs); th.start(); } } 
+4
source share
3 answers

I think I found the problem. The problem seems to appear on the page "grabber.start ()"; line. (Due to the fact that, commenting on this line, everything went well, and there appeared a problem with the openCV library. Therefore, I think that it is not easy to get rid of this problem.

Thanks to everyone for the effort though.

Edited: [FOUND THE SOLUTION]

It seems that the OpenCVFrameGrabber class implemented the Thread Runnable interface, so the object created by this class subsequently works like a thread. (Not the same). Anyway, as a solution to this problem, I first released grabber:

 public Test() { //canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); canvas.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.out.println("\nClosing it."); try { //if (grabber != null) grabber.release(); //grabber.stop(); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } System.exit(0); } }); } 
+1
source

Remember that

 The JVM will terminate only and only when All the Non-Daemon threads including the main thread has terminated. You can try System.exit(0) on the main thread, which runs the main method. 

Try creating a class that extends JFrame , and then put

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); in its constructor.

and yes .. in your main method, where you make your frame visible, use EventQueue ..

For instance:

 public static void main(String[] args){ EventQueue.invokeLater(new Runnable(){ public void run(){ myframe.setVisible } }); } 
+3
source
  • Exit your program (using System.exit () or JFrame.EXIT_ON_CLOSE)
  • Go to the task manager and pay attention to the process identifier (pid)
  • Open a terminal window and cd c:\<path>\java\bin (replace <path> with your Java installation)
  • Use jstack <pid> | more jstack <pid> | more . Replace <pid> with the process identifier from the task manager.

Look at threads that are not marked as "daemon". There will be at least one such thread that hangs and has a shutdown handler in the stack trace.

+2
source

All Articles