0

I have this code:

package com.myjava;

public class MyClass {

    public static void main(String args[]) {

        System.out.println("Hello World");

    }

}

Then

javac MyClass.java

Then

java com.myjava.MyClass

But throws

Error: Could not find or load main class com.myjava.MyClass

Why is it throwing that error, what could be wrong in the process?

quarks
  • 33,478
  • 73
  • 290
  • 513
  • 2
    This command will work only if you run it from the directory containing the `com` directory. If not, you need to ad it to the classpath: `java -cp path/to/directory/containingcom com.myjava.MyClass` – JB Nizet Aug 11 '19 at 22:14

2 Answers2

0

You don’t need to specify the package information when running the class so it would just be:

java MyClass

As long you are in the right directory it will work, if you are not, you must specify either the absolute file path to the class file or the relative path from your current location.

So if you were in the root directory of the package it would be:

java com.myjava.MyClass
BeyondPerception
  • 534
  • 1
  • 6
  • 10
0

It looks like you are running

javac MyClass.java

from com\myjava context where MyClass.java is located, and then running

java com.myjava.MyClass

which would be com\myjava\com\myjava\MyClass.class, which doesn't exist.

Hope that helps!