0

I am writing a basic TCP chat program and one of the requirements is that it can be run from the command line with the following argument formats:

java Server 8888
java Client localhost 8888

Which will start a server listening on 8888 and which waits to accept incoming connections from clients. Then start a client and connect to the server at localhost:8888. These classes can both be compiled and run from within Eclipse and I have added the above variables to the run configurations for the classes, respectively.

If I navigate to the directory of the files in CMD I can see the compiled .class files, but when I try to run the server with:

java Server 8888

I get the error

Error: Could not find or load main class Server

Eclipse>Window>Preferences>Java>Compiler shows JDK 1.7.

Running java -version from the command line shows

java version "1.7.0_02"
Java(TM) SE Runtime Environment (build 1.7.0_02-b13)
Java HotSpot(TM) 64-Bit Server VM (build 22.0-b10, mixed mode)

I want to be able to run both the classes from separate prompts in parallel. Any ideas?

asked Dec 23, 2012 at 20:26
4
  • You might like to check out the answer to this relevant question Commented Dec 23, 2012 at 20:29
  • Do you have your classes in a package name? Commented Dec 23, 2012 at 20:31
  • Then you need to specify basicChat.Server not just Server Commented Dec 23, 2012 at 20:33
  • Do your classes have public modifier? And if they do - can you show us some minimal code that gives the same error? Commented Dec 23, 2012 at 20:42

3 Answers 3

3
java -cp . basicChat.Server 8888

You need to specify the fully qualified class name (including the package name)

Reason:

The class full name (known as fully qualified name) is not Server, it is basicChat.Server. The file is located under a directory named basicChat. So java is looking for a a directory structure matching the package name. The Server.class file is located under the basicChat directory in the file system.

Else consider how we would have a problem to select the intended class if you had several classes called Server in different packages (name spaces).

answered Dec 23, 2012 at 20:34
Sign up to request clarification or add additional context in comments.

3 Comments

Because your class's full name is not Server, it is basicChat.Server. The file is located under a directory named basicChat
But my current directory was basicChat when I ran java Server 8888?
That doesn't work, need to be a the root of your packages. Else in your case it would be looking for a class named Server, but it found one named basicChat.Server. Again classes are identified by the full name and you specified a class with an empty package.
1

Are you certain that in both these classes you have main method?

public static void main(String[] args) { ... }

And do your classes have public modifier?

if you have this class in a package e.g.: test.Server you'll run it from bin folder like that:

java test.Server 8888

Compiling this class:

public class Server {
 public static void main(String[] args) {
 System.out.println(Arrays.deepToString(args));
 }
}

and running:

java Server 8888

gives output:

[8888]
answered Dec 23, 2012 at 20:33

1 Comment

Yes, I'm positive they do. As I said, they both compile and run in Eclipse. I tried running it from bin using the complete package name as well, same main class not found error.
0

Your CLASSPATH is incorrect. Try the following (on Windows) from bin directory:

java -cp .;%CLASSPATH% basicChat.Server 8888

Also, you can run your Server in Eclipse. Create a run configuration for it, specify a port in arguments. Then you can launch it in Eclipse, it will be running util you stop it or close Eclipse.

answered Dec 23, 2012 at 20:30

3 Comments

Error: Could not find or load main class Files\Java\jre7\bin\java.exe
what if you just try java -cp Server 8888
Error: Could not find or load main class 8888

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.