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 Why won't my program compile? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why won Watch "Why won New topic

Why won't my program compile?

Gareth James

Joined: Oct 02, 2011
Posts: 2
Hey guys I've been making a basic server client program which can be run from the same program by selecting 1 or 2 as the program launches. When I run Main I just get errors saying:

Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: ser
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at Source)
at Source)
at$100(Unknown Source)
at$ Source)
at$ Source)
at Method)
at Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

The java files are stored in directory c:\serverClient\ and I have compiled them using the following command and they compile fine:

javac *.java

Now once I try to run it it just don't work at all ... I'm running from a command prompt and I just cannot see what I've done wrong. Help please?






Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15094

Welcome to the Ranch.

The title of your post is "Why won't my program compile?" but what you actually mean is "Why won't my program run?" - it compiles without errors, as you've said yourself.

The error means that Java can't find your classfiles, because you've entered an incorrect command to run the program.

Your classes are in a package named serverClient, and you've put them in a directory named serverClient. So far, so good. To run the program, you must add the base directory, that is, the directory that contains the serverClient directory, in the classpath. You can do that with the "-cp" option on the command line. So, you should be able to run your program by a command like this:

java -cp C:\ serverClient.Main

Note the space between C:\ and serverClient.Main. The C:\ is the argument for the -cp option, and serverClient.Main is the fully-qualified name of your class (fully-qualified name = the name of the class including the package that it's in).

The forums here have a special feature to format code to make it easier to read. Put your code between code tags: see the UseCodeTags page for more info.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Gareth James

Joined: Oct 02, 2011
Posts: 2
Jesper I Salute you sir!

I got no idea why it's doing that .... and as for packages .... I have no package I just have the raw .class files (Server, Client and Main) in the c:\serverClient directory.

so everytime I want to do this I have to type the name of the package (in this case serverClient) then type .Main to run the main class?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

You have to type the fully qualified name of the class, yes.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10828

Gareth James wrote:.... and as for packages .... I have no package I just have the raw .class files (Server, Client and Main) in the c:\serverClient directory.

Package is nothing but a directory. When you say what you are effectively declaring is that the the file will be packaged inside a directory called serverClient like serverClient/
Later on if you say something like it would be expected that the file would be packaged inside directories structured like serverClient/foo/bar/

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
I agree. Here's the link:
subject: Why won't my program compile?
jQuery in Action, 3rd edition