Zuriar
Zuriar

Reputation: 11764

how to get the string returned from a call to exec which is in a java.io.OutputStream

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

Upvotes: 1

Views: 143

Answers (3)

ktsujister
ktsujister

Reputation: 501

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.

Upvotes: 1

benzen
benzen

Reputation: 6444

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

Upvotes: 0

Viktor K.
Viktor K.

Reputation: 2740

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"))))

Upvotes: 1

Related Questions