0

Ok, this seems to be a typical error as of discussing a lot in here, however I still don't know what's wrong with my program. I use commmand prompt to compile my java classes.

TestQueueString.java is a simple class I create to test my implementation of the block representation of queue in QueueStringBlock.java. This is how I compile.

C:\Users\ME\Desktop\Lab>javac -cp DAT.jar;Exceptions.jar QueueStringBlock.java TestQueueString.java

C:\Users\ME\Desktop\Lab>javac TestQueueString
error: Class names, 'TestQueueString', are only accepted if annotation processing is
explicitly requested 1 error

Any help would be appreciated.

EDIT

And when I use java instead of javac, it always asks for main method I guess:

C:\Users\TRI\Desktop\Lab>java -cp DAT.jar;Exceptions.jar TestQueueString
Exception in thread "main" java.lang.NoClassDefFoundError: TestQueueString
Caused by: java.lang.ClassNotFoundException: TestQueueString
        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:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: TestQueueString.  Program will exit.

Be noticed that it does produce .class files after compiling :)

Community
  • 1
  • 1

2 Answers2

5

You're calling javac, the compiler, to run your program. You run the program via java, not javac. Your second command should be:

java -cp DAT.jar;Exceptions.jar TestQueueString

Note no "c" on "java", and I've added the classpath since you added it when compiling.


Re your edit: The problem there is exactly what it says it is: It can't find the class TestQueueString. I can think of two possible reasons for this:

  1. The current directory is not in your classpath; try:

    java -cp DAT.jar;Exceptions.jar;. TestQueueString
    

    (Note the ;. at the end of the classpath.)

  2. Your TestQueueString.java file has a package declaration. If so, you need to tell java the full name of the class (e.g., foo.TestQueueString not just TestQueueString if it were in package foo), and the file has to be in the right place relative to the current working directory (again assuming package foo, the class file would have to be in a foo subdirectory).

Given the error message you've quoted, and assuming you're using an up-to-date version of Java, I'd guess #1, since #2 would say something like

Exception in thread "main" java.lang.NoClassDefFoundError: TestQueueString (wrong name: foo/TestQueueString)

...(note the "wrong name" bit) if the package were foo.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • It still doesn't help. See my edit above : ) Thanks for answering anyway. –  Mar 28 '12 at 06:07
  • @Trisism: The edit changes the question. I've edited the answer to reflect the new question. – T.J. Crowder Mar 28 '12 at 06:23
  • T.J.Crowder: "java -cp DAT.jar;Exceptions.jar TestQueueString" causes the same error... ): –  Mar 28 '12 at 06:35
  • And TestQueueString.java only has a few lines, just a constructor that calls QueueStringBlock's implementations i.e. q.enqueue(), q.dequeue(), etc. It has no package declaration at all. –  Mar 28 '12 at 06:37
  • @Trisism: Sorry, I had an error in #1 of the edit, I kind of forgot to add `.` to the classpath (which was the whole point of #1). If that's not it, it's clear that you need to post the full contents of `TestQueueString.java` in the question. – T.J. Crowder Mar 28 '12 at 06:43
  • @Trisism: Good deal! Sorry for the editing error earlier. :-) – T.J. Crowder Mar 28 '12 at 07:12
1

You are setting the Classpath to Dat.jar, and Exception.jar, essentially telling the JVM to look for the .class files in those locations. However, TestQueueString is not in either .jar file. It is in your current directory. Since you told the JVM to look for .class files in the .jars, it does not check your current directory. Breaking it down step by step, this line:

java -cp DAT.jar;Exceptions.jar TestQueueString

says: Run a java program, with the main method found in the class TestQueueString, and search for the .class files in Dat.jar and Exceptions.jar. What you can do is add ".;" to the beginning of your Classpath, which tells the JVM to also search in the current directory.

Adno
  • 203
  • 3
  • 10