wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes cannot run a simple Java app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cannot run a simple Java app" Watch "cannot run a simple Java app" New topic

cannot run a simple Java app

Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
I have 3 JREs installed and I cannot get a simple "Hello World" Java app to run from the command line.

I can, however, run it within Eclipse on the "console" panel at the bottom of the editor using the built-in "Run" ability.....

I can always compile just fine but I always get this error, regardless of which runtime I use or what type of app I try to run:

Exception in thread "main" java.lang.NoClassDefFoundError: Main/class

I also have this problem regardless of whether I'm on this Gentoo box or my Windows machine.....

I've read that it has something to do w/ the CLASSPATH env. variable but no matter what I put in the classpath, it doesn't help!

Currently, here on my Gentoo box my /etc/env.d/30java-finalclasspath reads:

...which I assume means that the classpath is equal to whatever the current directory of the currently-running java app would be?

This is a real drag, I don't understand why it's so needlessly complicated to simply run a Java class?

Thanks in advance!
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

It's not really needlessly complicated, but you do have to know what to do.
The important thing to note here is that the argument to the "java" program is the name of a class, not the path to a class file. Therefore

java Main

may be correct, but all the following are always wrong:

java Main.class
java /home/me/Main.class
java /usr/local/classes/Main

I can tell from the error message that you're trying the first of these three. Leave off the ".class" and you'll be all right.

[Jess in Action][AskingGoodQuestions]
Peter van der Linden
Ranch Hand

Joined: Sep 28, 2004
Posts: 46
Hi Vinnie,
Your debugging skills are right on the money!
The message "NoClassDefFoundError: Main" means exactly what you thought - that
the Java run time cannot find the class you are trying to run, in this case Main

And your diagnosis for fixing it was accurate too - you have to set the CLASSPATH
environment variable to point to the folder where your classes are. Often times for
beginning test programs, that will be "." i.e. the current working directory.

So what is wrong? Probably the way you are setting CLASSPATH. And that is not
a Java thing at all. It is a shell thing. The different shells use different syntax and
different files to set up their initial variables. The syntax you have is good for the
bash shell, but you will need to export the variable too, with a line like this:

You can type the command "env" to see the current values of your env variables.
Does CLASSPATH have the value you intended? If not, find out where the env
variables are set for the shell you are using. E.g. ~/.profile or .cshrc or whatever

I think that will likely fix your problem. You came 99% of the way just fine - just
need to get the shell thing right.

(author "Just java 6th Ed")

Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0131482114/ref=jranch-20" target="_blank" rel="nofollow">Just Java(TM) 2 (6th Edition)</a>
Peter van der Linden
Ranch Hand

Joined: Sep 28, 2004
Posts: 46
Ernest got it exactly right -- well done on a better diagnosis than mine Ernest.

Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
I'm not sure I understand, I'm going to have to have to alter the CLASSPATH variable every time I try to run a new java program? Will I have to keep appending names to the variable for each new app?

I left the .class off of the name and I get the same error, now with details!

...it says "wrong name" but that doesn't make any sense, it's the correct name of the file (HelloWorld.class).

Thanks guys!
[ September 28, 2004: Message edited by: Dirk Schreckmann ]
Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
BTW - I forgot to add; This app runs fine when I "Run" within eclipse - it prints out to the built-in console, as expected, what's the difference? Does eclipse automatically resolve the needed current CLASSPATH?
Peter van der Linden
Ranch Hand

Joined: Sep 28, 2004
Posts: 46
> I left the .class off of the name and I get the same error, now with details!

It looks like the same error, but it is a slightly different error.
For the last TEN years, I have been wishing that Sun would "special case" this
error, and print out a decent diagnostic. Instead they print out a diagnostic that
only makes sense if you already know what the error is (and hence probably won't commit it).

OK. Short story: your classpath is wrong for the Java package name you have chosen. It
(probably) does not contain the root of the package com.scientifik.SampleCLI
This is why it works in Eclipse, but not out of it. Eclipse knows where it has put packages
and knows where to find them.

You do not have to change your classpath for each program. It just needs to be set up
with all the places to start looking. I usually have about 2 or 3 paths in my classpath
corresponding to packages I am working with.

A full discussion of classpath is a bit beyond the scope of a typed answer here, but here is
the way to get it working simply. Get rid of the package statements in your code example.
Put everything in an anonymous package (i.e. don't use packages for your code) in the
current directory. Then it will work just fine. You may not be able to do this if you are
trying to compile someone else's code.

Then you will have to read up about how package names are reflected in classpath.
Or maybe someone can give a quick sketch here for Vinnie, or a better pointer.
It's p.103 in "Just Java 6th Ed".


Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
Not needlessly complicated, eh?

Anyhow, I'll look into it more I guess. I've been scouting for the proper tutorials for classpath configuration for 2 days now w/o anything that helped at all.

I really would like to use packages if I'm going to get serious as I'm fully accustomed to using the equivilent in .NET (namespaces). I'll see if I can't google this and find something coherent to help me along. So far, examples have been messy and ill-explained.

I wish I could afford your book, it got good reviews on Amazon! I just bought 2 Java books (core java 2 & core servlets/jsp) and I won't be able to spring for another for atleast a month or so.

Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
I followed some instructions, which basically didn't tell me anything I didn't know already....but now I get the old error again.

Can you tell me if I'm on the right path (pun n/i).

Here's my new classpath:

...which is where my HelloWorld.class file is located.

and here's the new old error:

The first time around with this, the error said "HelloWorld/class" at the end...now it just says HelloWorld.

Peter van der Linden
Ranch Hand

Joined: Sep 28, 2004
Posts: 46

Do any of your existing Java books cover classpath? I would be surprised if not.

Basically, when you tell the compiler:
package a.b.c;
public class d {
public static void main(

Then the java runtime expects to see a file called:

You will start the program with the command
java a.b.c.d

And the classpath has to contain the directory:

In other words, the classpath is the list of roots where the runtime should start looking
for top level packages. Components in package names correlate with components in pathnames.
The Java spec doesn't require it to be like this, and environments like eclipse don't do
it like this, but this is the simplest implementation, and the majority (all?) command line
environments including Sun Java do it this way.


Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
Yep, by all means, I've been following the instructions correctly and still no good...

I've tried this w/ all 3 compilers/jre's installed (blackdown, sun, and currently, sun 1.5.0-RC)

Here's my code:

...my directory structure is such:

...my CLASSPATH is such:

NOTE: You're saying I could have shortened this to /home/vjenks/Projects or something like that and it would recurse to find classes below it in the hierarchy, correct? Regardless, this should have worked then...

...here are some commands and their results:

I'm entirely and completely stumped...I had no idea what pain was involved here!
wood burning stoves
subject: cannot run a simple Java app
Similar Threads
Question related to Classpath when using the java and javac commands.
Running JForum on a headless linux box (no X)
new to sockets/networking
NoClassDefFoundError when using -cp
JVM? Beginning question