简体   繁体   中英

How to make executable file for Mac for running java command

I have following command that runs on Windows:

java -classpath lib/prov-jdk14-132.jar;../EncUtility com.xxxx.projects.disc.bowl.FileChooseApp

Now I am using nano command to make executable with following command in OS X:

java -classpath ../EncUtility/lib/prov-jdk14-132.jar:../EncUtility com.xxxx.projects.disc.bowl.FileChooseApp

This command runs perfectly in a terminal but when I use nano command to make utility then it shows the following error:

cp_mac1$ /Users/cp_mac1/Desktop/EncUtility/start ; exit;
Exception in thread "main" java.lang.NoClassDefFoundError: com/xxxx/projects/disc/bowl/FileChooseApp
Caused by: java.lang.ClassNotFoundException: com.xxxx.projects.disc.bowl.FileChooseApp
    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)
logout

[Process completed]

I have figure out that it uses extra /start in path. But i am not able to solve it even using cd ..

Your script is being run in a different folder than where it is stored…

The current working directory, when you start your script, is not the location of the script, it's whatever folder you happened to be in when you ran it.

Try using the snippet from Getting the source directory of a Bash script from within to set the working directory, from which your paths should be relative: eg

  #!/bin/bash
  SOURCE="${BASH_SOURCE[0]}"
  while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

  cd $DIR/..
  exec java -classpath EncUtility/lib/prov-jdk14-132.jar:EncUtility \
            com.xxxx.projects.disc.bowl.FileChooseApp

The preamble does various shell magics to resolve where the script actually is stored. The cd then changes the working directory to the folder containing that, and the exec is just for a tiny efficiency: it replaces your script's executable process with the Java VM, rather than starting it as a child process. (Note that nothing beyond the exec would run in your script.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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