How to exit the REPL

27,307

Solution 1

You can send the 'end-of-file' character.

You can just press ctrl-d (*nix) or ctrl-z (Windows) to exit the REPL.

Solution 2

My answer is now 10 years old and was given a context of less understanding (although I think I shared the same confusion as the original asker so it kind of works).

(System/exit 0) does indeed exit the whole JVM - that might be what you want "to exit the REPL" but not necessarily. Clojure and it's REPL are designed to run in a multi-threaded environment and you can even have multiple REPLs connected to the same process. Obviously exiting the JVM is not what you want if you want to exit a REPL in an otherwise continuing process.

Original answer below:

It looks like you have a different problem in your code.

The way to exit the repl is:(System/exit 0)

The alternative syntax (. System exit 0) also works.

You can test this from a clean repl started with: java -cp clojure.jar clojure.main -r

The exception you get would seem to indicate an error in some indexed lookup before your code gets to the intended exit point, apparently on a different thread.

Solution 3

The problem with (System/exit 0) is that it kills the whole JVM. The real question is how to programmatically exit just the current repl, and return to whatever function launched that repl.

Here is a convenient hack:

(clojure.main/repl
  ; Exit the repl whenever the user enters "exit" at the prompt.
  :read (fn [request-prompt request-exit]
          (let [form (clojure.main/repl-read request-prompt request-exit)]
            (if (= 'exit form) request-exit form))))

clojure.main/repl repeatedly calls a a reader, by default repl-read, to get one form at a time. One of the arguments to the reader is a special sentinel object that the reader is supposed to return when there are no more forms to be read. The default reader, repl-read, returns the sentinel value only on EOF. At the repl-read prompt, you do not have access to the sentinel object, so you cannot return it to tell the evaluator that you have finished entering forms. By installing your own reader, you can check for a particular form -- e.g., the symbol exit -- and return the sentinel object whenever this form is read, thus indicating to the evaluator that you are ready to exit the repl, without actually killing the entire VM.

Solution 4

i just wanted to exit my REPL and landed here.

This seems to be a question that comes to everyones mind when starting to do first steps in the Clojure REPL. And of course I did not read the start-up message. The answer for my Clojure 1.7.0 is (exit) or (quit) or Control-d as stated in other replies.

nREPL server started on port 49276 on host 127.0.0.1 - nrepl://127.0.0.1:49276
REPL-y 0.3.7, nREPL 0.2.10
Clojure 1.7.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_72-b15
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e

user=> (exit)
Bye for now!

Solution 5

You may use the following key combination to exit Cider REPL in emacs: C-c C-q

Share:
27,307
patz
Author by

patz

Updated on July 22, 2021

Comments

  • patz
    patz almost 3 years

    I'm trying to exit the REPL.

    I use (. System exit 0) or (System/exit 0), but that causes an error:

    Exception in thread "Thread-3" java.lang.RuntimeException: java.lang.IndexOutOfBoundsException
    

    Is there another way to exit the REPL? How I can resolve this error?

  • patz
    patz about 13 years
    thanks for respond but i need that the program finish automatically, my "archive.clj" has code and in the end the (. System exit 0)
  • patz
    patz about 13 years
    thanks for respond but i need that the program finish automatically, my "archive.clj" has code and in the end the (. System exit 0)
  • patz
    patz about 13 years
    I deleted all the code from my main, and started the repl, when I writing (System / exit 0) continues giving me this error...
  • Alex Stoddard
    Alex Stoddard about 13 years
    How are you starting the repl - is it from within an IDE? Something about your setup is not matching usual expectations. The above example was with a repl started from the command line.
  • patz
    patz about 13 years
    ­I use the terminal of linux to begin the repl with -lein repl- in the folder of my project. ­in the folder src/myproyect the archive core.clj has not code (and is the :main of my project.clj)... when I start the repl i don't have problems, inmedietly writing (System / exit 0) and I have the error =/
  • Alex Stoddard
    Alex Stoddard about 13 years
    Try things outside of the project tree entirely. Both with the plain java command above and with lein repl. (You should be able to get a repl without any project set up with lein). Something about your project code or structure is likely causing the issue. Maybe you have a code that is being evaluated before your main function is ever called.
  • patz
    patz about 13 years
    I trying with a empty project (only the basic information who put lein) and when I writing (System/exit 0) give me the same error, in a folder without a project (with the repl of clojure.core) no have problems to exiting
  • Alex Stoddard
    Alex Stoddard about 13 years
    So it seems like it is something in your project set up. Perhaps you could paste your project.clj file and any others to something like gist.github.com, otherwise I doubt people can help further.
  • patz
    patz about 13 years
    finally I make the jar file, when i execute java -jar myproyect.jar no have problems with the (Syste.exit 0)
  • Robert P
    Robert P about 12 years
    @patz why are you running an automatic program in REPL mode?
  • Phil Goetz
    Phil Goetz over 10 years
    No; typing ctrl-d in repl also closes the terminal you ran telnet in.
  • Sean Corfield
    Sean Corfield about 10 years
    @PhilGoetz I've never seen ctrl-d in the Clojure REPL close the Terminal in which it was running (unless you have a *nix user with lein repl as their login shell??).
  • Michael Butler
    Michael Butler about 10 years
    I'm on Windows with PowerShell, Ctrl-D doesn't exit the Clojure repl :(
  • rezwits
    rezwits over 5 years
    The Real Answer!!! Because all he asked was how do I EXIT, not how do I Exit and Kill!
  • Abdurrahman Adebiyi
    Abdurrahman Adebiyi over 4 years
    On my Mac , using brew installed clojure for verion Clojure 1.10.1 (exit) failed with error user=> (exit) Syntax error compiling at (REPL:1:1). Unable to resolve symbol: exit in this context . I ha dto use ( (. System exit 0) to move.@andreas-guther I see the header ... nREPL server .... in yours . Could version 1.7 and below worked with exit out of the box
  • Austin
    Austin over 2 years
    Had the same issue as OP. Ctrl-D had the desired effect of getting me out of the lein repl. Thank you.