Importing Java Classes in Clojure
I seem to be doing something wrong. I've built clojure from git, and am invoking it thus:
java -cp clojure.jar clojure.main
I 开发者_如何学编程get the repl, and then I type:
(import 'java.lang.string)
and I get:
java.lang.ClassNotFoundException: java.lang.string (NO_SOURCE_FILE:1)
I'm trying this with lang.string since I assume it has to exist on the classpath somewhere. I've tried other libraries, all without much luck. What am I doing wrong?
String
should be capitalized, that's all.
user> (import 'java.lang.String)
java.lang.String
But everything in java.lang
is already imported and available by default, so you shouldn't need to do this.
Btw in non repl exercises probably the best way to include Java classes is the ns macro.
(ns foo.bar
(:refer-clojure :exclude [ancestors printf])
(:require (clojure.contrib sql sql.tests))
(:use (my.lib this that))
(:import (java.util Date Timer Random)
(java.sql Connection Statement)))
Bleh, I think I found it. First of all the syntax should be:
(import java.lang.String)
Also notice it's String not string.
精彩评论