开发者

Can I run jrunscript from a JRE

开发者 https://www.devze.com 2022-12-11 08:31 出处:网络
The JDK has jrunscript executables.Does the JRE have the needed jars that can be executed so as not开发者_运维知识库 to require a complete JDK to use jrunscript?

The JDK has jrunscript executables. Does the JRE have the needed jars that can be executed so as not开发者_运维知识库 to require a complete JDK to use jrunscript?

In other words, can I run jrunscript by calling the java executable with a class name.


JRunscript executables are nothing but javascript files. I do not know if you can directly use commandline with just jre installed but, You can definitely do it using a simple javaclass and running the javaclass from the commandline.

import java.io.FileReader;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class RunScriptFile {
  public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    try {
      FileReader reader = new FileReader(args[0]);
      engine.eval(reader);
      reader.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

To compile (Required JDK):

javac RunScriptFile.java.
You'll get "RunScriptFile.class"

To Run (Requires JRE):

java RunScriptFile <myawesomescript.js>

Hope this helps.


jrunscript resides in com.sun.tools.script.shell.Main inside tools.jar on my JDK.

That file is apparently not bundled with the JRE

As someone already mentioned a small amount of java code can be used to launch your script instead:

new ScriptEngineManager().getEngineByName("js").eval(jsCode)

The special jrunscript functions such as ls(), mv(), echo() and read() are not in the JRE.

These functions are actually JavaScript functions defined in an init script [see init.js from the openjdk repo]. That script is automatically loaded by jrunscript before your code.

You could copy these functions in your script. Just keep in mind that openjdk is licensed under the GPL.


The JRE contains everything required run java programs, including any standard library jar files.

The JDK contains the tools and libraries required to write Java programs. So as long as you don't accidentally import implementation classes (such as stuff in com.sum.*) then you java program should run on any JRE of the correct version.

0

精彩评论

暂无评论...
验证码 换一张
取 消