2
String printcode = "(print "foo")";

If I have the above String in java, is there anyway to execute it by clojure inside the Java ?

6
  • 1
    What does it mean to "have a string in Java?" Can you be a bit more explicit? Commented Jul 23, 2018 at 15:52
  • @markspace just a simple String as my example. having the clojure code as a String Commented Jul 23, 2018 at 15:52
  • Someone who knows Clojure might understand, but I don't think Java programmers will. You're literally asking how to execute the above single statement in Clojure? Commented Jul 23, 2018 at 15:54
  • why not just use System.out.print(foo);? Commented Jul 23, 2018 at 16:22
  • Possible duplicate of Calling clojure from java Commented Jul 27, 2018 at 11:05

1 Answer 1

5

The following should work (assuming that you have properly set up dependencies and initialised the Clojure runtime):

import clojure.lang.RT;
import clojure.lang.Compiler;

...

Compiler.eval(RT.readString("(print \"foo\")")));
Sign up to request clarification or add additional context in comments.

Comments

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.