0

i've tried "javac MyFirstJavaProgram.java" it succed but when it come to the next "java MyFirstJavaProgram" it could not found or load main class, what does it means? this is my syntax

public class MyFirstJavaProgram {

   /* This is my first java program.  
    * This will print 'Hello World' as the output
    */

    public static void main(String []args) {
       System.out.println("Hello World"); // prints Hello World
    }
} 
Rustam
  • 6,485
  • 1
  • 25
  • 25
Deva Adithya Rama
  • 1,385
  • 2
  • 9
  • 6

2 Answers2

0

You have to specify the classpath using -cp

java -cp YOUR_CLASSPATH_HERE MyFirstJavaProgram
David Brossard
  • 13,584
  • 6
  • 55
  • 88
0

Java has the concept of a "Classpath". The VM will search your class in this path. E.g. If your classpath wil say C:\MyCoolProject\classes then the VM will look for your class in this directory. (The vm also looks in additional system paths, JDK Folders, but for simplicity we can left these out).

So if you call javac, you get a class file. The VM can only execute class files. Next you call java but you have to specify -cp as the others mentioned. E.g. java -cp . MySuperClass. Here is an article about this topic: Running a Java Program from Command Prompt

morpheus05
  • 4,772
  • 2
  • 32
  • 47