0

I would like to run my Java program using command prompt but I got an error Main class can not be found or loaded. Here is my Java Class:

package date_time;

import java.util.Date;

public class Demo {

    public static void main(String[] args) {
        Date date = new Date();
        System.out.println("Date Time: " + date.toString());

    }

}

I put the Java file in folder: C:\Users\sduong\workspace\DateTimeDemo\src\date_time. And then I used javac to compile, then inside my folder there are two files: Demo.class and Demo.java. Then I use command: java Demo to run but it not work, I also tried java date_time.Demo, java -cp Demo, java -cp date_time.Demo but none of them worked. I set also my Path Variable already to: C:\Program Files\Java\jdk1.8.0_131\bin. Can anyone have an idea how to fix it?

Ock
  • 1,262
  • 3
  • 18
  • 38

1 Answers1

1

you have to Change to Directory C:\Users\sduong\workspace\DateTimeDemo\src\

and run the command:

 java -cp .\ date_time.Demo 
Jens
  • 67,715
  • 15
  • 98
  • 113