如何退出 REPL

发布于 2024-10-16 22:13:46 字数 260 浏览 9 评论 0原文

我正在尝试退出 REPL。

我使用 (. System exit 0)(System/exit 0),但这会导致错误:

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

是否还有其他方法退出 REPL?我该如何解决这个错误?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

述情 2024-10-23 22:13:46

您可以发送“文件结束”字符。

您只需按 ctrl-d (*nix) 或 ctrl-z (Windows) 即可退出 REPL。

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

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

我只土不豪 2024-10-23 22:13:46

我的答案已经有 10 年历史了,而且人们对它的理解还不够深入(尽管我认为我和最初的提问者有着同样的困惑,所以它还是有效的)。

(System/exit 0) 确实退出整个 JVM - 这可能是您想要的“退出 REPL”,但不一定。 Clojure 及其 REPL 被设计为在多线程环境中运行,您甚至可以将多个 REPL 连接到同一进程。显然,如果您想在一个持续运行的进程中退出 REPL,那么退出 JVM 并不是您想要的。

原答案如下:

您的代码中似乎存在不同的问题。

退出repl的方式(System/exit 0)

替代语法(.System exit 0)也有效。

您可以从一个干净的 repl 开始测试这一点:
java -cp clojure.jar clojure.main -r

您得到的异常似乎表明在代码到达预期退出点之前某些索引查找中出现错误,显然是在不同的线程上。

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.

瀞厅☆埖开 2024-10-23 22:13:46

(System/exit 0) 的问题是它会杀死整个 JVM。真正的问题是如何以编程方式退出当前的 repl,并返回到启动该 repl 的任何函数。

这是一个方便的技巧:

(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 重复调用读取器(默认情况下为 repl-read),一次获取一个表单。向读者提出的论点之一是一个特殊的哨兵对象,当没有更多的表单可供阅读时,读者应该返回该对象。默认读取器 repl-read 仅在 EOF 时返回哨兵值。在 repl-read 提示符处,您无权访问哨兵对象,因此您无法返回它来告诉评估者您已完成表单输入。通过安装您自己的阅读器,您可以检查特定的形式(例如,符号退出),并在读取此形式时返回哨兵对象,从而向评估器表明您已准备好退出 repl,而无需实际杀死整个虚拟机。

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.

断舍离 2024-10-23 22:13:46

我只是想退出我的 REPL 并降落到这里。

当开始在 Clojure REPL 中执行第一步时,这似乎是每个人都会想到的一个问题。当然,我没有阅读启动消息。我的 Clojure 1.7.0 的答案是(退出)或(退出)或 Control-d,如其他回复中所述。

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!

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!
祁梦 2024-10-23 22:13:46

您可以使用以下组合键退出 emacs 中的 Cider REPL:Cc Cq

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

浅唱々樱花落 2024-10-23 22:13:46

如果您安装“lein”,则exitquit如下所示:

$ lein repl
...
user=>exit
Bye for now!

If you install "lein" then it's exit or quit like this:

$ lein repl
...
user=>exit
Bye for now!
我不是你的备胎 2024-10-23 22:13:46

在 Linux 上安装 Clojure 1.10.2

(. System exit 0)

On Clojure 1.10.2 installed on Linux

(. System exit 0)
仙女 2024-10-23 22:13:46

在 Mac 上,使用 CTRL + C 两次,请参阅:

REPL Image

On Mac, use CTRL + C twice, see:

REPL Image

舂唻埖巳落 2024-10-23 22:13:46

退出repl:
如果您从命令行运行 repl,那么(正如 Sean 提到的)ctrl-d
如果你是从 slime 运行它,那么 Alt-x slime-quit-lisp 应该可以做到。
如果您从 Eclipse 运行它,那么我不确定是否有一种干净的退出方式,请使用红色小按钮。

to exit the repl:
If you're running the repl from the command line then (as Sean mentions) ctrl-d
if you're running it from slime then Alt-x slime-quit-lisp should do it.
if you're running it from eclipse then i'm not sure there is a clean way to exit use the little red button.

女皇必胜 2024-10-23 22:13:46

今天我得到了答案,这就是我需要的

(import ('java.lang.management ManagementFactory)
(use 'clojure.contrib.shell)
(defn process-pid [] (let [m-name (.getName (ManagementFactory/getRuntimeMXBean))] (first (.split m-name "@"))))

(defn exit (sh "kill" (process-pid))

today I have the answer, this is what I need

(import ('java.lang.management ManagementFactory)
(use 'clojure.contrib.shell)
(defn process-pid [] (let [m-name (.getName (ManagementFactory/getRuntimeMXBean))] (first (.split m-name "@"))))

(defn exit (sh "kill" (process-pid))
音盲 2024-10-23 22:13:46

在 Windows 中(在 Windows 7 x64 上测试),使用标准命令 shell (cmd.exe),ctrl-Z(后跟 Enter 键)字符似乎是文件结束指示符,它使我脱离了REPL 返回命令提示符。我怀疑这在 Windows PowerShell 中也同样有效,熟悉它的人请测试并确认...

另请注意,这适用于库存 Clojure REPL - 如果您正在运行 Datomic shell(这似乎是一个Java Beanshell 的实例),输入“quit();”...

HTH。

In Windows (tested on Windows 7 x64), using the standard command shell (cmd.exe), the ctrl-Z (followed by the enter key) character appears to be the end-of-file indicator, it dropped me out of the REPL back to the command prompt. I suspect that this will also work equivalently in Windows PowerShell, would someone who is familiar with it please test and confirm...

Note also that this is for the stock Clojure REPL - if you are running the Datomic shell (which appears to be an instance of a Java Beanshell), enter "quit();"...

HTH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文