0

I have the following Java class:

public class Speak {
   public static String greet() {
     return "Hello! I am a human!";
   }
}

I have compiled this to Speak.class

How do I now import it into the clojure repl and how do classpaths and namespaces fit in?

Thanks

1 Answer 1

2

With the standard clojure repl, the class file needs to be available on the classpath used to start the repl. Here's an example

java -cp pathToClojure\clojure.jar;.\src;.\lib;.\lib\* clojure.main %1

I have the src included for clojure source, the lib folder included for class files, and lib* included for classes included in jar files.

In this example, the Speak class can be included and used with:

user=> (import Speak)
Speak
user=> (Speak/greet)
"Hello! I am a human!"

This assumes that you have set up the classpath before starting the repl. If you want to add something to the classpath after the repl is started it's more complicated. Example code to do it can be found here

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, how does lein repl allow me to add the classpath at start up?
leiningen wants all jar files to come from a standard repository for repeatability. There is a discussion of this at groups.google.com/forum/#!topic/leiningen/DC84wkWDB0c

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.