user9024779
user9024779

Reputation: 97

Exception while compiling scala code from Java program

I have the following code to compile scala code at runtime in a Java program

    Settings s = new Settings();
    Global g = new Global(s);
    Global.Run run = g.new Run();
    List<String> files = new LinkedList<>();

    files.add("src/main/java/scala/rules/ScalaRuleBasedStrategy.scala");

    run.compile(JavaConverters.asScalaBufferConverter(files)
     .asScala().toList());

But I am getting this error:

Exception in thread "main" java.lang.NoSuchMethodError: scala.tools.nsc.Global$gen$.mkBlock(Lscala/collection/immutable/List;)Lscala/reflect/internal/Trees$Tree; at scala.tools.nsc.ast.parser.TreeBuilder.makeBlock(TreeBuilder.scala:110) at scala.tools.nsc.ast.parser.Parsers$Parser.block(Parsers.scala:1689)

Upvotes: 0

Views: 404

Answers (1)

Dmytro Mitin
Dmytro Mitin

Reputation: 51658

Try

Settings s = new Settings();
MutableSettings.BooleanSetting usejavacp = s.usejavacp(); // added
usejavacp.value_$eq(true); // added
Global g = new Global(s);
Global.Run run = g.new Run();
List<String> files = new LinkedList<>();

files.add("src/main/java/scala/rules/ScalaRuleBasedStrategy.scala");

run.compile(
  JavaConverters.asScalaBufferConverter(files)
    .asScala().toList()
);

Upvotes: 1

Related Questions