11 Replies Latest reply: Jan 2, 2007 10:35 AM by 807599 RSS

    Cannot find the main class

    807599
      Hello,

      I am running

      Java(TM) 2 Runtime Environment, Standard Edition
      (build 1.5.0_07-b03)
      Java HotSpot(TM) Client VM
      (build 1.5.0_07-b03, mixed mode, sharing)

      on both Windows 2003 Server and Windows XP Professional.

      Whan I try to run any java program that's the message I am getting:

      Exception in thread "main" java.lang.NoClassDefFoundError: TestJava

      On my personal computer it's running fine. I have made sure that the java program works ok. I cannot figure out why this is happening on just a few machines. I even install the SDK on those machines hoping it will allow the program to run. No luck.

      Anybody has experienced something weird like this? Any ideas?
      I need to run my java program on at least 10 machines. I have to persuade a number of folks that java is reliable. I am using java for about 3 years. I have never experienced something like that.

      Glad to provide any additional information.

      Thanks a lot.
        • 1. Re: Cannot find the main class
          807599
          Check if the source code is compiled. I usually get that error when I did not compile the code.

          Hope it helps
          • 2. Re: Cannot find the main class
            doremifasollatido
            Check if the source code is compiled. I usually get that error when I did not compile the code.
            More likely that it is compiled but that the classpath was not specified properly on the "java" command line (using the "-cp" flag) when attempting to run it.

            mchris, I'm curious as to how you got through three years of Java programming without knowing how to set the classpath.
            • 3. Re: Cannot find the main class
              807599
              Thanks for the tips.
              That's why I said it's weird!!
              Program is compiled, classpath is in place and it runs fine on some of my machines.
              To give you an example:

              public class TestJava {

                   public static void main(String Args[]){

                        System.out.println("Success");
                   
                   }

              }

              javac TestJava.java
              java TestJava

              I get "Success" on my personal machine. On the other computers I get the message I posted above. That's why is driving me crazy!!
              I have been messing with classpaths and the like for three years on both Windows and Linux and it's the first time I get this crazy thing.

              I have update JRE to the latest version.

              Any more ideas?
              • 4. Re: Cannot find the main class
                doremifasollatido
                Okay, so enter the following at the cmd prompt in Windows:
                echo %CLASSPATH%
                If it is non-empty, there is your problem--you'll need to unset it (better yet, always specify on command line, even it is already unset). If it contains a "QTJava.zip", then blame QuickTime for setting it.
                • 5. Re: Cannot find the main class
                  807599
                  Ok,

                  Here are the results of echo %CLASSPATH% on the machine I am having trouble with:

                  C:\Program Files\Java\jre1.5.0_08\lib\ext\sqljdbc.jar

                  and the results on my personal machine where everything works fine:

                  C:\Program Files\Java\jdk1.5.0_07\jre\lib\ext\mysql-connector-java-3.1.14-bin.jar;C:\Program Files\Java\sqljdbc_1.1\enu\sqljdbc.jar;

                  I need those jars to access SQL databases.

                  So you are saying that I should clear those entries?

                  (By the way, I have experience problems before with QuickTime!!)

                  Thanks for the effort :)
                  • 6. Re: Cannot find the main class
                    doremifasollatido
                    You're welcome.

                    Okay, you don't necessarily need to delete the whole classpath. But, you do need to get it correct on the machines with trouble.

                    Either change the system-wide classpath on the machines with trouble (make sure it includes all of the necessary jars), or specify it on the command line every time:

                    java -cp "C:\Program Files\Java\jdk1.5.0_07\jre\lib\ext\mysql-connector-java-3.1.14-bin.jar;C:\Program Files\Java\sqljdbc_1.1\enu\sqljdbc.jar" TestJava


                    It is usually recommended that you specify it on the command line, to avoid the exact troubles that you are having: your CLASSPATH environment variable isn't set the way you think it is. If you set it on the command line (could be through a script [e.g., a .bat file]), you are more likely to know what you have there and to remember to get it right.

                    What directory is your class with your main method in? You'll need to have the correct directory for your class's package in the classpath, too. I'm surprised it works on your own machine, since all you have in your classpath are two jars (unless, of course, your class itself is in one of the jars).

                    QuickTime is known to change the Java classpath, even setting one if one wasn't set before. I've seen other people on the forum have the problem, so I thought maybe that was the problem for you.
                    • 7. Re: Cannot find the main class
                      807599
                      Something is extremely wrong here!
                      Let's forget about the classmates for a sec and the jar directories
                      Let me go to the basics here:

                      I installed Java 1.5.0_07-b03 on my Windows 2003 server.
                      In command prompt, when I run java -version I am getting:

                      java version "1.5.0_07"
                      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)
                      Java HotSpot(TM) Client VM (build 1.5.0_07-b03, mixed mode, sharing)

                      When I just type java I am getting:

                      Usage: java [-options] class [args...]
                      (to execute a class)
                      or java [-options] -jar jarfile [args...]
                      (to execute a jar file)

                      where options include:
                      -client to select the "client" VM
                      -server to select the "server" VM
                      -hotspot is a synonym for the "client" VM [deprecated]
                      The default VM is client.

                      -cp <class search path of directories and zip/jar files>

                      .
                      .
                      .
                      etc.

                      To me that tells me java (JRE) is up and running. I should be able to run a program as easy as

                      public class TestJava {

                      public static void main(String Args[]){

                      System.out.println("Success");

                      }

                      }

                      after I compile it as such

                      javac TestJava.java
                      without getting any errors.

                      However when I run

                      java TestJava

                      I am getting

                      Exception in thread "main" java.lang.NoClassDefFoundError: TestJava

                      I am in the same directory where the TestJava.class exists.

                      Something so simple and it does not work. That's what drives me crazy.

                      Any other test you might suggest?

                      Thanks a lot.
                      • 8. Re: Cannot find the main class
                        doremifasollatido
                        Your current directory is not in your classpath, apparently. If you have a CLASSPATH environment variable and don't use -cp on the command line, then the environment variable is used. If it doesn't include the current directory, you won't be able to use things in your current directory. In newer versions (including 1.5) of Java, if no CLASSPATH exists and you don't use -cp, then the classpath is set to "current directory only". You have three options (maybe more):

                        1. Delete CLASSPATH, use -cp on the command line to specify the CLASSPATH (including the current directory, which is represented as "." [single period as the path]).

                        2. Leave CLASSPATH, use the following with "-cp" (which appends the current directory to the environment variable, only for the current call):
                        java -cp %CLASSPATH%;. TestJava
                        3. Append ";." to the permanent CLASSPATH environment variable, so that you don't have to do #2 every time.
                        • 9. Re: Cannot find the main class
                          796447
                          I am in the same directory where the TestJava.class
                          exists.
                          And that directory is not in your classpath. And it needs to be. Period.
                          • 10. Re: Cannot find the main class
                            807599
                            set classpath=.

                            Ted.

                            EDIT:
                            God, I even amaze myself some days!
                            • 11. Re: Cannot find the main class
                              807599
                              Thank you very much for all the help and effort!!

                              :)