4

I've looked here, here, here, and here.

While informative they just didn't quite have enough for me to discover the root of my problem. My code isn't contained within a JAR file and the customer has requested we do not ship it as such.

I built the application in Eclipse and from there it runs fine. I've set up a script that will modify the main method of one of the java files for testing purposes. I want to run the application using a call from Python after I modify it. However, from the command line when I attempt to call the program using java or java -cp . I get the following error:

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

My project depends on several JARs, which are not within the root directory, and a DLL file, again not in the root directory. I'm hoping it's related to the location of one of those but I'm not sure and am making little headway. Here is DemoAPIFunctionality.java:

public class DemoAPIFunctionality
{
  public final static void main(String[] args)
  {
    DemoAPIFunctionality demo = new DemoAPIFunctionality();

I'd happily list any more information if it will help you help me. (No, I'm not a sports agent.) What do I need to do to get it to work at least from the command line?

Thanks in advance.

Community
  • 1
  • 1
wheaties
  • 35,646
  • 15
  • 94
  • 131
  • 1
    Is `DemoAPIFunctionality` in a package? (If you can show the full source, it would help.) What is your directory and file structure? Where do the `.class` files reside? – Dave Jarvis Jan 22 '10 at 22:20

3 Answers3

4
  1. having classes in the default package (i.e. no package) is discouraged
  2. you must call java -cp . from the root directory of your project - i.e. the directory where your .class file resides, or when you create packages, the directory, where the com (org, etc) subdirectory resides.
  3. you must refer to the class by it full qualified name - i.e. com.package.ClassName
  4. you'd better use an IDE - for educational purposes there's BlueJ and JCreator. Eclipse and NetBeans are more powerful and complex.
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 1. Yes, yes, bad me. Will changing it fix it? 2. I did. 3. see 1 4. while built in an IDE they don't want the demo using an IDE. It's supposed to show it can be tested as is, using a script. – wheaties Jan 22 '10 at 22:29
3

You didn't mention that your python script actually compiles the java source file after applying the edits.

Eclipse will compile it automatically (build-in builder) but of course, outside of the IDE you have to call javac before you can execute the class. java will not work on source files.

EDIT

I don't know you actual experience in java - but that's how it usually works on the command line.

Assuming, you have a java class com.example.Hello with a main method and a file structure like

/dev/bin/com/example/Hello.class

then you cd to /dev/bin and do a java com.example.Hello. Note that the package name (com.example) is always mapped to a folder structure (com/example). If you do the above command and java can't find Hello.class in the folder ./com/example, then it will throw the NoClassDefFoundError.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • You're so right. However, the error remains. I'm using the code compiled by Eclipse at the moment from the command line and am getting the same thing to occur. – wheaties Jan 22 '10 at 23:35
  • 1
    Strange. Please double-check, that you're really in the `bin` folder of your project when executing the command `java DemoAPIFunctionality` and not in the project or `src` folder (assuming you have a typical eclipse project folder structure with `project/src` for sources and `project/bin` for class files) – Andreas Dolk Jan 22 '10 at 23:54
  • Yup, I was in the bin directory and not the src. That's why I included the tidbit about having other JAR dependencies and a DLL dependency which aren't found within the bin directory tree. – wheaties Jan 23 '10 at 00:21
  • If it just can't find the other jars or the dll it would show different errors - `NoClassDefFoundErros` as well but for classes located in the 'missing' jars. – Andreas Dolk Jan 23 '10 at 08:39
  • Damn. I'll keep working at it. It's got to be something simple but since you pointed out the issue with javac before java you get the credit. That ultimately would have tripped me up too. – wheaties Jan 25 '10 at 14:19
3

Easy workaround, hint and shortcut. Dont use any path when calling java classname from within python. eg. commands.getstatusoutput("java -cp $CLASSPATH clusterize")

ajtrichards
  • 29,723
  • 13
  • 94
  • 101
kalegs
  • 31
  • 1