Results 1 to 11 of 11
  1. #1
    mc6415 is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default Problem with Java...

    Well my problem is as follows. Until today all my java files compiled and ran just fine, though all of a sudden today they compile and do not run instead giving me this error:

    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: hello
    Caused by: java.lang.ClassNotFoundException: hello
            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: hello.  Program will exit.
    The code from said file is:

    Java Code:
    public class hello
    {
    	public static void main (String args[])
    	{
    		System.out.println("Hello");
    	}
    }
    As you can see very very simple bit of code due to me being very new to this.

    EDIT: After trying it in a command prompt and getting the error I decided to copy and paste the code directly into an IDE (JCreator) and it compiled and ran the code with no problems, I just can't seem to get it to work in the command prompt which is frustrating.
    Last edited by mc6415; 02-20-2010 at 06:15 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    The error means that the Java runtime cannot find the class hello. This is quite common - so don't panic!

    What command are you using to launch the application?

    Try something like:

    Java Code:
    > dir
    > java -cp . main

    Use these commands from the directory that contains the hello.class file. The first line - "dir" - is not necessary. It is just a sanity check which should verify that hello.class really does exist in the current directory. (Nonexistence or misspellings are a common source of NoClassDefFound)

    The interesting bit is "dash-cp-space-dot-space". This tells the Java runtime where to look for classes. In this case it is saying "The classpath (the place to look for classes) is the current directory".

    If this doesn't work post the commands and the exact and entire output they generate.

  3. #3
    mc6415 is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    The error means that the Java runtime cannot find the class hello. This is quite common - so don't panic!

    What command are you using to launch the application?

    Try something like:

    Java Code:
    > dir
    > java -cp . main

    Use these commands from the directory that contains the hello.class file. The first line - "dir" - is not necessary. It is just a sanity check which should verify that hello.class really does exist in the current directory. (Nonexistence or misspellings are a common source of NoClassDefFound)

    The interesting bit is "dash-cp-space-dot-space". This tells the Java runtime where to look for classes. In this case it is saying "The classpath (the place to look for classes) is the current directory".

    If this doesn't work post the commands and the exact and entire output they generate.
    Hi there,

    Thank you for replying, upon using your result I still got errors.

    What I get is:

    Java Code:
    Microsoft Windows [Version 6.1.7600]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
    
    C:\Users\Coombes>cd \Users\Coombes\Documents\JavaStuff
    
    C:\Users\Coombes\Documents\JavaStuff>java -cp . attempt
    Exception in thread "main" java.lang.NoClassDefFoundError: attempt
    Caused by: java.lang.ClassNotFoundException: attempt
            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: attempt.  Program will exit.
    
    C:\Users\Coombes\Documents\JavaStuff>
    Thank you for the help :D.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Do use the "dir" command as it helps to identify all sorts of simple errors like spelling mistakes.

    In this case you would expect to see "attempt.class" listed among the files in your JavaStuff directory. If it not there then you have not compiled the program successfully.

  5. #5
    mc6415 is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Do use the "dir" command as it helps to identify all sorts of simple errors like spelling mistakes.

    In this case you would expect to see "attempt.class" listed among the files in your JavaStuff directory. If it not there then you have not compiled the program successfully.
    Hi there,

    I followed your instructions and my command prompt output was:

    Java Code:
    Microsoft Windows [Version 6.1.7600]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
    
    C:\Users\Coombes>cd \Users\Coombes\Documents\JavaStuff
    
    C:\Users\Coombes\Documents\JavaStuff>javac hello.java
    
    C:\Users\Coombes\Documents\JavaStuff>dir
     Volume in drive C has no label.
     Volume Serial Number is DC25-D911
    
     Directory of C:\Users\Coombes\Documents\JavaStuff
    
    21/02/2010  14:12    <DIR>          .
    21/02/2010  14:12    <DIR>          ..
    19/02/2010  15:36    <DIR>          .metadata
    19/02/2010  14:52    <DIR>          book
    20/02/2010  15:34    <DIR>          DesktopApplication1
    19/02/2010  15:40    <DIR>          Hello World
    19/02/2010  18:37    <DIR>          Hello World2
    21/02/2010  14:12               409 hello.class
    20/02/2010  16:39               107 hello.java
    19/02/2010  15:37    <DIR>          HelloWorld3
    19/02/2010  15:44    <DIR>          Helloworldlul
    20/02/2010  15:45    <DIR>          JavaApplication3
                   2 File(s)            516 bytes
                  10 Dir(s)  23,533,543,424 bytes free
    
    C:\Users\Coombes\Documents\JavaStuff>java hello
    Exception in thread "main" java.lang.NoClassDefFoundError: hello
    Caused by: java.lang.ClassNotFoundException: hello
            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: hello.  Program will exit.
    
    C:\Users\Coombes\Documents\JavaStuff>
    EDIT: Eureka, using java -cp . hello worked, I'm guessing that means I have a problem with my CLASSPATH setting.

    On another note what would I use to get keyboard input for a variable, that's not worded great but I basically want the equivalent of Python's variable=raw_input("Input variable: ") command in java.
    Last edited by mc6415; 02-21-2010 at 03:58 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mc6415 View Post
    EDIT: Eureka, using java -cp . hello worked, I'm guessing that means I have a problem with my CLASSPATH setting.
    pbrockway's a genius, isn't he? But I wouldn't change your system's CLASSPATH data but rather the cp info when calling java as you're doing above.

    On another note what would I use to get keyboard input for a variable, that's not worded great but I basically want the equivalent of Python's variable=raw_input("Input variable: ") command in java.
    Do you mean to dynamically set a variable's name during the running of a program? No, that's generally not done in Java. Instead you would use collections such as Lists and HashMaps to associated dynamically created objects.

  7. #7
    mc6415 is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    pbrockway's a genius, isn't he? But I wouldn't change your system's CLASSPATH data but rather the cp info when calling java as you're doing above.


    Do you mean to dynamically set a variable's name during the running of a program? No, that's generally not done in Java. Instead you would use collections such as Lists and HashMaps to associated dynamically created objects.
    Indeed I'm just gonna keep doing what's working and not play around with anything I don't need to until I'm comfortable with Java.

    Ah I see, I'm still very new to this and it's hard to shake off assuming things that will work from my work with Python, I'll get used to it in the end.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mc6415 View Post
    Ah I see, I'm still very new to this and it's hard to shake off assuming things that will work from my work with Python, I'll get used to it in the end.
    As I see things, there's a Python way and philosophy of programming and a Java way and philosophy. Both are different, and both are correct in their own universes. Welcome to the Java way.

  9. #9
    mc6415 is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    As I see things, there's a Python way and philosophy of programming and a Java way and philosophy. Both are different, and both are correct in their own universes. Welcome to the Java way.
    That's a good way of putting it and sums up my reason for learning Java which is I want to get a broader understanding of programming languages on the whole.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Indeed I'm just gonna keep doing what's working and not play around with anything I don't need to until I'm comfortable with Java.

    That sounds like a wise approach.

    Act as if the CLASSPATH environment variable doesn't exist. I'm no expert - but that just allows me to to state more vehemently - it should have been deprecated years ago. And then removed from java and javac so that people who relied on it (Apple etc) would be forced to fix their broken software.

    Hopefully you understand the explication I gave of the -cp argument:

    "-cp . " == "look for classes in the current directory"

    That provides the basis for more elaborate command line arguments as and when you need them.

    -----

    re Python's raw_input(). That's a way of obtaining a string from standard input. Java offers various ways of doing this, including one that's pretty much equivalent: the Console method readLine(). Details here.

    The reference is to Oracle's Tutorial which is a must read. (Well as much of it as you need: it's quite comprehensive). Nearby sections on scanning and character streams are also useful, but are presented in terms of files because, unlike raw_input(), they are not limited to standard input.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pbrockway2 View Post
    re Python's raw_input(). That's a way of obtaining a string from standard input. Java offers various ways of doing this, including one that's pretty much equivalent: the Console method readLine()....
    I was interpreting the OP's question as that he/she wanted to name the variable based on the input, but on re-reading it, probably I assumed incorrectly. :eek:

Similar Threads

  1. Java problem help!
    By wexgal in forum New To Java
    Replies: 3
    Last Post: 10-26-2009, 01:52 PM
  2. Java Problem. Need Help!
    By bob101 in forum New To Java
    Replies: 6
    Last Post: 03-19-2009, 05:34 AM
  3. JAVA and XML Problem
    By jackchang in forum XML
    Replies: 4
    Last Post: 02-22-2009, 09:28 PM
  4. problem with java nio
    By andrei stoiculescu in forum Networking
    Replies: 3
    Last Post: 02-02-2009, 04:35 PM
  5. Java problem
    By grend in forum New To Java
    Replies: 5
    Last Post: 08-19-2008, 12:44 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
  •