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
  • 10,078
  • 18
  • 51
  • 89

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,610
  • 2
  • 18
  • 29
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
  • 441
  • 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
  • 5,690
  • 4
  • 23
  • 36