0

I have this simple Java source:

class HelloJava {

   public static String greetMe() {
      return "Hello, this is Java calling!";
   }

}

which I compile down into a class file called HelloJava.class

HelloWorld.class is in the same directory that I launch the Repl from.

How can I now call HelloJava.greetMe() in the Clojure REPL?

1 Answer 1

1

Static methods are accessed via Class/method, and like most things in Clojure, are invoked via wrapping in parens:

(import org.user3231690.HelloJava)
(HelloJava/greetMe)
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, I see. How about if I haven't put the class in a package but have just compiled it into the same directory I launched the Repl from. Shouldn't this also be on the classpath so (import HelloJava) should work?
OK, I figured it out. I have HelloJava.class in the directory I start the Repl from so I need to do: java -cp clojure-1.6.0.jar:. clojure.main to load the Repl with the current directory in the CP. Now I do (import HelloJava) and it imports. However, when I then do (HelloJava/greetMe) I get IllegalAccessError tried to access class HelloJava from class user$eval3 user/eval3 (NO_SOURCE_FILE:2)
And the reason for the above IllegalAccessError is because the original Java code did not declare the class as 'public'. It should be public class HelloJava { ....

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.