File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Running Java classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Running Java classes" Watch "Running Java classes" New topic

Running Java classes

Mark Morgan

Joined: Jul 19, 2005
Posts: 3
Note - I am an absolute Java beginner using Head First Java.

I installed the 1.5.0 JDK and assigned the relevant path environment variables.

I then created a simple Hello World .java code file (cutting the code from the JavaRanch beginners page). This file compiled sucessfully using the javac utility.

However when I come to run the "program" from the command line (i.e. java HelloWorld) an error message is returned:

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

I assume there is some configuration issue. Can anyone assist so that I can get up and running.

Thank you in advance.
Jacquie Barker
Ranch Hand

Joined: Dec 20, 2000
Posts: 201
Try this:

java -cp . HelloWorld

The -cp command-line flag instructions the Java Virtual Machine to look in a series of directories/jar files for the bytecode you wish to execute; a single period ("dot") tells it to look in your current directory.



P.S. You may want to check out my book, Beginning Java Objects -- I make sure to cover all of the nitty-gritty details liable to trip up newcomers to Java (like explaining the -cp flag), leaving nothing to chance!

[ July 19, 2005: Message edited by: Jacquie Barker ]

Author of Beginning Java Objects, Beginning C# Objects, and Taming the Technology Tidal Wave
Mark Morgan

Joined: Jul 19, 2005
Posts: 3
Unfortunately the error message is still being displayed as before, but thank you anyway for your consideration.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199


Welcome to JavaRanch!

In your haste to come in and ask a question, you seem to have missed reading our policy on display names, which quite clearly states that you must use a real (sounding) first and last name for your display name -- no joke names, "handles," or last initials are acceptable. You can fix your display name here. Thanks for your cooperation!

As to your question: if you've successfully compiled, you ought to have a file named "HelloWorld.class" in your working directory -- i.e., if you say "dir" or "ls" or whatever is appropriate for your platform, you should see this file. If you don't, but you can find the file somewhere else, change into that other directory, and do what Jacquie says. If you can't find that file, then the "javac" step didn't actually work, so we need to back up a bit.

We're assuming your file contains a class named "HelloWorld". If your class name isn't the same as the file name, it's the class name that you need to use in the "java" command, not the name of the Java file, which is irrelevant.

[Jess in Action][AskingGoodQuestions]
Mark Morgan

Joined: Jul 19, 2005
Posts: 3
My user name has been updated forthwith.

Also the execution issues have been sucessfully resolved.

Much obliged.
I agree. Here's the link:
subject: Running Java classes
It's not a secret anymore!