1

I want to use Java TM Scripting Engines, but not with JavaScript. I want to use ScriptEngine for Java language. I found Article where all explains. But I can't run example.

Maven dependency java-engine.

Java TM Scripting Engines - supports many different kinds of engines

When I try to run program I receive: Engine error (unknown engine)!

why ScriptEngineManager can't getEngineByName("java")?

also who know another methods how to create Java interpreter in Java program please share your knowledge

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:\Users\RooT>java -version

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)

after update: 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

Article for java-engine

452
  • 366
  • 3
  • 7
  • 21

3 Answers3

1

The only thing I've come across that could interpret Java was BeanShell, but there hasn't been a release of that for a number of years. The bottom line is that there doesn't seem to be a big demand for this functionality.

Ian Fairman
  • 625
  • 5
  • 11
1

After checking the java-engine-20080611.jar I can only conclude the JAR is not in your project's run-time classpath. Or that the JAR requires a JDK and you are running it in a JRE - there is no tools.jar in a JRE where the compiler resides.

Java is not a scripting language, there's too much syntax compared to JavaScript or Groovy and other Java scripting languages. That's why there are JavaCompiler and MemoryClassLoader classes in the JAR - the script needs to be compiled and the generated class loaded.

Cebence
  • 2,366
  • 2
  • 17
  • 20
0

I tried to run your code and I couldn't find the artifact:

<dependency>
    <groupId>com.sun.script</groupId>
    <artifactId>java-engine</artifactId>
    <version>20080611</version>
</dependency>

in Maven Central, so I downloaded it (using the link you provided) to my project directory to lib subdirectory and then changed th dependency to:

<dependency>
    <groupId>com.sun.script</groupId>
    <artifactId>java-engine</artifactId>
    <version>20080611</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/java-engine-20080611.jar</systemPath>
</dependency>

and the scripting engine started working as expected.

Konrad Botor
  • 3,904
  • 1
  • 10
  • 21