• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why won't my program compile?

 
Gareth James
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
verClient/Main)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown 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?

Regards
Gareth


MY CODE IS AS FOLLOWS:

(MAIN.JAVA)




(CLIENT.JAVA)




(SERVER.JAVA)

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gareth James
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to type the fully qualified name of the class, yes.
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/Main.java
Later on if you say something like it would be expected that the file would be packaged inside directories structured like serverClient/foo/bar/Main.java
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic