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

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

Tiara
  • 59
  • 6
  • 1
    What does it mean to "have a string in Java?" Can you be a bit more explicit? – markspace Jul 23 '18 at 15:52
  • @markspace just a simple String as my example. having the clojure code as a String – Tiara Jul 23 '18 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? – markspace Jul 23 '18 at 15:54
  • why not just use `System.out.print(foo);`? – nha Jul 23 '18 at 16:22
  • Possible duplicate of [Calling clojure from java](https://stackoverflow.com/questions/2181774/calling-clojure-from-java) – nha Jul 27 '18 at 11:05
  • i wrote that calling clojure form java question and that other question is asking the opposite of this one. – Arthur Ulfeldt Jul 27 '18 at 19:00

1 Answers1

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\")")));
Aleph Aleph
  • 5,215
  • 2
  • 13
  • 28