Fork me on GitHub
#tools-deps
<
2018-10-16
>
kenny02:10:27

I have this code:

(ns where-print
  (:require
    [clojure.spec.alpha :as s]))

(s/def ::foo string?)

(defn -main
  [& args]
  (when-not (s/valid? ::foo 1)
    (s/explain ::foo 1)
    (System/exit 1)))
If I run clj -m where-print nothing is printed to stdout. Any idea how to prevent this from happening?

seancorfield02:10:21

(System/exit 1) is a pretty brutal way to exit the program and it can quit the JVM before output is flushed. If you add (flush) before that exit, you should get your output.

seancorfield02:10:47

It would also work if you did this:

(defn -main
  [& args]
  (when-not (s/valid? ::foo 1)
    (println (s/explain-str ::foo 1))
    (System/exit 1)))
since println will flush stdout to the newline (although your output will now have two newlines) @kenny

kenny03:10:56

@seancorfield thanks. By brutal, are you hinting at a better way to exit the process with a unsuccessful exit code?

seancorfield03:10:51

No. Just observing that it it immediately kicks off a shutdown of the JVM and some things may not get done that you are expecting 🙂

dottedmag06:10:02

JVM — Unleash your inner Hulk!