1

I recently have had a class that has me running java programs from the command line and I seem to have the problem of the cmd not finding the main class

I have installed java correctly, java and javac commands come up with the respective menus. No issues come up when I press javac UDPServer.java, however if I press java UDPServer, I get the error. Any suggestions?

import java.io.*; 
import java.net.*; 
import java.util.*;


class UDPServer { 
public static void main(String args[]) throws Exception 
{ 

InetAddress srvIP = InetAddress.getByName("192.168.1.3");

  DatagramSocket serverSocket = new DatagramSocket(5000,srvIP); 

  byte[] receiveData = new byte[64]; byte[] sendData  = new byte[64]; 

  while(true) 
    { 
      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 
      serverSocket.receive(receivePacket); 
      String sentence = new String(receivePacket.getData()); 
      InetAddress IPAddress = receivePacket.getAddress();  
      int port = receivePacket.getPort(); 

            Calendar rightNow = Calendar.getInstance();
              sendData = rightNow.getTime().toString().getBytes();

      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); 
      serverSocket.send(sendPacket); 
    } 
} 

}

Also, another quick question: I am running an Ubuntu VM and I have to run a Server.java program on my host and a Client.java on my VM. When I try to configure the IP addresses. I'm not sure what to put, there are two: Ethernet adapter Virtual-Box and Ethernet Adapter Local Area Connection. To connect the two programs, which should I use?\

Thanks in advance

9
  • Can you copy and paste the error message? Commented Sep 2, 2013 at 16:47
  • I can type it out, sorry if the formatting is off: Commented Sep 2, 2013 at 16:49
  • C:\User\Desktop\CS455 javac UDPServer.java C:\User\Desktop\CS455 java UDPServer Error:Could not find or load the main class Also, the UDPServer.class is in the file structure, idk if that's relevant Commented Sep 2, 2013 at 16:50
  • Try java -cp . UDPserver - it's possible you need to put the current directory in the classpath. Commented Sep 2, 2013 at 17:06
  • Can you run any other Java files from the command line? Commented Sep 2, 2013 at 17:07

3 Answers 3

0

It's very possible that you have a package name specified in the java source code. In that case you need to specify that when you run the class.

Sign up to request clarification or add additional context in comments.

Comments

0

it is difficult help without detailed information of your problem. However, check if the name of the main class you are trying to run is exactly the same as the name of the filename.

Comments

0

I know this is late response and may not be relevant anymore.

First off, you can't run a *.java file so you need to compile it into a class file and that way run it.
For detailed info on how to use javac in console see: javac
To explain further into this a *.java file is simply a text file and needs to be compiled into java machine code (*.class). to finally wrapping your whole program together you export into an archive known as *.jar which is java's equivalent to *.exe

But to address a few other flaws in your code:
1. It is moverkill to import java.io.*, java.net.*, java.util.*
For the correct way of doing this just import the packages/classes needed and leave the rest out.
2. I understand that your class is using cmd and javac to code in java but this have no practical implications other than if you are making programs who needs to use javac to compile.
There are a wide range of compilers and editors you can use who will automaticly compile and help you debug your code, just a few: Eclipse, netbeans or blueJ

Comments

Your Answer

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.