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 compile from the terminal ok but error running from terminal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compile from the terminal ok but error running from terminal" Watch "compile from the terminal ok but error running from terminal" New topic

compile from the terminal ok but error running from terminal

Matthew Hartman

Joined: Jun 27, 2006
Posts: 2
Howdy (I've always wanted to say that (and mean it) from some strange reason),
I'de first like to say that this site is awsome, I love the theme and with all these java experts around, I am wondering if I can hang my hat for a little while (ok, enough with the silliness(SP?), I am aware that it can annoy some grumpy folks after a while :-)).
Ok, down to business. I have the JDK 5.0 release 7 and I have it installed in just one folder away from my lettered drive on Windows XP, for example; (F:\Java). When I edit source code through a text editor and then compile it through the terminal it seems to compile fine and I get a pretty little class file afterwords. However, the trouble comes when I try and run the program with the 'java' command. I get a weird error? message. It reads as follows:

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

Of course in this example I used helloWorld as the name of the class that I was trying to run. It's not the code, at least I don't think so because my Mac runs the same code on the same JDK (just a different release, release 5) perfectly. This is not just isolated to one incedent either, it happens every time.
Because of my problem (well not the entire reason, I love eclipse), I am using eclipse and ofcourse with eclipse, everything works perfectly but it just erks me that I can't run my classes/apps from the terminal on my wondows machine. I also have a windows laptop but I am affraid to edit the path variables on that machine as well for fear that I might screw it up in the same way. I am new to the Windows Environment, having come back to windows because of .NET around a year ago but other than that I am used to *nix platforms such as Linux and OS X so my knowledge of windows is slim. Does anyone know what's wrong? I tried to look up: java.lang.NoClassDefFoundError in Java in a Nutshell (latest edition) and I can't seem to find anything about it, as if it doesn't exsist?
Please excuse my ignorance, I am brand spanking new to Java but not to programming (some C (old school, lol I'm 25y/o, what am I doing talking about old school?), .NET, Python and ActionScript) If anyone could help, I would be very greatful and extremely happy. (I'll have my itch scatched, you know the one the is starting to sting a little.) No really, I would be very greatful for a fix to my seemingly small but notoriously big problem. Thanks in advance
You can contact me through this e-mail.

Yours Truly,
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
Most likely you have a problem with your CLASSPATH.

You need to make sure that the directory containing your .class file is in your CLASSPATH.

Try this

java -cp . helloWorld

Are you sure the name of the class is helloWorld?

Remember that you have to have the exact name.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15084

Hello Matt, welcome to the forums.

Try typing in "NoClassDefFoundError" in Google. You'll find that it's one of the pitfalls that almost all new Java programmers hit.

In addition to that, in my signature (below this message) there is a link to The Java Tutorial. It's a very useful online tutorial, and the "Hello World" example there explains exactly what this error means and what you have to do about it.

Have fun with Java.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Matthew Hartman

Joined: Jun 27, 2006
Posts: 2
Thanks guys,
Keith, so my workspace (whichever that may be) has to be named in my CLASSPATH?

Jesper, I am still going through the links in your signature. I still havent found anything on the issue at hand (but I have just looked through briefly.) I got it to work by putting my workspace or the folder containing my source and class file in my CLASSPATH variable, However, I had to delete the the package name in the source file in order for it to work.

I agree. Here's the link:
subject: compile from the terminal ok but error running from terminal
It's not a secret anymore!