Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Java classes to Clojure

Trying to import a class outside the java-library with no result. I'm runnig counterclockwise on Eclipse Helios. The commons-land-2.6.jar is in the buildpath. I'm new to Clojure and can't figure this out. All help greatly appreciated!

Naturally this works fine:

1:7 exp2=> (import '(java.io FileReader))

> java.io.FileReader

but this doesn't:

1:6 exp2=> (import '(org.apache.commons.lang.StringUtils))

> nil

This is the ultimate goal:

1:10 exp2=> (defn whitespace? [character] (. StringUtils (isEmpty character )))

> java.lang.Exception: Unable to resolve symbol: StringUtils in this context (repl-1:10)

like image 440
user594883 Avatar asked Jan 29 '11 11:01

user594883


People also ask

Can clojure use Java library?

Clojure programs get compiled to Java bytecode and executed within a JVM process. Clojure programs also have access to Java libraries, and you can easily interact with them using Clojure's interop facilities.

Is clojure interoperable with Java?

Overview. Clojure was designed to be a hosted language that directly interoperates with its host platform (JVM, CLR and so on). Clojure code is compiled to JVM bytecode. For method calls on Java objects, Clojure compiler will try to emit the same bytecode javac would produce.

What does Gen class do in Clojure?

The generated class automatically defines all of the non-private methods of its superclasses/interfaces. This parameter can be used to specify the signatures of additional methods of the generated class.

What is Clojure namespace?

A namespace is both a name context and a container for vars. Namespace names are symbols where periods are used to separate namespace parts, such as clojure. string . By convention, namespace names are typically lower-case and use - to separate words, although this is not required.


1 Answers

You made one error - you didn't put space between org.apache.commons.lang and StringUtils class. This form of import allows you to import several classes from one package, for example:

(import '(org.apache.commons.lang StringUtils SystemUtils))

if you want to import only one class, then you can use version without parentheses:

(import 'org.apache.commons.lang.StringUtils)

And because functions in StringUtils are static, you need to use following code:

(StringUtils/isEmpty character)

to invoke their functions

like image 78
Alex Ott Avatar answered Oct 05 '22 23:10

Alex Ott