Java ScriptEngineManager no longer works with Moun

2020-07-06 08:38发布

问题:

Ever since I upgraded to Mountain Lion, I can't run my AppleScript code through the Java ScriptEngineManager.

The sample code found on Apple's page (link) returns null for the engine object.

public static void main(String[] args) throws Throwable {
    String script = "say \"Hello from Java\"";

    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("AppleScript");
    engine.eval(script);
}

Anybody know of any workarounds?

回答1:

I got it working by adding a file named "javax.script.ScriptEngineFactory" in the folder "META-INF/services" of my jar as ytw indicated.

I also have to change a bit of code: language seems to no longer be "AppleScript" but "AppleScriptEngine". So this should work:

    public static void main(String[] args) throws Throwable {
        String script = "say \"Hello from Java\"";

        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByName("AppleScriptEngine");
        engine.eval(script);
    }

At least this works on my MacOS X Mavericks with JDK 1.7.45...



回答2:

I posted this issue on bugreport.apple.com (bug id: 12692742). I received this response:

20-Feb-2013 04:21 PM Apple Developer Bug Reporting Team : We think you had an install of Lion with JavaDeveloper. You upgraded to Mountain Lion, which removed (by design) all traces of previously installed Java SE 6 under /System. This left a functional SE 6 JDK bundle under /Library/Java/JavaVirtualMachines, but the contents of /System/Library/Java/Extensions were gone.

You can resolve this a couple ways:

1) Re-install the JavaDeveloper package on the Mountain Lion system.

2) Re-install Java for OS X by removing any JDK bundles under /Library/Java/JavaVirtualMachines and /System/Library/Java/JavaVirtualMachines and running 'java -version' or '/usr/libexec/java_home --request' to initiate install-on-demand.

3) Install Java 7 from Oracle, which bundles AppleScriptEngine.

Of the 3 options, #3 is the recommend one, as developers should be moving to Java 7 anyway.



回答3:

I got this working by copying AppleScriptEngine.jar and libAppleScriptEngine.jnilib from /System/Library/Java/Extensions/ on Mac OS X 10.7 (Lion) and adding it to my classpath.

Not sure why Apple removed these extensions in Mountain Lion. I've asked about it on their developer forums here: link.



回答4:

On Mac OS Yosemite, java -version : 1.7.0_40-ea

  1. Instead of ScriptEngine engine = mgr.getEngineByName("AppleScript"); you must use:

ScriptEngine engine = mgr.getEngineByName("AppleScriptEngine");

  1. In your src directory create directory META-INF

  2. In your src directory create directory META-INF/services

  3. Create file META-INF/services/javax.script.ScriptEngineFactory

  4. In this file is one line:

apple.applescript.AppleScriptEngineFactory

Build and run application ! (BTW it is not more Java, it is magic)



回答5:

The AppleScriptEngine class is already in rt.jar (Java 7 for Mac OS X). If you follow the information here (http://docs.oracle.com/javase/7/docs/api/?javax/script/package-summary.html) to make it 'discoverable' by placing a file called javax.script.ScriptEngineFactory under META-INF/services, then it can work without relying on the AppleScriptEngine.jar in /System/Library/Java/Extensions/. Personally, I think it's more robust to use what's already in rt.jar.