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?
asked Sep 14, 2014 at 17:04
Zuriar
11.9k23 gold badges64 silver badges98 bronze badges
1 Answer 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)
answered Sep 14, 2014 at 17:17
noisesmith
20.2k2 gold badges43 silver badges50 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Zuriar
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?
Zuriar
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)
Zuriar
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 { ....
lang-clj