Java ScriptEngineManager no longer works with Moun

2020-07-06 08:53发布

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?

5条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-07-06 09:00

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.

查看更多
祖国的老花朵
3楼-- · 2020-07-06 09:01

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.

查看更多
该账号已被封号
4楼-- · 2020-07-06 09:09

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...

查看更多
淡お忘
5楼-- · 2020-07-06 09:19

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.

查看更多
我想做一个坏孩纸
6楼-- · 2020-07-06 09:20

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)

查看更多
登录 后发表回答