lein run and standard input (*in*) with read-line

401 views
Skip to first unread message

Tim Cross

unread,
Mar 3, 2012, 1:25:37 AM3/3/12
to clo...@googlegroups.com
Hi All,

I've been working on my first little clojure project for the past couple of days, but have now run into a problem with leiningen and was hoping someone could point me in the right direction to find a solution. 

I'm using lein 1.7.0, clojure 1.3.0 and openJDK 6 on a ubuntu based system. 

My code has a simple loop iin the -main funciton which calls read-line to get the input. 

If I run lein repl, execute (-main), I can enter data 
If I create an uberjar, and run it, I can enter data

However, if I use lein run, I cannot eneter any data. It appears the program enters the while loop, but when I try to enter data, nothing happens - no characters are echoed to the screen (as is the case in the other two invokations) and it appears that the program is waiting for input from some other input source. 

My guess is that lein run does something different wrt *in*, but I've no idea what and cannot find anything obvious in the docs. 

My project file is very simple -

(defproject clj-espeak "1.0.0-SNAPSHOT"
  :description "FIXME: write description"
  :dependencies [[org.clojure/clojure "1.3.0"]]
  :java-source-path "java"
  :ant [clj-espeak.core]
  :main clj-espeak.core)

The java-source-path is necessary for some java interface classes for a JNI based interface to a C based text to speech library. This seems to be working fine. 

My -main funciton is very simple

(defn -main [& args]
  (println "Starting interpreter")
  (tts-initialize)
  (println "Starting command loop.....")
  (flush)
  (while true
    (dispatch (parse-input (read-line)))))

As mentioned, this is my first clojure program. I'm also not a java programmer (though I did do some java back in the late 90's using java 1.0). I'm hoping it is just something obvious I've missed and would appreciate any suggestions. 

Background

The reason I want to use lein run is that I want to execute the program via a shell script which is called by emacs. Emacs then communicates with this sub-process via standard input. The data it sends are commands to generate various bits of speech from text. Eventually, the aim is to have emacs start the clojure applicaiton, which will setup swank and also listen for input from emacs. I will then use slime to connect to the running process so that I can futher develop/refine the clojure code based on the input recieved from emacs. This is all part of developing a clojure based speech server for the emacspeak program. 

TIA

Tim

Phil Hagelberg

unread,
Mar 3, 2012, 12:54:00 PM3/3/12
to clo...@googlegroups.com
Tim Cross <theop...@gmail.com> writes:

> My guess is that lein run does something different wrt *in*, but I've
> no idea what and cannot find anything obvious in the docs.

Try using "lein trampoline run" to get around the subprocess issues with stdin.

-Phil

Tim Cross

unread,
Mar 3, 2012, 5:56:53 PM3/3/12
to clo...@googlegroups.com
Hi Phil,

thanks, lein trampoline run seems to get around the problem. 

BTW, spotted a minor error in the .lein/bin/swank-clojure script. The CLASSPATH setting at the top of the script appears to have a hard coded path instead of one based on $HOME. For example, after installing the swank plugin, my script has the path 

CLASSPATH="/home/phil/.m2/repository/swank-clojure/swank-clojure/1.4.0/swank-clojure-1.4.0.jar"

instead of CLASSPATH="/home/tcross/.....

Tim
Reply all
Reply to author
Forward
0 new messages