Clojure-koans run error: - clojure

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

Related

Could not locate clojure/data/json: How do I get my REPL to see this (and similar) dependencies

I am using lein repl without a project so there is no project.clj.
I am running Leiningen 2.8.1 on Java 1.8.0_191 OpenJDK 64-Bit Server VM.
When I require a Clojure dependency that I assume should just work - like clojure.data.json - I notice that it is not in my .m2 directory. Is that why I am getting a FileNotFoundException Could not locate clojure/data/json__init.class or clojure/data/js
on.clj on classpath? I can't find my other Clojure dependencies there either so I don't know where they reside and if this dependancy should be in .m2 or not.
I understand the error message but without knowing its location or even knowing how to properly add it to the CLASSPATH for the REPL to see it, I remain stuck.
Is this a dependency that I still need to install? If so, how do I install it without going through a project?
I don't understand the JVM as I am new to it, so add a little extra information in your answer.
I have looked at this, this, this, this and this. I don't know if I am overlooking anything so your help will really be appreciated.
I am using lein run without a project so there is no project.clj.
If you're using Leiningen, this'll be much easier if you create a project.clj file that declares your dependencies. Leiningen will read project.clj and handle fetching any missing dependencies to your local Maven repository, and add them to your classpath when you start your REPL/application. (lein run doesn't work for me in a directory without a project.clj; I get an error: No :main namespace specified in project.clj.. Did you mean lein repl?)
When I require a Clojure dependency that I assume should just work - like clojure.data.json - I notice that it is not in my .m2 directory.
clojure.data.json doesn't ship with Clojure — it's a separate dependency that must be fetched and added to your classpath in order to use it. The classpath tells the JVM where to look when it loads class files. Leiningen will do both of these things for you if you declare the dependency in project.clj:
:dependencies [[org.clojure/clojure "1.10.0"]
[org.clojure/data.json "0.2.6"]]
You can also use the lein deps command if you only want to fetch dependencies.
You can create a new/blank Leiningen project with lein new project_name_goes_here. It will have a project.clj with a few boilerplate entries and a :dependencies key where you can declare dependencies.
I understand the error message but without knowing its location or even knowing how to properly add it to the CLASSPATH for the REPL to see it, I remain stuck. Is this a dependency that I still need to install? If so, how do I install it without going through a project?
You could manually download it from the internet, then manually add its path to your classpath, but if you're already using Leiningen it's much easier to add a line to a project.clj file and have Leiningen handle this for you.
If using a project.clj file w/Leiningen isn't an option, there are other ways to use Clojure and resolve dependencies/build a classpath at runtime. Boot accommodates this workflow, you can use Leiningen like this with a little added effort, as well as the newer tools.deps tooling. There are examples of each in this ClojureVerse thread, but note that some of these approaches are doing essentially the same thing as declaring the dependency in a file — instead declaring them as CLI arguments.
For example, using Clojure CLI tooling:
$ clj -Sdeps "{:deps {org.clojure/data.json {:mvn/version \"0.2.6\"}}}"
Clojure 1.9.0
user=> (require '[clojure.data.json :as json])
nil
user=> (json/write-str {:foo "bar"})
"{\"foo\":\"bar\"}"
user=> (System/getProperty "java.class.path")
"src:
/Users/me/.m2/repository/org/clojure/clojure/1.9.0/clojure-1.9.0.jar:
/Users/me/.m2/repository/org/clojure/data.json/0.2.6/data.json-0.2.6.jar:
/Users/me/.m2/repository/org/clojure/spec.alpha/0.1.143/spec.alpha-0.1.143.jar:
/Users/me/.m2/repository/org/clojure/core.specs.alpha/0.1.24/core.specs.alpha-0.1.24.jar"
You could create a deps.edn file containing {:deps {org.clojure/data.json {:mvn/version \"0.2.6\"}}} in the same directory, and clj would read that, resolve the dependencies if necessary, and build the classpath accordingly.
This is a great opportunity to use lein try. Once you add it to your ~/.lein/profiles.clj, you'd simply run: lein try org.clojure/data.json and you'll be greeted with a running REPL with that dependency just a require away.

How do I use the Clojure `use` function in leiningen?

I am very new to both Clojure and Leiningen. I have installed Clojure on Windows at C:\clojure-1.5.1 and leiningen-win-installer beta1 at C:\.lein\bin.
Now I am trying the example from Eric Rochester's book. I have included dependent libraries for Incanter in project.clj and also using dep.
How do I use the command (use 'incanter.core)? I am getting 'use' is not recognized as an internal or external command. In addition, how do I use lein commands at user=>?
Edit - I forgot "lein deps" until I saw Mars's answer
Before you start lein repl, you have to make the project (with "lein new getting-data" or whatever is in the book).
That makes a new directory, and in that directory you find and edit "project.clj" to include the dependencies (as shown in book).
cd into the directory that project.clj is in and run "lein deps" from the dos/powershell prompt.
THEN when you run lein repl, from within that same directory, at the user=> prompt, do
(use 'incanter.core)
and it will come back with "nil" and you'll be running. You might want to first run some examples from the leiningen page to get more of a feel for lein. You always type clojure commands at the "user=>" prompt, not at the "c:....>" prompt.
There's a bit of subtlety in Leiningen project.clj's. I haven't found an easy introduction. Levin Magruder's advice will no doubt get you started. The basic idea is that once the project file is set up correctly, lein deps will go out and find the libraries you need, and put them in a place where lein repl can find them. Then use will work for the libraries that have been downloaded. For more info, study of the detailed comments project.clj sample file may be helpful. (Not part of the answer to this question, but if you're having problems with use, you're likely to get tripped up by ns and filenames soon (I was): I recommend Colin Jones' introduction to ns and its options.)

Using lwjgl in Leiningen/Clojure

Solution
(1) (println (. System getProperty "java.library.path"))
This gives me a list of places java looks for native extensions.
Then, I took the lwjgl native extensions, and put them there.
Things that didn't work for me (probably because I used them incorrectly)
(*) setting :native-path
(*) setting :native-dependencies
Problem
My setup:
(lein deps; echo "====="; cat project.clj; echo "====="; cat src/main.clj; echo "====="; lein repl) &> log
contents of "log"
Copying 10 files to /Volumes/ramdisk/fail/lib
=====
(defproject
mincase "0.0.1"
:dependencies [[org.clojure/clojure "1.4.0"]
[org.lwjgl.lwjgl/lwjgl "2.8.2"] ]
:repositories {"local" "/Users/x/z/maven_repo"}
:jvm-opts ["-Xms4g" "-Xmx4g"]
:repl-init main
)
=====
(ns main
(:import org.lwjgl.opengl.Display))
=====
REPL started; server listening on localhost port 31235
UnsatisfiedLinkError no lwjgl in java.library.path java.lang.ClassLoader.loadLibrary (ClassLoader.java:1860)
clojure.core=>
Note -- I had already done a "lein deps", so the lwjgl library has been pulled into maven. What I don't understand are:
(*) how do I get access to lwjgl from Clojure?
(*) more importantly, how do I debug which step this whole thing has gone wrong at?
$ find lib
lib
lib/clojure-1.4.0.jar
lib/jinput-2.0.5.jar
lib/jinput-platform-2.0.5-natives-linux.jar
lib/jinput-platform-2.0.5-natives-osx.jar
lib/jinput-platform-2.0.5-natives-windows.jar
lib/jutils-1.0.0.jar
lib/lwjgl-2.8.2.jar
lib/lwjgl-platform-2.8.2-natives-linux.jar
lib/lwjgl-platform-2.8.2-natives-osx.jar
lib/lwjgl-platform-2.8.2-natives-windows.jar
So it appears that lwjgl has been pulled in.
What are the steps I should try to figure out which step I went wrong on?
Thanks!
Dropping this note here since google found this post for my similar question.
The Leiningen folks have now addressed this: https://github.com/technomancy/leiningen/issues/898
If you get version 2.1.0 or later, it has the fix. See the bug for details.
UPDATE: (Aug 2013)
I have a project on github I use for experimentation with lwjgl here: https://github.com/rogerallen/hello_lwjgl
I'm also using LWJGL in my shadertone project here: https://github.com/overtone/shadertone
Because Shadertone is a library, I found I needed to package up the natives myself to get it to install reasonably for projects that depend on shadertone.
If anyone has some pull with the LWJGL folks, it sure would be nice if they just put natives into appropriate subdirectories as lein expects in their clojars releases.
Looks like a problem with your LD_LIBRARY_PATH. Are you including the correct .dll or .so files?
You probably need to add something like :native-dependencies [[lwjgl "2.8.2"]] to your project.clj.
Alternatively, you could try setting the right value from your shell:
export LD_LIBRARY_PATH=/home/username/lwjgl-2.8.2/native/linux/
It's a bit confusing why Display is refusing to import, though other classes in the same jar file import properly
(import '[org.lwjgl.opengl Util WindowsAWTGLCanvasPeerInfo])
I suspect that this jar file is broken, perhaps you could try a different version.
I tried debuggin this by running
cd lib
jar xf lwjgl-2.8.2.jar
cd org/lwjgl/opengl/
and then trying to load various classes i see there.
lein swank also does tab completion which can help in exploring classes without extracting them from the shell.
Ran into this today, solved it a bit differently by adding the native directory to :jvm-opts in project.clj:
(defproject projectname "0.0.1-SNAPSHOT"
:description "my project"
:jvm-opts ["-Djava.library.path=native/linux"]
:dependencies [[org.clojure/clojure "1.4.0"]])
I copied the jar files from the latest lwjgl release into lib and copied the native directory into the project root. Seems to work so far:
user=> (import org.lwjgl.opengl.Display)
org.lwjgl.opengl.Display
But I am only just getting started. Anyway, hope this helps someone else :)

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.