[英]Running Java from Apple Script
我對 Apple 腳本很陌生,所以請耐心等待。 我需要使用applescript
運行一個.jar
文件,該jar
是不可執行的,所以我調用了com.path.to.myClass
之class
類。 我的 Apple 腳本如下所示-
display alert "You are about to start the image rename process." buttons {"OK", "Cancel"}
set theAnswer to button returned of the result
if theAnswer is "OK" then
do shell script "java -classpath ./ImageRename_JAVA-1.0.0.jar:. com.mff.image.rename.Main"
else
say "Exit"
end if
ImageRename_JAVA-1.0.0.jar
和ImageRename_JAVA-1.0.0.jar
都在同一個目錄中,但是當我運行腳本時,它給了我一個錯誤-
error "Exception in thread \"main\" java.lang.NoClassDefFoundError: com/mff/image/rename/Main
Caused by: java.lang.ClassNotFoundException: com.mff.image.rename.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)" number 1
我設置的classpath
錯誤嗎? 如果是這樣,正確的方法是什么? 另外,如何向classpath
添加更多jar
?
當我從Terminal
運行以下命令時,它運行得很好。
$ java -classpath ./ImageRename_JAVA-1.0.0.jar:. com.mff.image.rename.Main
我知道使用JAR Bundler
可以更好地完成它,但我無法控制JAR
及其由其他人開發的。 有沒有一種方法可以將所有JAR
包含在YourApplicationName.app/Contents/MacOS/Resources/Java/
目錄下的應用程序中,並在類路徑中使用它們。
我認為您不能保證do shell script
的工作目錄是什么,但是您可以使用以下方法解決它:
set scriptPath to the POSIX path of (path to me)
do shell script "SCRIPTDIR=`dirname " & scriptPath & "` ; " ¬
& "java -classpath $SCRIPTDIR/ImageRename_JAVA-1.0.0.jar:$SCRIPTDIR com.mff.image.rename.Main"
要將額外的 JAR 添加到類路徑,您可以利用java
命令提供的快捷方式,其中以*
結尾的類路徑條目包括給定目錄中的所有.jar
文件。
do shell script "java -classpath " ¬
& "/Applications/Something.app/Contents/Resources/Java/\\* com.example.MyClass"
*
需要被反斜杠轉義以防止它被shell擴展,當它在AppleScript字符串文字中時,反斜杠本身需要被反斜杠轉義,因此\\\\*
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.