简体   繁体   English

从 Apple Script 运行 Java

[英]Running Java from Apple Script

I am very new to Apple scripting so please bear with me.我对 Apple 脚本很陌生,所以请耐心等待。 I need to run a .jar file using applescript , the jar is not executable so I invoke the class like com.path.to.myClass .我需要使用applescript运行一个.jar文件,该jar是不可执行的,所以我调用了com.path.to.myClassclass类。 My Apple script looks like below-我的 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

Both the applescript and the ImageRename_JAVA-1.0.0.jar are in the same directory, but when I run the script it gives me an error- ImageRename_JAVA-1.0.0.jarImageRename_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

Am I setting the classpath wrong?我设置的classpath错误吗? If so, what is the correct way?如果是这样,正确的方法是什么? Also, how can I add more jar s to the classpath ?另外,如何向classpath添加更多jar

When I run the below command from Terminal it runs just fine.当我从Terminal运行以下命令时,它运行得很好。

$ java -classpath ./ImageRename_JAVA-1.0.0.jar:. com.mff.image.rename.Main

I know that it can be done in a better way using JAR Bundler but I have no control over the JAR and its developed by someone else.我知道使用JAR Bundler可以更好地完成它,但我无法控制JAR及其由其他人开发的。 Is there a way that I can include all the JAR s inside the application under YourApplicationName.app/Contents/MacOS/Resources/Java/ directory and use those in the class path.有没有一种方法可以将所有JAR包含在YourApplicationName.app/Contents/MacOS/Resources/Java/目录下的应用程序中,并在类路径中使用它们。

I don't think you can guarantee what the working directory is in a do shell script , but you can work it out with something like this:我认为您不能保证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"

To add extra JARs to the classpath you can take advantage of a shortcut provided by the java command whereby a classpath entry ending in * includes all .jar files in the given directory.要将额外的 JAR 添加到类路径,您可以利用java命令提供的快捷方式,其中以*结尾的类路径条目包括给定目录中的所有.jar文件。

do shell script "java -classpath " ¬
  & "/Applications/Something.app/Contents/Resources/Java/\\* com.example.MyClass"

The * needs to be backslash escaped to protect it from expansion by the shell, and the backslash itself needs to be backslash-escaped when it is within an AppleScript string literal, hence the \\\\* . *需要被反斜杠转义以防止它被shell扩展,当它在AppleScript字符串文字中时,反斜杠本身需要被反斜杠转义,因此\\\\*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM