-1

I am trying to run these programs, but I am getting

Error: "Could not find or load main class" 

Here's a screenshot of me trying to run the programs in cmd line:

Windows Powershell Screenshot:

Windows Powershell Screenshot

This makes no sense to me seeing as how the files compiled just fine, which would imply that the main class was able to be found.

If anyone could explain what's going wrong I would appreciate it very much, thank you.

The UDPServer code:

Server Code

The UDPClient code:

Client Code

devjoco
  • 425
  • 3
  • 15
  • Successful compilation does not necessarily mean that Main class is present in your code. – Zaki Anwar Hamdani Sep 19 '17 at 00:36
  • 1) Do at least a basic search for the error message before posting here. I found the duplicate I linked in about 3 seconds. 2) Do **not** post images of your code. Code in images is useless; see [this Meta post](https://meta.stackoverflow.com/a/285557/62576) for a list of the many reasons to avoid doing so. – Ken White Sep 19 '17 at 00:56
  • Don't post pictures of text here. Post the text. – user207421 Sep 19 '17 at 00:57

4 Answers4

0

It would be useful if you post your code here. But if I try a shot in the dark I would say that your filename doesn't match the classname in your Java file.

0

When running javac you pass in the paths to files you want to compile - hence those files are implicitly on the classpath. When running java you aren't passing in any files explicitly, so you have to include the current directory in the classpath in order for the JVM to know to look there.

$ javac Foo.java
$ java -cp . Foo

I use this Bash function pretty often for quick JVM/JDK experiments, if you want to try to replicate it in Powershell.

It's one of those cases where the current working directory probably should be on the classpath by default ~90% of the time, but it can't be in order to accommodate that last 10%. (Whether that's a good design decision or not is debatable, of course).

dimo414
  • 47,227
  • 18
  • 148
  • 244
0

You are:

  1. In the wrong directory. You should be in the directory that contains serverClient.
  2. Using the wrong commands. They should be:

    javac serverClient/*.java
    java serverClient.UDPServer
    java serverClient.UDPClient
    
user207421
  • 305,947
  • 44
  • 307
  • 483
0

You have defined a package serverClient; at the top of both files.

So you should be having a directory named serverClient with your .class files.

if you wish to execute using java command line, you should execute from the src directory like this

PS ...\Programming Assignments\src > java serverClient.UDPServer

PS ...\Programming Assignments\src > java serverClient.UDPClient

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78