0

I used scala-compiler.jar to compile an embedded Scala program This scala program imports a class written using jni

  • The code is as follows
class test{
  def test(ctx: ContractContext): ActionResult = {
    val s = new DllTest
    s.loadLibrary()
    print(s.intMethod(5))
    null
  }
}

DllTest is a jni application

  • The error is as follows
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.

Anyone knows the reason? And how to solve the problem? Thanks for shedding some lights on it

It works fine in the main function

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

    //Create system instance
    val s = new DllTest
    s.loadLibrary()
    print(s.intMethod(5))
......
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
tony
  • 21
  • 3
  • 1
    Connected discussion https://users.scala-lang.org/t/missing-dependency-object-scala-native-in-compiler-mirror/9162 – Dmytro Mitin Mar 06 '23 at 12:51

2 Answers2

2

According to

object scala in compiler mirror not found - running Scala compiler programmatically

"scala.runtime in compiler mirror not found" but working when started with -Xbootclasspath/p:scala-library.jar

Why am I getting "object scala.runtime in compiler mirror not found." with scala-maven-plugin when dependency is upgraded?

object scala in compiler mirror not found - running Scala compiler programatically [no sbt - no IDE]

most probably you should try to switch on settings.usejavacp.value = true as written in the error message. How to feed this setting to the compiler depends on how you run it.

See examples how to run the compiler programmatically in

Exception while compiling scala code from Java program

How to compile and execute scala code at run-time in Scala3?

How can I run generated code during script runtime?

Scala reflection: compile akka actor with protobuf

Dynamic compilation of multiple Scala classes at runtime

How to eval code that uses InterfaceStability annotation (that fails with "illegal cyclic reference involving class InterfaceStability")?

Tensorflow in Scala reflection

dynamically parse json in flink map

I'm afraid the code you provided is not self-contained, so can't be used for reproduction. If you still experience issues please prepare MCVE.

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • 1
    I noticed an open ticket about classpaths which started as a clash of `--release 8` and `-javabootclasspath`. So many options, so many class loaders, so many build tools. https://github.com/scala/scala/pull/10336 – som-snytt Mar 06 '23 at 21:34
1

Thanks for the answer, I have set the appropriate options in my code

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)

My testing environment is a system that uploads scala code and compiles and runs scala code. So I can't provide mcve. I submitted some scala programs that worked fine without importing jni, but whenever I tried to include jni code in my scala code, I got an error saying that scala.native was not found Here is my program

class DllTest {

  @native def intMethod(n: Int): Int

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

It is then imported into the embedded scala code

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

Is scala-compiler.jar unable to compile native methods? Moreover, the imported code works fine in the main system function, but scala-compiler.jar throws an error

After debugging, the code that throws the exception looks like this enter image description here

tony
  • 21
  • 3
  • I could try to reproduce, firstly without scala-compiler.jar, secondly with it. Do you have an example of `libtest.dll` so that I could reproduce? Will this work on Linux (I'm on Ubuntu, not Windows). It seems there are some issues with classpath. You should compare classpath while not using scala-compiler.jar and while using it. It should work somehow with native methods since when you compile without scala-compiler.jar it's actually done by the same scala-compiler.jar, just it's not you who starts it manually. – Dmytro Mitin Mar 08 '23 at 15:34
  • Do you have any updates? – Dmytro Mitin Mar 16 '23 at 01:28