There is a way to detect the escape key press in Java if you create some form of GUI:
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
ActionListener action = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}...