Clojure.org documentation on compilation and gen-class - clojure

I am reading the documention on clojure.org about compilation, the last part gen-class examples. I do the examples and then when trying to run it as java app with: java -cp ./classes:clojure.jar clojure.examples.hello Fred in the terminal i get : Error: Could not find or load main class clojure.examples.hello. Can someone help?
Can someone introduce where to learn about gen-class and :gen-class, i find not much documentation on web

The command java -cp ./classes:clojure.jar tst.core from your base+system+user+dave is almost correct. The java.lang.NoClassDefFoundError: clojure/lang/IFn error is because the JVM cannot find the Clojure classes as there is no clojure.jar file in the base+system+user+dave directory, so you need to specify the correct path for the clojure.jar file.
As you are using lein, it downloads your project dependencies to your local repository. One of the dependencies will be Clojure itself, so assuming you are on iOS/Linux and that your lein project.clj has a dependency with clojure 1.7.0, the command to run from the base+system+user+dave directory will be:
java -cp ./classes:~/.m2/repository/org/clojure/clojure/1.7.0/clojure-1.7.0.jar tst.core
As this gets quite annoying once you have more than one dependency, I would suggest to use lein uberjar that will create a file in the target directory called your-project-name-standalone.jar that will have all required classes, so to run it from the command line go to the target directory and run something like :
java -cp tst-standalone.jar tst.core
To understand more about how the classpath works in the JVM, you can start with Wikipedia

Related

Clojure-koans run error:

I'm trying to learn Clojure by Clojure-koans; however, Clojure-koans could not run:
~/Downloads/clojure-koans-2012-10-18_18-42/script $> ./run
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel
Error: Could not find or load main class clojure.main
But I can start clojure by lein repl. And by emacs cider-jack-in.
What should I do to get Clojure-koans run?
Edit: Problem solved by edit the dependencies in project.clj in src folder:
(changed to the latest clojure 1.6.0)
:dependencies [[org.clojure/clojure "1.6.0"]
Should I answer my own question? or how could I mark the question as solved?
Have you run lein deps in the project? From the documentation (https://github.com/functional-koans/clojure-koans):
The only things you'll need to run the Clojure Koans are:
JRE 1.5 or higher
clojure-1.5.1.jar
You can use Leiningen to automatically install the Clojure jar in the right place. Leiningen will also get you a couple more jarfiles, including JLine, which allows you some of the functionality of readline (command-line history, for example).
I suggest either cd .. && ./script/run (I'm guessing the shell script may assume being run one directory up) or cloning the clojure-koans repo and running lein koan run (also from the project root directory).

cannot load a new clojure library

I'm trying out clojure on my second day and I don't understand almost anything yet. I am working with the Programming Clojure 2nd ed. and I am stuck with libraries.
I have Leiningen and have the REPL running. The book first tells the reader to run a simple
(require 'clojure.java.io)
which works just fine (I get a nil). Then it wants to load a file called introduction.clj by running another simple
(require 'examples.introduction)
where I get an error message
FileNotFoundException Could not locate clojure/java/introduction__init.class
or clojure/java/introduction.clj on classpath: clojure.lang.RT.load (RT.java:432)
I downloaded the introduction.clj file and looked where should I place it. The error and the book says the command will search in my classpath, but I have no idea where or what that is (after 1h of searching and reading I still don't get it, sorry). I ran a few commands and I had many classpaths listed (from which none contain a clojure/java/io.clj).
So I tried another approach - find the io.clj file on my disk and simply copy the file there and run it with a command
(require 'clojure.java.introduction)
This doesn't seem to work either. By the way, the io.clj file I found was in "C:\Program Files\clojure\src\clj\clojure\java". I tried running several other .clj files from the java folder as well from the clojure folder, like javadoc.clj or inspector.clj and all seem to work just fine with the above mentioned command. Only the new file doesn't seem to load this way.
Any help appreciated :)
Clojure runs on the Java Virtual Machine, so you will need to learn a bit about PATH and CLASSPATH concepts:
See: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
Regarding the error message, the Clojure runtime is expecting to find introduction.clj in the directory clojure\java\example\introduction.clj (not where it really should be - see below).
The convention for Clojure namespaces is that the last component is the file name, while any previous components are parent directories. So
clojure.java.introduction
would have to be in the directory (relative to your source "root" or classpath)
clojure\java\introduction.clj
(The lein REPL automatically adds your source root to the classpath).
Another concept you need to understand is where the "root" of your source code is located. For Leiningen (the build tool you are using) the default is either "src" or "src/main/clojure" - as documented in the Leiningen sample project file on GitHub).
Finally, if you get really stuck, it seems the complete project for the book is available on GitHub.
Looking at the project, I see that you should actually be placing the file under src\examples\introduction.clj
Are you reading the book "Programming Clojure"?
I have encountered the same problem. It ban be sovled as follows:
If you start clojure by java:
I work in windows, the clojure.jar is placed in D:\backup\clojure-1.5.1, and the source code of the book "Programming Clojure" is placed in D:\study\clojure\shcloj-code\code. You should first delete the user.clj file in folder D:\study\clojure\shcloj-code\code.
java -cp d:\backup\clojure-1.5.1\clojure-1.5.1.jar;d:\study\clojure\shcloj-code\code clojure.main -r
If you work in linux, replace the ";" with ":"
If you start clojure by lein
You should first cd to the D:\study\clojure\shcloj-code\code folder, and then
lein repl
You should also delete the user.clj file in folder D:\study\clojure\shcloj-code\code.

Setting CLASSPATH for Clojure project

I have a plain project structure:
Base Dir
src ;; Pile of Clojure files
lib ;; Jar files
To export the classpath:
$ export CLASSPATH=$CLASSPATH:src:lib/*
Trying to run a Clojure file:
java -cp $CLASSPATH -jar lib/clojure.jar src/wizard-game.clj
But I got:
Exception in thread "main" java.io.FileNotFoundException: Could not locate clojure/contrib/trace_init.class or clojure/contrib/trace.clj on classpath:
Caused by: java.io.FileNotFoundException: Could not locate clojure/contrib/trace_init.class or clojure/contrib/trace.clj on classpath:
Ok, this is a classpath issue but what/where I'm doing wrong?
Is there a better way to try to run it?
UPDATE:
I tried this command:
java -classpath $CLASSPATH clojure.main src/wizard-game.clj
It runs ok now.
From the java man pages regarding the -jar option:
When you use this option, the JAR file
is the source of all user classes, and
other user class path settings are
ignored.
So that's a bit of a bummer, but the good news is that you can get around this by using a different launching syntax (referenced at clojure.org):
java -cp $CLASSPATH clojure.main src/wizard-game.clj
Alternatively, use a tool like Leiningen to manage your project's classpath and dependencies for you!
This is a response to your "How to run a standalone Clojure file in Lein?" you should look at into lein run. I'm not sure the current state but there was a standalone Lein plugin and now there is at least some (maybe all) of the functionality build into lein by default.
Try running a lein help run at the command line for a quick introduction.
Standalone lein-run project. Documentation may be useful. Not 100% sure if it matches up with the built-in lein run but I know from my own usage at least some of it does.
https://github.com/sids/lein-run

clojure/lein REPL with jline

For some reason I can't get clojure REPL working with jline, what i did was git clone the clojure repository off github then run ant to build it, then i download jline-0.9.94.jar to the directory with clojure.jar, then run the following command:
java -cp jline-0.9.94.jar:clojure.jar jline.ConsoleRunner clojure.main
And get the following errors:
Exception in thread "main" java.lang.ClassNotFoundException: clojure.main
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:317)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:375)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at jline.ConsoleRunner.main(Unknown Source)
Here are the files in my current directory:
vvshs-macbook-2:clojure vvsh$ ls
build.xml clojure-sources-1.2.0-master-SNAPSHOT.jar epl-v10.html src
classes clojure-sources.jar jline-0.9.94.jar test
clojure-1.2.0-master-SNAPSHOT.jar clojure.iml pom-template.xml
clojure-slim-1.2.0-master-SNAPSHOT.jar clojure.jar pom.xml
clojure-slim.jar doc readme.txt
vvshs-macbook-2:clojure vvsh$
I got the same error on clojure 1.1 and lein repl(it seems lein maintain its own version of clojure).
By the way, this is on mac ox 10.5.8
java version "1.5.0_24"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_24-b02-357-9M3165)
Java HotSpot(TM) Client VM (build 1.5.0_24-149, mixed mode, sharing)
Anybody know what's wrong and how to fix it? As I really want to get lein repl working to start a project.
I solved the same problem today by removing a redundant jline*.jar from /Library/Java/Extensions, leaving just one jline installation in CLASSPATH.
Longer explanation: I was trying to build labrepl, which installs all its dependencies in subdirectory lib, but I had previously set up jline by copying the .jar file to /Library/Java/Extensions. Apparently, java.lang.ClassLoader couldn't handle two installations of jline, and as long as there were ones available in two places, the command line starting a Clojure REPL would fail finding the latter one of classes jline.ConsoleRunner and clojure.main, depending on the order they were given to the java command.
I hope this helps.
see my alternative instructions for install on my fork:
http://github.com/jedschneider/leiningen
I sent a pull request to update the readme, but didn't get a response on it. once you have lein installed, you can put the bin/leiningen.sh in your ~/bin and call it anywhere. i put a shortcut on my .bash_profile
alias lein="~/bin/leiningen.sh"
and then call lein repl to launch a shell
also checkout the labrepl which is a great learning tool.
http://github.com/relevance/labrepl
Your java ... command looks fine, I'd double check if all the jars are where you think they should be (and that they are the jars you think they are, i.e. clojure.jar is actually the one you built locally and not some super-outdated one).
If you want to use Leiningen for lein repl, that's possible too. Note that it isn't accurate to say that it "maintains its own version of Clojure"; Leiningen is a build tool, used as part of a project-oriented workflow, and the separate Clojure jars are project-specific. To create a sample project to play around in, say lein new foo someplace convenient. Then you'll need to cd foo, get your Clojure & contrib jars with lein deps (the project.clj skeleton provided by lein new already contains entries for both) and start a REPL with lein repl.
Finally, you could use the new cljr project; it has a comprehensive README which should get you started in no time, and with it you'll be able to say cljr repl to get a Clojure REPL anywhere, without the need to create a "project".
I met the same problem. jline.ConsoleRunner (at least about jline-0.9.5 which I use) seems not to be able to find class files in the path added to class search paths from -cp option of 'java' command. It means it can't find neither classes in your current directory. I solved this by copying clojure.main under /Library/Java/Extensions/ too.

Clojure load files

I'm trying to set up a simple clojure project, and I'm not sure how to load files between the project. I'm sure that the answer is in the documentation, but I can't find a simple answer any where and I'm not sure where to look.
Essentially, my directory looks like this:
Clojure/
clojure/
clojure.jar
other clojure files
clojure-contrib/
clojure-contrib.jar
other contrib files
project/
main.clj
utils.clj
And I want main.clj to be something like this:
(ns project.main
(:require project.utils))
(greet)
and utils.clj to be something like this:
(ns project.utils)
(defn greet [] (println "Hello, World!"))
But that fails with:
Exception in thread "main" java.io.FileNotFoundException: Could not locate project/utils__init.class or project/utils.clj on classpath: (main.clj:1)
When I attempt to run it. My classpath includes the top Clojure/ directory and both jars. I also tried putting the project/ directory in the classpath as well, with no luck.
How do you set up a simple clojure project?
You don't mention what your environment is (i.e. Emacs/SLIME/Swank, vim/Vimclojure), so I'm going to assume you are trying to invoke it from the command line.
You need to have your Clojure/ project directory in the classpath:
java -cp path/to/clojure.jar:path/to/clojure-contrib.jar:path/to/Clojure ...
Make sure to check that paths are correct relative to current working directory. It needs to point to the root of your namespace (i.e. if running in Clojure/, the path is .).
In fact, your project layout Works On My Machine(tm), with the exception that I have use instead of require (but you should've got a different error anyway if you got to the point when Clojure could find all your files).
This answer I posted to another question should hopefully give you an idea of how your filenames should relate to namespace names for things to work. However, since your question is "how to set up a simple Clojure project", the following is a better start:
Go to GitHub and grab Leiningen.
Follow the instructions in the README. You'll end up doing something like
$ lein new my-project
$ cd my-project
# ... edit project.clj ...
$ lein deps
Hack away! You'll need to put your files in the correct places. That will mean putting your source files in the directory tree rooted at my-project/src, with your core namespace most likely residing at my-project/src/my_project/core.clj. But really, I've explained all the details in the answer linked to above, so please read it (and do leave a comment if I missed something). :-)
Leiningen will take care of the basic project layout and setting up the classpath for a REPL / swank / nailgun for you (if you haven't yet come across the latter two, you will soon -- but that's a separate topic, the swank part of which I have covered to a certain degree e.g. in this SO answer), so hopefully you'll never need to deal with the java -cp ... nonsense by hand. (The swank-related answer I linked to in the last parenthetical remark has details on how to set up swank with the correct classpath from within Emacs.)