1

I have this:

(import 'java.lang.Runtime)

(defn foo []
    (println (.getOutputStream (. (Runtime/getRuntime) exec "pwd"))))

It successfully returns a java.io.OutputStream (java docs here: http://docs.oracle.com/javase/1.5.0/docs/api/java/io/OutputStream.html

How do I now write this stream using clojure/java interop? I want to get a string of the 'pwd' command.

Thanks

Zuriar
  • 11,096
  • 20
  • 57
  • 92

3 Answers3

1

You can use slurp. Also you have to use getInputStream instead of getOutputStream method (you want to consume the input stream of the process). This snippet should work :

(println (slurp (.getInputStream (.exec (Runtime/getRuntime) "pwd"))))
Viktor K.
  • 2,670
  • 2
  • 19
  • 30
1

This isn't the direct answer to the question, but thought it might be helpful to others to mention that https://clojuredocs.org/clojure.java.shell/sh would be helpful in this situation as well.

ktsujister
  • 471
  • 5
  • 12
0

Maybe this is not the best way to get the current working directory. Take a look here: Getting the Current Working Directory in Java

Community
  • 1
  • 1
benzen
  • 6,204
  • 4
  • 25
  • 37