Results 1 to 7 of 7
  1. #1
    otacon's Avatar
    otacon is offline Member
    Join Date
    Dec 2010
    Posts
    24
    Rep Power
    0

    Default Problem with main class

    Ok, so I'm having a kind of a silly problem, which might have a very simple solution but apparently I don't see it.

    My java compiler does not see my main class.

    I make a folder, write a simple class:

    class Test{

    public static void main(String[] args){
    System.out.println("Hello World");
    }
    }

    in cmd I get to the folder, compile (C:\Test> javac Test.java), works without any problmes, and creates the Test.class. While in the folder i run "java Test" and get :

    C:\test>java Test
    Exception in thread "main" java.lang.NoClassDefFoundError: Test
    Caused by: java.lang.ClassNotFoundException: Test
    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: Test. Program will exit.


    I don't think its a path issue, since I'm IN the folder trying to run it...any ideas will be appriciated.

    (btw, I have no problems running it through eclipse)

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    First, is that all your code?
    There's no package declaration you're not showing us is there?

    Also, is there a Test.class file in that folder after compiling?

  3. #3
    otacon's Avatar
    otacon is offline Member
    Join Date
    Dec 2010
    Posts
    24
    Rep Power
    0

    Default

    yep, thats all the code, no packages or anything else.

    And yes, after the code compiles Test.class is created in the same folder.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    What does "echo $CLASSPATH" (or "echo %CLASSPATH%" on Windows) show? If there is no CLASSPATH then you should be able to execute a .class file in the current directory, but if you do have a CLASSPATH and . (the current directory) is not part of it, then you can't.

    -Gary-

  5. #5
    otacon's Avatar
    otacon is offline Member
    Join Date
    Dec 2010
    Posts
    24
    Rep Power
    0

    Default

    Wow. That was a classic example of a bran-fart on my end! I actually spent hours trying to fix it (as originally the problem seemed a lot more complex before i narrowed it down to not being able to run a hello world class:D )

    But yes, you are absolutely correct, my classpath was pointing to a jade.jar (which is a really cool Java Agent DEvelopment Framework, btw). Adding my test location to the classpath fixed the problem.

    Thank you so much! I feel silly :)

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Why not just add '.'?
    Because otherwise you'll encounter this again.

    Also you really don't want to be adding your jar files and all that to the system CLASSPATH. That's what the -cp option is for with java and javac.

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by otacon View Post
    Wow. That was a classic example of a bran-fart on my end! I actually spent hours trying to fix it (as originally the problem seemed a lot more complex before i narrowed it down to not being able to run a hello world class:D )

    But yes, you are absolutely correct, my classpath was pointing to a jade.jar (which is a really cool Java Agent DEvelopment Framework, btw). Adding my test location to the classpath fixed the problem.

    Thank you so much! I feel silly :)
    Don't feel bad, it happens all the time. And by creating an SSCCE, you made it easy to identify the problem and get you the help you needed. Too bad that lesson won't ge around to the newbies who need it. :)

    -Gary-

Similar Threads

  1. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 09:30 AM
  2. how do i call a class from main class
    By revanger in forum New To Java
    Replies: 7
    Last Post: 07-20-2010, 06:32 PM
  3. Replies: 6
    Last Post: 05-18-2010, 06:11 PM
  4. different multiple public class and main class
    By mr idiot in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 01:10 PM
  5. How to create main class link to another two class?
    By pearllymary78 in forum New To Java
    Replies: 6
    Last Post: 07-17-2008, 12:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •