Skip to main content
Removed "down" since it doesn't actually count down, but rather up.
Source Link

Okay. If simply calling System.exit isn't permitted in Java, how about calling it via reflection from another thread?

import java.lang.reflect.*; public class Quit { public static void main(String[] args) throws Exception { final Method exit = System.class.getMethod("exit", new Class<?>[]{ int.class }); new Thread(new Runnable() { @Override public void run() { try { System.out.println("calling... " + exit); exit.invoke(null, new Object[] { 0 }); } catch (Exception e) { e.printStackTrace(); } } }).start(); for (int i = 1; ; ++i) { System.out.println("counting... " + i); try { Thread.sleep(1000); } catch (InterruptedException e) { break; } } } } 

Looks up the exit method, spawns a new thread, and counts downdown until that thread kills the process by calling exit via reflection.

Okay. If simply calling System.exit isn't permitted in Java, how about calling it via reflection from another thread?

import java.lang.reflect.*; public class Quit { public static void main(String[] args) throws Exception { final Method exit = System.class.getMethod("exit", new Class<?>[]{ int.class }); new Thread(new Runnable() { @Override public void run() { try { System.out.println("calling... " + exit); exit.invoke(null, new Object[] { 0 }); } catch (Exception e) { e.printStackTrace(); } } }).start(); for (int i = 1; ; ++i) { System.out.println("counting... " + i); try { Thread.sleep(1000); } catch (InterruptedException e) { break; } } } } 

Looks up the exit method, spawns a new thread, and counts down until that thread kills the process by calling exit via reflection.

Okay. If simply calling System.exit isn't permitted in Java, how about calling it via reflection from another thread?

import java.lang.reflect.*; public class Quit { public static void main(String[] args) throws Exception { final Method exit = System.class.getMethod("exit", new Class<?>[]{ int.class }); new Thread(new Runnable() { @Override public void run() { try { System.out.println("calling... " + exit); exit.invoke(null, new Object[] { 0 }); } catch (Exception e) { e.printStackTrace(); } } }).start(); for (int i = 1; ; ++i) { System.out.println("counting... " + i); try { Thread.sleep(1000); } catch (InterruptedException e) { break; } } } } 

Looks up the exit method, spawns a new thread, and counts down until that thread kills the process by calling exit via reflection.

Source Link

Okay. If simply calling System.exit isn't permitted in Java, how about calling it via reflection from another thread?

import java.lang.reflect.*; public class Quit { public static void main(String[] args) throws Exception { final Method exit = System.class.getMethod("exit", new Class<?>[]{ int.class }); new Thread(new Runnable() { @Override public void run() { try { System.out.println("calling... " + exit); exit.invoke(null, new Object[] { 0 }); } catch (Exception e) { e.printStackTrace(); } } }).start(); for (int i = 1; ; ++i) { System.out.println("counting... " + i); try { Thread.sleep(1000); } catch (InterruptedException e) { break; } } } } 

Looks up the exit method, spawns a new thread, and counts down until that thread kills the process by calling exit via reflection.