从Java程序编译Scala代码时发生异常

时间:2018-08-06 10:16:11

标签: java scala scala-compiler

我有以下代码在运行时在Java程序中编译scala代码

    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());

但是我收到此错误:

线程“主”中的异常java.lang.NoSuchMethodError:scala.tools.nsc.Global $ gen $ .mkBlock(Lscala / collection / immutable / List;)Lscala / reflect / internal / Trees $ Tree;     在scala.tools.nsc.ast.parser.TreeBuilder.makeBlock(TreeBuilder.scala:110)     在scala.tools.nsc.ast.parser.Parsers $ Parser.block(Parsers.scala:1689)

1 个答案:

答案 0 :(得分:1)

尝试

    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());