Here's a solution to the problem I was trying to solve.  I'm not happy with
it (the definition of has-repl?), but it's working for now.
It's also Sun/Oracle JVM specific with the sun.java.command clause.

Again, the problem is that I wanted an exit routine I could call that
wouldn't terminate the jvm when I'm running -main from the REPL, but that
otherwise calls System/exit when running as an uberjar.

Other suggestions welcome.

(defn has-repl?
  "Return true if we appear to be running with a REPL, false otherwise."
  []
  ;; *TBD*: Consider looking for some particular repl routine in jvm stack
traces
  ;; Don't care about the clojure.* prop vals, just that the property
exists.
  (if (or (System/getProperty "clojure.debug")
          (System/getProperty "clojure.compile.path")
          (if-let [command (System/getProperty "sun.java.command")]
            (.contains command "clojure.main")))
    true
    false))

(defn- exit [status & [msg]]
  (when msg
    (println msg))
  (flush)
  (if (has-repl?)
    (throw (ex-info msg {:repl-exit true :status status}))
    (System/exit status)))

;; And in (-main)
    ...
      (catch Exception e
        (if-let [m (ex-data e)]
          ;; exception is of type clojure.lang.ExceptionInfo
          (if (:repl-exit m)
            (:status m)
            (exit 2 (str "Unexpected ex-data:" m "on" e)))
          ;; Caught an exception that wasn't thrown via pseudo-exit
          (if (has-repl?)
            (println (str e))           ;print to REPL
            (exit 2 (str e))))))))      ;print and exit java



On Tue, Apr 22, 2014 at 12:59 PM, Dave Tenny <dave.te...@gmail.com> wrote:

> I have an app I'm building.  It calls System/exit.  That doesn't works so
> well if I'm debugging in the REPL however.
>
> What's the preferred method of determining whether I'm in REPL mode
> interaction vs running as a standalone app?
>
> Also, long as I'm asking and being lazy, does the -main function return
> value translate to a System/exit value if it's numeric?
> Or is System/exit the way to go?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "Clojure" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/clojure/KZJQsmOeiHc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to