• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Terminal Error when Loading .class

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just started Java and i'm getting an error when i try to load my first .class.

I was able to compile the HelloWorld.java from the terminal giving me a HelloWorld.class

But when i try load it from the terminal with the following commands:

java HelloWorld
java HelloWorld.class

I got the following error:


Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
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)

Any ideas?

Cheers

Pete
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a common mistake for people just starting out. It usually results from typing the first letter of the file then hitting tab and letting the cmd fill in the rest, which includes the .class at the end. To make it work, don't put the .class on the end of the file for example

DON'T java HelloWorld.class

DO java HelloWorld

subtle difference, thats it.

Good luck, and welcome to Java!

Al
 
Peter Caulfield
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Al,

Thanks for the reply and the welcome! I have tried both commands and returns exactly the same error.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is that you have set a system classpath that does not include a dot to indicate the current directory. In Terminal, change the directory to the location of your class file. (You can verify that your class file exists in that directly by typing "dir" to list the directory's contents.) Then type...

>java -cp . HelloWorld

If this works, then that is the problem. (The "-cp" is for specifying a classpath option, and the dot indicates the current directory. So you are telling java to look in the current directory for the class file.) Let us know if that's the issue.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Al Fraelich wrote:... DON'T java HelloWorld.class

DO java HelloWorld...


Good instinct -- but in that case, the error would have been...

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/class
Caused by: java.lang.ClassNotFoundException: HelloWorld.class
...
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think if he is using a windows then it(dot) will automatically get added.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shanky Sohar wrote:i think if he is using a windows then it(dot) will automatically get added.


Regardless of OS, the current directory is Java's default to look for classes, unless a classpath is explicitly set. So if no classpath is set, then it's automatic. But a common mistake is to set a classpath (which is often a mistake in itself) and not include the dot.
 
Peter Caulfield
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc,

Thanks for the reply. I get a "command not found" for the dir command :?

This is after i have used

cd "directory_for_my_java_source_code"

to change the directory.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pete, it occurred to me that since you're using a Mac, you probably didn't have to do any installation or configuration for Java, so you probably didn't make the mistake of setting a classpath.

So now I'm thinking the issue might be changing the directory. Remember, Mac OS X uses a forward slash (not a backslash) as a directory separator. If you're having trouble changing directories, please post your complete Terminal commands.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post your complete Terminal text.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

marc weber wrote:

Shanky Sohar wrote:i think if he is using a windows then it(dot) will automatically get added.


Regardless of OS, the current directory is Java's default to look for classes, unless a classpath is explicitly set. So if no classpath is set, then it's automatic. But a common mistake is to set a classpath (which is often a mistake in itself) and not include the dot.


but for linux, i think this doesnot happen..we have to explicitly give the current directly as (dot)
 
Peter Caulfield
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my terminal code so far:

Peter-Caulfields-iMac:~ petercaulfield$ cd "/Users/petercaulfield/Desktop/Java Source Code"
Peter-Caulfields-iMac:Java Source Code petercaulfield$ dir
-bash: dir: command not found
Peter-Caulfields-iMac:Java Source Code petercaulfield$

So i changed the directory and then tried to list its content. As you quite rightly say i haven't done any configuration to the machine for java. Because it's built in i just checked the versions where up to date and downloaded the api documentation.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is beyond my bash abilities, but it looks like the issue is the spaces in your directory name, and I'm not convinced that using quotes when changing directories had the desired effect.

I think there is syntax for dealing with this, but using a directory without spaces might be the easiest way to get past this.
 
Peter Caulfield
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Spot on Marc. Changed the folder to java and then got:


Peter-Caulfields-iMac:Java petercaulfield$ java HelloWorld
Hello World from a Mac!

Success!

Thanks for all the help

Pete

P.S i should really brush up on my unix :P

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shanky Sohar wrote:

marc weber wrote:

Shanky Sohar wrote:i think if he is using a windows then it(dot) will automatically get added.


Regardless of OS, the current directory is Java's default to look for classes, unless a classpath is explicitly set. So if no classpath is set, then it's automatic. But a common mistake is to set a classpath (which is often a mistake in itself) and not include the dot.


but for linux, i think this doesnot happen..we have to explicitly give the current directly as (dot)



Things behave as Marc describes on all platforms, Linux included.
reply
    Bookmark Topic Watch Topic
  • New Topic