Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to setup a cronjob to download my daily and weekly reports from iTunes Connect. I have downloaded Apple's Autoingestion.class and I have made the following shell script to download the weekly reports:

java Autoingestion <MyAccountName> <MyPassword> <MyVendorID> Sales Weekly Summary

This shell script is stored inside the same directory as the Autoingestion.class. When I run this shell script inside the directory, it works fine. If I go to another directory and run it, I am getting the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: Autoingestion
Caused by: java.lang.ClassNotFoundException: Autoingestion
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: Autoingestion. Program will exit.

Because it does not work when being ran from another directory, my cronjob does not work.

Does anyone have an idea why this happens and if there is anything to do to get rid of it?

share|improve this question
    
Have you tried specifying the classpath with -cp /path/to/your/stuff? –  slhck Oct 22 '11 at 11:06
add comment

2 Answers

up vote 3 down vote accepted

Use the following script:

#!/usr/bin/env bash
cd "$( dirname "$0" )"
java Autoingestion <MyAccountName> <MyPassword> <MyVendorID> Sales Weekly Summary

This will change the working directory to the location of the script, then execute the Java program.

The reason is probably the implied class path .. From man java:

   -cp classpath
          Specifies a list of directories, JAR archives, and ZIP  archives
          to  search for class files.  Class path entries are separated by
          colons (:). Specifying -classpath or -cp overrides  any  setting
          of the CLASSPATH environment variable.

          If -classpath and -cp are not used and CLASSPATH is not set, the
          user class path consists of the current directory (.).

Since java doesn't find Autoingestion in your current working directory if you call the script from somewhere else, it fails to launch.

Depending on the exact behavior of the program (e.g. writing files in the current working directory) you can also try specifying a proper -classpath, e.g.:

java -cp "$( dirname "$0" )" Autoingestion <MyAccountName> <MyPassword> <MyVendorID> Sales Weekly Summary

share|improve this answer
    
I'm not 100% sure the -cp parameter will work exactly like this. It might depend on other libraries you also need. –  Daniel Beck Oct 22 '11 at 11:20
    
Thank you very much! This solved the issue. Also, thanks for the explanation. That makes sense :-) I chose to add cd "$( dirname "$0" )" to the shell script and that did the trick. –  SimonBS Oct 22 '11 at 11:48
add comment

I just had this error, and the reason was I was being a bit stupid - Apple provide Autoingestion.class.zip. I simply hadn't unzipped the class before running the class. Doh!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.