0

I'm new to coding and I'm trying to run my helloworld script in the command prompt and it keeps saying it can't find or load the main class. I used eclipse to write the code and i can see the main class in the run configurations, but when i put that in it says the same thing. Here's the code.

    package helloworld;

public class helloworld {


public static void main(String[] args) {
    // TODO Auto-generated method stub
System.out.println("Hello World!");
//display the string
}

}

1 Answers1

0

When running java main class from the command line you want to go to root directory of the project and run the class using FQN (Fully qualified name). like so:

java helloworld.helloworld

This assumes that your project is in i.e

C:/eclipse/workspace/helloworld <- root directory of the project

and you execute the above 'java' command from that root directory

Points to make:

  1. Class name should be CAPITALIZED so HelloWorld or Helloworld

  2. Its Bad practice to have class name the same as package name. Its allowed just bad practice

EDIT:

Fully Qualified Name consists of all the packages in the tree + the class file you want to run.

So for example if the class you want to run from CMD is in:

C:/eclipse/workspace/helloworld/com/mysubfolder/folder/mainclass

Your root directory of the the project is in:

C:/eclipse/workspace/helloworld

So from that directory, you need to run the following command

java com.mysubfolder.folder.mainclass
Maciej Cygan
  • 5,351
  • 5
  • 38
  • 72