0

My java class files run in Eclipse but not in command line. I have tried all possible solutions. My code has the following structure:

Client_1/src/filedownload/Client.java

RMI_interface/src/filedownload/Hello.java

The Client.java file is dependent on Hello.java. filedownload is the name of package. When I compile using the following command, it works.

javac RMI_interface/src/filedownload/Hello.java Client_1/src/filedownload/Client.java

But when I execute the class file in the Client_1/src folder using following command, it does not work.

java filedownload.Client

The error displayed is

Could not find or load main class

I have tried many posts on stackoverflow but I am unable to solve it. I am using ubuntu.

The code structure is

package filedownload;
import ....
public class Client implements Hello, Runnable{
...other functions.....
public static void main(String args[])throws Exception{

}
}
user8109
  • 189
  • 1
  • 12
  • Do you set classpath? Take a look at these : https://stackoverflow.com/questions/16137713/how-do-i-run-a-java-program-from-the-command-line-on-windows https://stackoverflow.com/questions/35448151/java-command-line-could-not-find-or-load-main-class https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean – Emre Savcı Apr 25 '18 at 14:40
  • You should `java` in `Client_1/src/filedownload` not in `Client_1/src`. – Shanu Gupta Apr 25 '18 at 14:40
  • I tried from Client_1/src/filedownload but it did not work. – user8109 Apr 25 '18 at 14:42
  • Can you post the code of both classes? – Shanu Gupta Apr 25 '18 at 14:51
  • It is not possible to post the code since it is a project submission but I can share relevant sections of the code. – user8109 Apr 25 '18 at 14:54
  • Please share especially the main method. – Shanu Gupta Apr 25 '18 at 14:56

3 Answers3

0

Does your Client class have the main() method ? Where are the .class files after compilation (that is, what's the current directory you're executing the compile from) ? What's the current directory when you try to execute ? What's the classpath when you try to execute ?

Without all that info, there's little chance of anyone being able to get you going (but for the obvious advice of just setting up eclipse and doing everything from within eclipse - letting eclipse take care of all the nitty gritty detail).

(And the questions themselves suggest various possible points of failure in your scenario so look at it.)

Erwin Smout
  • 18,113
  • 4
  • 33
  • 52
  • my Client class has a main(). I am executing from Client_1/src folder. Class files reside in Client_1/src/filedownload and RMI_interface/src/filedownload. I am using the following command from Client/src folder :java filedownload.Client. – user8109 Apr 25 '18 at 14:47
  • From Oracle docs : "The default value of the class path is ".", meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the -cp command line switch overrides this value." Note "overrides this value". Check if you have a CLASSPATH variable set by trying "echo $CLASSPATH" in a terminal. – Erwin Smout Apr 26 '18 at 07:14
0

All your steps seems to be correct. You didn't share the Client.java code which has main method. Make sure you follow this main method syntax:

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

E.g. if you write main without args, it can't be found.

Shanu Gupta
  • 3,699
  • 2
  • 19
  • 29
0

You need to put your classes in a separate folder, separated from your sources.

javac -d bin RMI_interface/src/filedownload/Hello.java Client_1/src/filedownload/Client.java

(folder 'bin' must exist already) And inside folder 'bin' execute command:

java filedownload.Client
Blocked
  • 340
  • 2
  • 5
  • 20