• Advertisement
Sign in to follow this  

[java] Java SE 6.0 Scripting

This topic is 4222 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it just me, or is this exciting? Scripting for the Java Platform. I think the first little code example says it all.
import javax.script.*;

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
try {
    jsEngine.eval("print('Hello, world!')");
} catch (ScriptException ex) {
    ex.printStackTrace();
}    

Event more exciting is the way you let the script access the java.
  List<String> namesList = new ArrayList<String>();
  namesList.add("Jill");
  namesList.add("Bob");
  namesList.add("Laureen");
  namesList.add("Ed");
  jsEngine.put("namesListKey", namesList);
  System.out.println("Executing in script environment...");
  try {
    jsEngine.eval("var x;" +
                  "var names = namesListKey.toArray();" +
                  "for(x in names) {" +
                  "  println(names[x]);" +
                  "}" +
                  "namesListKey.add(\"Dana\");");
  } catch (ScriptException ex) {
      ex.printStackTrace();
  }
  System.out.println("Executing in Java environment...");
  for (String name: namesList) {
    System.out.println(name);
  }  

I have done scripting in the past, and embedding the language always seems to be the hardest part. This will be fun to play with! Ciao

Share this post


Link to post
Share on other sites
Advertisement
and not to forget that there are at least 16 suppoerted engines available at the moment (scripting.dev.java.net, beanshell.org and the included javascript). IMHO this is a great number since JSE 6.0 is still beta. Hopefully, it will increase further and we can use LUA, which is probably the most popular one in gamedev..

Share this post


Link to post
Share on other sites
Wow that sure looks nice! Thanks for sharing this info with us :) Now I've began to like Java even more.

Share this post


Link to post
Share on other sites
I think some things got broken in recent builds. The source for the "Java" scripting engine (not "javascript") refers to things which don't exist anymore. Although, it might just be me. I may have messed up because the documentation is poor.

Has anyone gotten the Java Engine to work? You can find it in the extra engines pack at scripting.dev.java.net. It'd be really really nice to make a Quake-Like console without needing to write a command parser :)

Update:
Ok, I figured it out. The packages at scripting.dev.java.net are outdated with 6.0 beta 2. I downloaded the source from the CVS and found that things were referring to classes and methods which don't exist anymore. I patched things up mostly, but there are issues getting the current JavaCompiler instance from javax.tools.

In the meantime, javascript isn't a terrible option. You can interface with outside classes like this:

public class ScriptTest {
public ScriptTest() {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine scriptEngine = manager.getEngineByName("JavaScript");
String packageName = this.getClass().getPackage().getName();

try {
scriptEngine.eval("importPackage(" + packageName + ");");
scriptEngine.eval("ScriptTest.saySomething(\"Hello World\");");
}
catch(ScriptException ex) {
ex.printStackTrace();
}

}

public static void saySomething(String msg) {
System.out.println(msg);
}

public static void main(String[] args) {
new ScriptTest();
}
}


[Edited by - Funkapotamus on August 2, 2006 1:55:43 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement