0

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

DShah
  • 9,768
  • 11
  • 71
  • 127
  • In your Windows command, you have /Users/cp_mac1/Desktop/EncUtility/Bowl, whereas in your MacOS version, you only have ../EncUtility; shouldn't that be ../EncUtility/Bowl? – Frank Schmitt Dec 20 '11 at 12:49
  • no i have also noticed that ... but its working fine in Terminal... – DShah Dec 20 '11 at 12:57
  • I don't think this has anything to do with Objective C, and please, please take your employer's name out of classpath in the examples you have posted. Nobody should get fired at this time of year. Oh, and it's a classpath problem. I see you're passing relative paths in your second example. Perhaps that's it. – Tim Dec 20 '11 at 15:37
  • @TimKemp Who's going to get fired for that? – Dave Newton Dec 20 '11 at 15:44
  • @DaveNewton he works for a secretive division of a Wall St. bank who would be none too pleased to discover that an offshore developer was broadcasting their continued usage of Java 1.4 on the world's most popular programming website. His contract likely prohibits this kind of thing. While I agree it's unlikely he'll get fired for this, a little caution goes a long way in his chosen field. Anyway, I've done it for him. – Tim Dec 20 '11 at 16:00
  • @TimKemp: I am sorry but I have made a little correction in my first line of code for windows. Also I know that i have used relative path for windows... But i have to use relative path as i have to make Executable file which i have to transfer it to other place..... – DShah Dec 20 '11 at 17:51

1 Answers1

2

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: e.g.

  #!/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.)

Community
  • 1
  • 1
BRPocock
  • 13,638
  • 3
  • 31
  • 50
  • how should i store this file?? I mean what extension should i give?? – DShah Dec 20 '11 at 17:53
  • You don't need to use an “extension” on a program on a Unix system; just set it to have executable permissions. EG, you could name this file “file-choose-app” and do `chmod +x ~/path/to/file-choose-app` or (I think you can) set the “execute” permission bit in the “Get Info” window in Finder. (The `#!/bin/bash` header line tells the system how to interpret the program.) – BRPocock Dec 20 '11 at 17:58