Import Java classes in Clojure

I seem to be doing something wrong. I built clojure from git and call it this way:

java -cp clojure.jar clojure.main 

I get repl and then I type:

 (import 'java.lang.string) 

and I get:

 java.lang.ClassNotFoundException: java.lang.string (NO_SOURCE_FILE:1) 

I am trying to use this with lang.string, since I am assuming that it should exist in the classpath somewhere. I tried other libraries, all without much luck. What am I doing wrong?

+7
clojure
source share
3 answers

String should be uppercase, that's all.

 user> (import 'java.lang.String) java.lang.String 

But everything in java.lang already imported and available by default, so you do not need to do this.

+14
source share

Btw in non-responsive exercises is probably the best way to enable Java classes is with 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))) 
+8
source share

Blech, I think I found it. First of all, the syntax should be:

 (import java.lang.String) 

Also note that String is not a string.

+2
source share

All Articles