Reputation: 7932
I am very new to Apple scripting so please bear with me. I need to run a .jar
file using applescript
, the jar
is not executable so I invoke the class
like com.path.to.myClass
. My Apple script looks like below-
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-
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? If so, what is the correct way? Also, how can I add more jar
s to the classpath
?
When I run the below command from Terminal
it runs just fine.
$ 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. 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.
Upvotes: 0
Views: 3917
Reputation: 122394
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:
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.
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 \\*
.
Upvotes: 2