缺少依赖项‘object scala.native in compiler mirror’

问题描述 投票:0回答:2

我使用scala-compiler.jar编译了一个嵌入式Scala程序 这个 scala 程序导入一个使用 jni

编写的类
  • 代码如下
class test{
  def test(ctx: ContractContext): ActionResult = {
    val s = new DllTest
    s.loadLibrary()
    print(s.intMethod(5))
    null
  }
}

DllTest 是一个 jni 应用程序

  • 报错如下
error: error while loading Object, Missing dependency 'object scala.native in compiler mirror', required by /modules/java.base/java/lang/Object.class

Failed to initialize compiler: object scala in compiler mirror not found.
** Note that as of 2.8 scala does not assume use of the java classpath.
** For the old behavior pass -usejavacp to scala, or if using a Settings
** object programmatically, settings.usejavacp.value = true.

有人知道原因吗?以及如何解决问题?感谢您对它有所了解

在主函数中运行良好

  def main(args: Array[String]): Unit = {

    //Create system instance
    val s = new DllTest
    s.loadLibrary()
    print(s.intMethod(5))
......
scala java-native-interface scala-compiler runtime-compilation
2个回答

0
投票

感谢您的回答,我已经在我的代码中设置了适当的选项

val classCache = mutable.Map[String, Class[_]]()
private val settings = new Settings()
  settings.deprecation.value = true // enable detailed deprecation warnings
  settings.unchecked.value = true // enable detailed unchecked warnings
  settings.outputDirs.setSingleOutput(target)
  settings.usejavacp.value = true
  settings.classpath.append(getSourcePath())

  private val global = new Global(settings)
  private lazy val run = new global.Run
  val classLoader = new AbstractFileClassLoader(target, this.getClass.getClassLoader)

我的测试环境是上传scala代码并编译运行scala代码的系统。所以我不能提供mcve。我提交了一些在不导入 jni 的情况下运行良好的 scala 程序,但是每当我尝试在我的 scala 代码中包含 jni 代码时,我都会收到一条错误消息,指出找不到 scala.native 这是我的程序

class DllTest {

  @native def intMethod(n: Int): Int

  def loadLibrary(): Unit = {
    System.load("D:\\Idea\\scala_test\\libtest.dll")
  }
}

然后导入到嵌入式scala代码中

import rep.DllTest
  def test(ctx: ContractContext): ActionResult = {
    val s = new DllTest
    s.loadLibrary()
    print(s.intMethod(5))
    null
  }

难道scala-compiler.jar不能编译native methods? 此外,导入的代码在主系统功能中运行良好,但 scala-compiler.jar 抛出错误

© www.soinside.com 2019 - 2024. All rights reserved.