我想使用Java TM Scripting Engines,但不使用JavaScript。我想使用ScriptEngine for Java語言。我在所有解釋中找到了文章。但我無法運行示例。基於Java的應用程序中Java代碼的解釋 - 如何在java程序中運行java代碼
Maven依賴關係java引擎。
Java TM Scripting Engines - supports many different kinds of engines
當我嘗試運行程序我得到:引擎錯誤(未知引擎)!
爲什麼ScriptEngineManager不能getEngineByName(「java」)?
也,誰知道另一個方法是如何創建的Java程序的Java解釋器,請分享你的知識
package script;
import javax.script.*;
public class Main2 {
public static void main(String[] args) throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("java");
if (engine == null) {
System.err.println("Engine error (unknown engine)!");
} else {
engine.put(ScriptEngine.FILENAME, "TestApp.java");
engine.eval("public class TestApp { public static void main(String[] a) {System.out.println(\"hello!\");} }");
}
}
}
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>alefemet</groupId>
<artifactId>script</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>script</name>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jexl</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.mozilla</groupId>
<artifactId>rhino</artifactId>
<version>1.7R4</version>
</dependency>
<dependency>
<groupId>com.sun.script</groupId>
<artifactId>java-engine</artifactId>
<version>20080611</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>asf</id>
<name>ASF</name>
<url>http://svn.apache.org/repos/asf/servicemix/m2-repo</url>
</repository>
</repositories>
</project>
C:\用戶\ ROOT> Java的版本
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) Client VM (build 23.25-b01, mixed mode, sharing)
更新後:C:\ Users \ RooT> java -version
java version "1.7.0_40"
Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
Java HotSpot(TM) Client VM (build 24.0-b56, mixed mode, sharing)
Java Scripting Programmer's Guide - javax.script
哇!通常最好不要在標題中包含主要標籤,但是您已經設法將它包括在內** 4次!**我不知道該如何處理這個混亂,所以我會保持原樣。 :) –
請將java -version輸出添加到問題中 – WeMakeSoftware