Importing clojure functions from jars - clojure

I'm playing around with Clojure, and I can't figure out how to import a function from clojure-contrib.jar. Working from this answer, I'm doing the following:
Running the REPL like so:
java -cp clojure.jar:clojure-contrib.jar clojure.main
Then trying to import a function:
user=> (use '[clojure-contrib.duck-streams :only (writer reader)])
It doesn't work, and I get the following error:
java.io.FileNotFoundException: Could not locate clojure_contrib/duck_streams__init.class or clojure_contrib/duck_streams.clj on classpath: (NO_SOURCE_FILE:0)
Trying it with a dot instead of a dash also doesn't work:
user=> (use '[clojure.contrib.duck-streams :only (writer reader)])
I get mostly the same error:
java.io.FileNotFoundException: Could not locate clojure/contrib/duck_streams__init.class or clojure/contrib/duck_streams.clj on classpath: (NO_SOURCE_FILE:0)
What am I doing wrong?

Is clojure.jar and clojure-contrib.jar in your current working directory? If not, you need to specify the full path to the JAR files in the CLASSPATH.

This should work
(use 'clojure.contrib)
I don't have clojure handy right now to check, but
(use 'clojure.contrib :only (writer reader))
should also work

It's clojure.contrib, not clojute-contrib. Note dot versus dash.

Related

how to get meaningful dependency errors in clojure?

If I add the following require:
(ns project.core
(:require
[compojure.route :as route])
(:gen-class))
(defn -main [& {:as args}]
(println "hello"))
an do
lein run
I get
Syntax error macroexpanding clojure.core/ns at (ring/util/mime_type.clj:1:1).
((require [clojure.string :as str])) - failed: Extra input spec: :clojure.core.specs.alpha/ns-form
is there a way I can get
"compojure.route not found; not defined; or whatever" e.g. something meaningful?
if I remove it it works fine. e.g. it says Hello
using Leiningen 2.9.4 on Java 14.0.1 OpenJDK 64-Bit Server VM
The project you are using is using very old dependencies. Clojure spec (introduced "recenlty") added macro/compile time checks and the mentioned file there triggers it.
compojure.route actually is found and it then requires its transitive deps. And while going down the chain ring.util.mime-type is required and the version you are using is not ready for current Clojure versions.
Your best bet here is to upgrade your deps. E.g. if you are following a book or if you are using a template this things can happen. If you have lein-ancient installed, it can attempt the update for you. Otherwise your best bet is to start in your project.clj and compare versions (e.g. check clojars).
If the problem still persists, have a look at lein deps :tree and see what is going on with the transtive deps.

Spyscope Java RuntimeException No reader function for tag spy/p

I'm trying out spyscope, and following the documented example, I'm getting:
user=> (take 20 (repeat #spy/d (+ 1 2 3)))
RuntimeException No reader function for tag spy/d clojure.lang.LispReader$CtorReader.readTagged (LispReader.java:1245)
RuntimeException Unmatched delimiter: ) clojure.lang.Util.runtimeException (Util.java:221)
RuntimeException Unmatched delimiter: ) clojure.lang.Util.runtimeException (Util.java:221)
My ~/.lein/profile.clj is:
{:user {:dependencies [[spyscope "0.1.6"]]
:injections [(require 'spyscope.core)
(use 'clojure.tools.trace)]
:plugins [[lein-try "0.4.3"]]
}
}
My version of Leiningen is:
$ lein --version
Leiningen 2.8.1 on Java 1.8.0_212 OpenJDK 64-Bit Server VM
I can't seem to find any answers on how to resolve this.
Answering my own question to make it easier for the next person to looking for the solution.
The answer can be traced to [this issue]: (https://github.com/dgrnbrg/spyscope/issues/8)
Lein 2 allows users to fire lein repl in a non project directory and that's the case
You have to use spyscope within a directory with a project.clj file.
You can use spyscope this way at your profiles.clj
; try/catch to workaround an issue where `lein repl` outside a project dir
; will not load reader literal definitions correctly:
(try (require 'spyscope.core)
(catch RuntimeException e))

Missing doc in non-user namespaces in Clojure 1.5

What should I do to get doc available again?
brew install --devel leiningen
lein new app yeehaw
cd yeehaw
; edit project.clj and replace "1.4.0" with 1.5.0-RC1"
lein repl
yeehaw.core=> (doc map)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: doc
in this context, compiling:(NO_SOURCE_PATH:1:1)
This change comes from CLJ-1085 and this associated source.
Note that doc and the usual REPL functionality is still available in the user namespace, but it would be nice to have it at my fingertips in my app's namespace, at least while in the REPL.
Get doc into current namespace:
(clojure.core/use '[clojure.repl :only (doc)])
And after that try doc again.
Thanks to help from clojure IRC, here are some options:
use clojure.repl/doc instead of doc
use (apply require clojure.main/repl-requires) to bring doc and other REPL functionality into a new namespace.

clojure namespaces .core file and repl

A beginners question.
running clojure using lein + emacs + nrepl.
I am slightly confused about the following:
I wish to use the exponent function. This function lives in the following place clojure.math.numeric-tower. I add [org.clojure/math.numeric-tower "0.0.1"] to the dependencies and run lein deps.
Now is it possible (I'm sure it is possible) to add this to my .core ns as follows:
(ns learning.core
(:require [clojure.math.numeric-tower :as math]))
(def i-know-the-answer
(math/expt 2 10))
now when I try to load (ctl-x e) this into the REPL, it throws errors.
clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: No such namespace: math, compiling:(NO_SOURCE_PATH:2)
do the dependencies need to be loaded into the REPL directly? Can I not just change the source file / recompile it and use that?
Load the file with ctrl-c ctrl-l then Switch your repl to the namespace in that file with either
(in-ns 'learning.core)
Or hit ctrl-c alt-n from the Clojure buffer to switch the repl to the buffer's namespace. You can tell if it worked by looking at the prompt in the repl.

clojure.contrib.duck-streams FileNotFoundException, why?

user=> (use '[clojure.contrib.duck-streams])
java.io.FileNotFoundException: Could not locate clojure/contrib/duck_streams__in
it.class or clojure/contrib/duck_streams.clj on classpath: (NO_SOURCE_FILE:0)
Clojure.contrib is in CLASSPATH still it is throwing Exception. Do I need some other Jar?
Use clojure.java.io instead, it's included with 1.2.
I highly recommend using leiningen to sort this out.
try the lein repl command to get a quick repl with a working classpath.
According to this message:
http://osdir.com/ml/clojure/2010-10/msg00834.html
clojure.contrib.duck-streams has been deprecated.