-1

Getting an error for executing the java file with command java ClassFileName as below:

Error: Could not find or load main class.

but executing when using a command java -cp . ClassFileName without any error.

Can u please help me why it is happening. ?

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38

2 Answers2

0

You must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath, which you've done and that's why it's working fine for you.

So, to execute using java ClassFileName just add the location of .class to your classpath. I hope it helps.

0

Complile a Java file to generate a class:

javac filename.java

Execute the generated class:

java filename
Loan
  • 142
  • 1
  • 16