Java ScriptEngine 支持的语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20013310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:19:38  来源:igfitidea点击:

Java ScriptEngine supported languages

javabuilt-inscriptengine

提问by

Java has a ScriptEnginesystem that allows you to run/evaluate statements in a different language.
I know for a fact that JavaScript is supported, but I couldn't find any other languages to work with it.
Is, for example, Ruby implemented?

Java 有一个ScriptEngine系统,允许您运行/评估不同语言的语句。
我知道支持 JavaScript,但我找不到任何其他语言来使用它。
例如,是否实现了 Ruby?

采纳答案by Andrew Thompson

..I know for a fact that JavaScript is supported,..

..我知道支持 JavaScript 的事实,..

ECMAscript, technically.

ECMAscript,技术上。

.. but I couldn't find any other languages to work with it. Is, for example, Ruby implemented?

..但我找不到任何其他语言来使用它。例如,是否实现了 Ruby?

No. The ECMAscript engine is the only one included by default, the last time I heard.

不。ECMAscript 引擎是唯一一个默认包含的引擎,这是我最后一次听说的。

回答by Akira

There are several other languages available. For instance, Jython (Python implementation in Java). The way to use other languages is by adding the JAR file to CLASSPATH and making a reference to the right name.

还有其他几种语言可用。例如,Jython(Java 中的 Python 实现)。使用其他语言的方法是将 JAR 文件添加到 CLASSPATH 并引用正确的名称。

For Ruby, there is JRuby. See the following: https://github.com/jruby/jruby/wiki/JavaIntegration

对于 Ruby,有 JRuby。请参阅以下内容:https: //github.com/jruby/jruby/wiki/JavaIntegration

ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine rubyEngine = m.getEngineByName("jruby");

回答by Kyle Bridenstine

The Java ScriptEngine API will work with all JSR-223 Scripting Languages. I haven't found a well documented complete list of these before but this post does a pretty good job, Where can I find a list of available JSR-223 scripting languages?Here is one list from that post,

Java ScriptEngine API 将适用于所有 JSR-223 脚本语言。我之前没有找到这些文件的完整列表,但这篇文章做得很好, 我在哪里可以找到可用的 JSR-223 脚本语言列表?这是该帖子中的一个列表,

JSR-223 script engines

JSR-223 脚本引擎

  • AWK
  • BeanShell
  • ejs
  • FreeMarker
  • Groovy
  • Jaskell
  • Java
  • JavaScript
  • JavaScript (Web Browser)
  • Jelly
  • JEP
  • Jexl
  • jst
  • JudoScript
  • JUEL
  • OGNL
  • Pnuts
  • Python
  • Ruby
  • Scheme
  • Sleep
  • Tcl
  • Velocity
  • XPath
  • XSLT
  • AWK
  • 豆壳
  • ejs
  • 自由标记
  • 常规
  • 贾斯克尔
  • 爪哇
  • JavaScript
  • JavaScript(网络浏览器)
  • 果冻
  • JEP
  • Hyman斯
  • 刚刚
  • 柔道
  • 尤尔
  • OGNL
  • 坚果
  • Python
  • 红宝石
  • 方案
  • 睡觉
  • TCL
  • 速度
  • XPath
  • XSLT

JSR 223 script engines maintained elsewhere

在别处维护的 JSR 223 脚本引擎

  • JavaFX Script
  • ABCL
  • AppleScript
  • Bex script
  • OCaml Scripting Project
  • PHP
  • PHP (another one)
  • Python
  • Smalltalk
  • CajuScript
  • MathEclipse
  • JavaFX 脚本
  • ABCL
  • 苹果脚本
  • Bex 脚本
  • OCaml 脚本项目
  • PHP
  • PHP(另一个)
  • Python
  • 短暂聊天
  • CajuScript
  • 数学日食

Most have a special implementation for it to work. For example python alone will not work you need the Jython jar added to the class path. Same for Ruby you'll need JRuby.

大多数都有一个特殊的实现来让它工作。例如,单独使用 python 是行不通的,您需要将 Jython jar 添加到类路径中。Ruby 也一样,你需要 JRuby。

回答by hjk321

Here is a script to determine all languages on your system:

这是一个用于确定系统上所有语言的脚本:

import java.util.List;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngineFactory;


public class Test {

public static void main(String[] args)
{
    ScriptEngineManager mgr = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = mgr.getEngineFactories();
    for (ScriptEngineFactory factory : factories)
    {
        System.out.println("ScriptEngineFactory Info");
        String engName = factory.getEngineName();
        String engVersion = factory.getEngineVersion();
        String langName = factory.getLanguageName();
        String langVersion = factory.getLanguageVersion();
        System.out.printf("\tScript Engine: %s (%s)\n", engName, engVersion);
        List<String> engNames = factory.getNames();
        for (String name : engNames)
        {
            System.out.printf("\tEngine Alias: %s\n", name);
        }
        System.out.printf("\tLanguage: %s (%s)\n", langName, langVersion);
        }
    }

}

Hope this helps.

希望这可以帮助。