1.18 JavaScript

Discussion in 'Spigot Plugin Development' started by xMaikiYT, Jan 13, 2022 at 2:37 PM.

  1. Hello,

    I am trying to execute javascript code from my bukkit plugin.

    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    try {

    System.out.println(engine.getContext());
    System.out.println(engine.eval(PlaceholderAPI.setPlaceholders(player, "%player_name% == MaikyDev"));
    }catch (Exception a1) {
    System.out.println(a1);
    }

    Engine.getContext() is null!
     
  2. Replace JavaScript with nashorn (the default jscript engine that comes with the jdk).
     
  3. If i am not wrong the nashorn engine it was removed from java 14
     
  4. Optic_Fusion1

    Resource Staff

    It's still usable, you just need the following dependency

    <dependency>
    <groupId>org.openjdk.nashorn</groupId>
    <artifactId>nashorn-core</artifactId>
    <version>VERSION</version>
    </dependency>
     
    • Agree Agree x 2
  5. My Jar is build on java 8 and the server on what is running can be java 8 or 17
     
  6. Hi,

    I imported the nashorn js engine
    upload_2022-1-14_9-59-25.png

    And I still have my js engine null!

    upload_2022-1-14_9-59-5.png
     
  7. I believe the following code should be able to get an instance of the engine from standalone nashorn.
    Code (Java):
    ScriptEngineFactory sef = new NashornScriptEngineFactory();
    ScriptEngine engine = sef.getScriptEngine();
    Also the Nashorn GitHub page on different java versions with Nashorn outlines the use of Nashorn with different Java versions.

    Additionally if you are running the plugin on a server with a newer version of java remember to shade nashorn into the plugin as well.
     
  8. Yeah... from where I can get the nashorn code to addit in my plugin?
     

    Attached Files:

  9. This is the maven dependency that I've used in previous projects.
    Code (Text):
            <dependency>
                <groupId>org.openjdk.nashorn</groupId>
                <artifactId>nashorn-core</artifactId>
                <version>15.3</version>
            </dependency>
    Then use the NashornScriptEngineFactory to create an instance of the engine. If that doesn't work I would recommend trying to build in a newer version of java if possible.
     
  10. I am building in java 8 because my plugins run on 1.8 annddd cant change that atm... But I saw some other plugins add the nashorn in the jar directly without maven import!
     
  11. Also add maven-shade-plugin to your pom.