为什么在使用 java -jar 调用时打印函数不打印?
Why print function is not printing when invoked with java -jar?
我正在使用 leiningen 学习 clojure。我写了一个简单的代码来测试 lein & java -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 没有得到要求的输出?
如果我在源文件中用 println 替换 print ,我得到了需要的输出 lein 运行 & java -jar.
您的进程输出已缓冲。 print
不刷新输出。大多数情况下,您的输出流将设置为自动刷新换行符,这就是显示 println
的原因。如果需要不带换行的flush,可以调用(flush)
函数。
我正在使用 leiningen 学习 clojure。我写了一个简单的代码来测试 lein & java -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 没有得到要求的输出?
如果我在源文件中用 println 替换 print ,我得到了需要的输出 lein 运行 & java -jar.
您的进程输出已缓冲。 print
不刷新输出。大多数情况下,您的输出流将设置为自动刷新换行符,这就是显示 println
的原因。如果需要不带换行的flush,可以调用(flush)
函数。