On Jun 18, 6:15 pm, Paul Moore <p.f.mo...@gmail.com> wrote: > I've just seen a couple of postings which, if I'm not mistaken, imply > that it's possible to have a Clojure script in my classspath. Is that > right?
Yes, you can have .clj files on your classpath. In fact, you can pretty much have anything on your classpath. Checkout java.lang.ClassLoader http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String) . This is what Clojure uses under the covers. > Can anyone give me a complete example of how this works? (I probably > need to get the file names and namespaces right, something I'm still > struggling with - again the Java/JVM requirements for filenames that > match classnames is a new concept for me). Let's say I put my clojure.jar on the classpath, but only put the clojure-contrib source files on the classpath and I want to use the clojure.contrib.string/blank? function. Here is a session at the REPL. Note that I used a custom script to start the REPL, it prints out the Java executable and CLASSPATH env variable at the top. rzeze...@chinaski.local [~] clojure >>> JAVA: >>> /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/bin/java >>> CLASSPATH: >>> /Users/rzezeski/projects/clojure/clojure.jar:/Users/rzezeski/projects/clojure-contrib/src/main/clojure Clojure 1.2.0-master-SNAPSHOT user=> (clojure.contrib.string/blank? "") java.lang.ClassNotFoundException: clojure.contrib.string (NO_SOURCE_FILE:0) user=> (load "/clojure/contrib/string") nil user=> (clojure.contrib.string/blank? "") true user=> Notice the load function converted my string into a path to the .clj I wanted. The initial forward-slash means I want to lookup relative to the classpath. The lib I want has the namespace clojure.contrib.string. In this case simply convert the periods to forward-slashes. You don't specify the .clj extension because Clojure will first try to load the lib via class file or source depending on which is newer. For example, if I tried to load the lib foo.bar. user=> (load "/foo/bar") java.io.FileNotFoundException: Could not locate foo/bar__init.class or foo/bar.clj on classpath: (NO_SOURCE_FILE:0) Now that I've shown you the load function I'm going to pull the rug from underneath you and tell you not to use it to load your Clojure libraries. The idiomatic way to load a lib is via the require function, which uses load under the covers. Require allows you to specify your lib in it's native Clojure tongue. user=> (require '[clojure.contrib.repl-utils :as ru]) nil user=> (ru/show ClassLoader) === public abstract java.lang.ClassLoader === [ 0] static getSystemClassLoader : ClassLoader () [ 1] static getSystemResource : URL (String) ... HTH, -Ryan -- 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