-2

I am trying to write hello world on java. My code is:

class myfirstjavaprog
{  
        public static void main(String args[])
        {
           System.out.println("Hello World!");
        }
}

And when I run java youtube.java on cmd I got this error:

Error: Could not find or load main class youtube.java

Aykhan Amiraslanli
  • 593
  • 2
  • 8
  • 22

4 Answers4

0

In Java the name of the public class has to be the same as the source file or you will get that error. Try changing the first line to class youtube.

lanceg
  • 86
  • 6
0

To compile the class java with cmd use:

  javac youtube.java 

But if you want to run it use this:

  java youtube

If you have error with command javac check your Environment Variables.

Abdelhak
  • 8,299
  • 4
  • 22
  • 36
0

Here's what you need to do:

Assuming your file name is youtube.java,

Run the following:

javac youtube.java

Once it compiles, it should generate a myfirstjavaprog.class file.

To execute this class, you need to run the following command:

java myfirstjavaprog

Hope this helps!

anacron
  • 6,443
  • 2
  • 26
  • 31
0

Usually that issue is when you try to run application on older JRE, that is compiled with newer Java version. Check your java version, then compile code according to it. If you are using Eclipse, you can change that on configure build path option.