aspose file tools*
The moose likes Beginning Java and the fly likes Terminal Error when Loading .class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Terminal Error when Loading .class" Watch "Terminal Error when Loading .class" New topic
Author

Terminal Error when Loading .class

Peter Caulfield
Greenhorn

Joined: Sep 27, 2010
Posts: 10
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
Al Fraelich
Greenhorn

Joined: May 25, 2009
Posts: 20
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

Joined: Sep 27, 2010
Posts: 10
Hi Al,

Thanks for the reply and the welcome! I have tried both commands and returns exactly the same error.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
...
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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


SCJP6.0,My blog Ranchers from Delhi
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Sep 27, 2010
Posts: 10
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 31, 2004
Posts: 11343

Please post your complete Terminal text.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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

Joined: Sep 27, 2010
Posts: 10
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Sep 27, 2010
Posts: 10
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

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Terminal Error when Loading .class