There is a ToggleButton button, when pressed, the server framework should be launched from the code in a separate thread.
there is no possibility to terminate the server normally, separately in the console it ends via Ctrl+C
It is necessary that when the ToggleButton button is activated, the server works, and when it is deactivated ( selest=false ), the thread selest=false and the variable is released for re-activation.

Now so does not work

 Thread threadTerminalServer; boolean init = false; @FXML private void handleButtonTerminalServer(ActionEvent event) { if (init == false) { threadTerminalServer = new Thread(new Runnable() { @Override public void run() { StartTerminalServer startTerminalServer = null; try { if (buttonTerminalServer.isSelected()) { startTerminalServer = new StartTerminalServer(); System.out.println("Start"); init = true; } } catch (Exception e) { e.printStackTrace(); } } }); } if (buttonTerminalServer.isSelected()) { threadTerminalServer.start(); } else { System.out.println("Stop-1"); threadTerminalServer.stop(); init = false; threadTerminalServer = null; Runtime.getRuntime().gc(); } } 
  • @ hitman249 Didn't you try hard Thread.interrupt() ? - Barmaley
  • @Barmaley, tried, does not help, maybe something was wrong doing - hitman249

2 answers 2

You seem to misunderstand the work of your Thread and threadTerminalServer - the fact is that it stops working after launching your terminal. And the terminal lives in its thread and on the drum what happened to its parent.

You need to find a way to kill the terminal thread, and this you cannot because you have no information inside what the terminal lives.

I would suggest this method:

  1. Run Process.exec() - actually a copy of the axis interpreter
  2. We are launching a terminal inside it (that is, we now know what your terminal lives inside)
  3. Further, if necessary, kill the command interpreter of the axis via Process.destroy()

A little manual how to work with Process.exec() here

    For such purposes, they simply create a Boolean flag and add checks on this flag in a loop that is executed by the stream.

    • Well, I will create a flag, complete how? return th is not working - hitman249
    • You have the StartTerminalServer constructor called there, what is this class like? Do you have all the server logic in its constructor? Or is there another stream created? - nitrocaster
    • there, and then there goes the server in which I can’t change anything, and then there are already other threads created. public class StartTerminalServer {public StartTerminalServer () {try {System.setProperty ("port", "7788"); System.setProperty ("jfx_use_udp", "true"); TerminalServer.main (new String [] {}); } catch (Exception e) {}}} - hitman249
    • Where does the TerminalServer class come from? - nitrocaster
    • from the launch line in the bat file :) it works fine, I just can't complete it normally - hitman249