简体   繁体   中英

How to prevent JFrame/EXIT_ON_CLOSE from killing the nrepl server?

EDIT Using DISPOSE_ON_CLOSE is not what I'm after, because them I've got the "inverse" problem: the REPL stays alive when I close the JFrame (which is good) but then it also stays alive when launced from outside the REPL (which is bad).

I'm writing a Swing application in Clojure and I find it convenient to both test thing from the REPL ( nrepl under Emacs) and from outside the REPL (for example by using lein run or by running the standalone .jar ).

When I'm not launching my Swing application from the REPL, I find it convenient to set the default close operation to be EXIT_ON_CLOSE. For example:

 (.setDefaultCloseOperation jframe JFrame/EXIT_ON_CLOSE)

So I can click on the JFrame's close button and be done with my app.

However apparently (I may be mistaken on this but I think I'm not seeing things) this is problematic when run from the REPL: as soon as I click on the JFrame's close button I apparently kill the nrepl server and have to reopen a new nrepl .

Knowing that I need to both test from nrepl and from outside any REPL, how can I solve my problem?

Should I "detect" that I'm run from a REPL and then not set the default close operation to exit? Or?

JFrame.EXIT_ON_CLOSE exits the application by calling System.exit() which terminates the currently running Java virtual machine . Since both your application and the REPL run inside the same JVM (since you've started your app from the REPL), this means that the REPL will get killed as well.

Instead, try using:

(.setDefaultCloseOperation jframe JFrame/DISPOSE_ON_CLOSE)

This will also hide (and dispose) the JFrame when you click its close button, but keep your JVM -- and hence your REPL -- running.

Use an environment variable when starting your REPL and choose whether to do dispose on close or exit onclose depending on that variable.

When launch standalone, the variable check would fail and you can thus do exit on close.

Edit:

In the shell:

export REPL_MODE=true ; lein repl

In the clojure code:

(def repl-mode (System/getenv "REPL_MODE"))
(if (not (nil? repl-mode))
     ; in repl, set to dispose on close
     ; in standalone mode, set to exit on close

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM