Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Java Classes in Clojure

Tags:

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?

like image 725
Timothy Baldridge Avatar asked Jun 24 '10 15:06

Timothy Baldridge


2 Answers

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.

like image 185
Brian Carper Avatar answered Sep 29 '22 23:09

Brian Carper


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))) 
like image 29
Bozhidar Batsov Avatar answered Sep 29 '22 23:09

Bozhidar Batsov