Is it possible to run Scala code instead of Java?

60 views
Skip to first unread message

Henrik Johansson

unread,
May 26, 2020, 9:16:52 AM5/26/20
to node-java
  
   As the title says, I'm trying to use the package to run Scala code, but so far without success.
  

  Setup:
 
   I have set up a test project in Scala using sbt, compiled it and moved the resulting .jar file to the same dir as my test js file. (Files below)
  

   Error:

   The error I receive is the following:

/home/henrik/UU/bachelor/tests/test.js:7
var glue = java.newInstanceSync("Tape.typeWrapper");
               
^

Error: Could not find method "Tape.typeWrapper()" on class "class Tape.typeWrapper". No methods with that name.
    at
Object.<anonymous> (/home/henrik/UU/bachelor/tests/test.js:7:17)
    at
Module._compile (module.js:653:30)
    at
Object.Module._extensions..js (module.js:664:10)
    at
Module.load (module.js:566:32)
    at tryModuleLoad
(module.js:506:12)
    at
Function.Module._load (module.js:498:3)
    at
Function.Module.runMain (module.js:694:10)
    at startup
(bootstrap_node.js:204:16)
    at bootstrap_node
.js:625:3

  I have tried using both object and class keywords, neither work. There seems to be some issue with the .class file generated when compiling the .scala file.


    Any help or suggestions are welcome.

  Scala file:

   package Tape


   import java.io.File

   object typeWrapper {

     def someWrapper(x : Any) = Some(x)

     def nullSome() = None;

     def nullJavaFile() = null : File

     def trueBool() = true

     def falseBool() = false

     def main(args: Array[String]): Unit = {
       println("Hello world!")
     }
   }

  JavaScript File:


   const java = require('java');

   java.classpath.push("./glue.jar");

   var glue = java.newInstanceSync("Tape.typeWrapper");

   console.log("Whoo!");

Reply all
Reply to author
Forward
0 new messages