Hi,

You can write some lines of Clojure code in a file, and then execute
it with the command 'clj <your file>'. I'm sitting on a linux box
where the script to start Clojure is called 'clj', but if you don't
have such or simply downloaded the zip from clojure.org, you can use
this line to execute a script called test.clj:

java -cp <path/to/clojure.jar> clojure.main test.clj

Which on my linux system would be

java -cp /usr/share/clojure/clojure.jar clojure.main test.clj

The '-cp' after java means "add this file to the classpath", and
basically tells java where to find libraries and classes you use in
your code. Don't worry about it if you use a build tool like leiningen
(see end of this post).

Now for a Clojure script that simply calculates the 5th fibonacci
number and prints it:

(defn fib [n]
  (if (< n 2) 1
      (+ (fib (- n 2))
          (fib (- n 1)))))

(println (fib 5))

You can simply copy that to a file test.clj and use the above command
to run it - should produce 8 as output. This procedure should work
with every script you throw after clojure.

For larger (non-script) projects I would recommend using the
'leiningen' build tool (https://github.com/technomancy/leiningen), the
README should explain how to use it. Basically it can handle your
project dependencies for you, provide a way to package your
application in a JAR file for execution with java, and many other nice
features.

I hope this helps,
  /Matthias

On Mar 23, 8:50 am, ultranewb <pineapple.l...@yahoo.com> wrote:
> Short version:  How do I just open an editor, type in some Clojure
> code, save it in a file, and then run it?
>
> Long version:  Okay, I'm very new to Clojure.  But I'm not a Java
> programmer (don't want to be).  I'm not used to all this complexity
> just to do something simple.  What I want to do is the "normal
> programming" that I do with every other environment and language I
> work with, i.e. I edit some source code on screen, save it in a file,
> and either compile/run, or interpret it or whatever.  But I haven't
> figured out how to do that yet, and don't know if it's possible.
>
> I downloaded Netbeans and Enclojure.  It runs fine.  I can get a REPL,
> blah blah.  But have no idea how to do anything "real" i.e. execute a
> program saved in a file.  Again, I want to edit some code with the
> very nice editor, save it, and hit some button that says "execute" or
> perhaps "compile and execute" or perhaps "build and execute" or
> whatever.  But apparently there is a heck of a lot more to it than
> that.  I understand that you have to build a "project" or whatever.
> Fine - I did that.  Still, I have no idea which directory out of that
> huge structure I'm supposed to put code in, I have no idea how to set
> up all these "dependencies" or whatever.  I did try some random stuff,
> i.e. saving a file in various directories and hitting "build" but that
> didn't work.  I also tried editing various files that were already
> there, hoping one of them was the "main" file I was supposed to be
> dumping source code into, but that didn't work either.
>
> So I downloaded Clojure Box.  It installs and runs fine.  Again, I get
> a REPL no problem.  But there's only so much coding I can do in a
> REPL.  Again, I'd like to do more.
>
> I spent a long time trying to find some help online (googling, etc),
> but everything I've found assumes I know too much, i.e. how to set up
> all these projects and dependencies.
>
> Actually, I'm not interested in fooling with all the boilerplate and
> crap AT ALL.  So if I HAVE to do that, I'm outta here.  But something
> tells me I may not have to, i.e. there may be some automated tool
> somewhere, or some "template" files I can just use over and over, or
> some "trick" to use like "just name your program 'main' and stick it
> in such-and-such directory."
>
> Any help?

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

Reply via email to