Reputation: 71
I used scala-compiler.jar to compile an embedded Scala program This scala program imports a class written using jni
class test{
def test(ctx: ContractContext): ActionResult = {
val s = new DllTest
s.loadLibrary()
print(s.intMethod(5))
null
}
}
DllTest is a jni application
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?
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))
......
Upvotes: 0
Views: 1440
Reputation: 51703
According to
object scala in compiler mirror not found - running Scala compiler programmatically
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
Tensorflow in Scala reflection
dynamically parse json in flink map
Upvotes: 2
Reputation: 71
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
Upvotes: 1