【问题标题】:Why print function is not printing when invoked with java -jar? [duplicate]为什么使用 java -jar 调用时打印功能不打印? [复制]
【发布时间】:2016-12-20 22:58:22
【问题描述】:

我正在和 leiningen 一起学习 clojure。我编写了一个简单的代码来测试 leinjava -jar 命令。这是我的 project.clj 文件:

$ cat project.clj 
(defproject hello "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.8.0"]]
  :main ^:skip-aot hello.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}})

这是我的源代码:

$ cat src/hello/core.clj 
(ns hello.core
  (:gen-class))

(defn -main
  [& args]
  (print "Hello, World!"))

当我使用 lein 运行此代码时,它运行良好。显示在这里

$ lein run
Hello, World!$

当我尝试运行 java -jar 时,它不起作用

$ lein uberjar
Compiling hello.core
Created /home/rishi/hello/target/uberjar/hello-0.1.0-SNAPSHOT.jar
Created /home/rishi/hello/target/uberjar/hello-0.1.0-SNAPSHOT-standalone.jar

$ java -jar target/uberjar/hello-0.1.0-SNAPSHOT-standalone.jar

我不明白,为什么我没有使用 java -jar 获得所需的输出?

如果我在源文件中将 print 替换为 println,我会同时使用 lein runjava -jar。

【问题讨论】:

    标签: java jar clojure leiningen


    【解决方案1】:

    您的进程输出已缓冲。 print 不会刷新输出。大多数情况下,您的输出流将设置为自动刷新换行符,这就是显示println 的原因。如果您需要在没有换行符的情况下刷新,您可以调用(flush) 函数。

    【讨论】:

    • 酷!我明白了。我得到了我的答案。谢谢。 @noisesmith
    • 我来! #(我来!)
    猜你喜欢
    • 2020-01-11
    • 1970-01-01
    • 2021-06-24
    • 2018-07-10
    • 2015-11-28
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    相关资源
    最近更新 更多