-3

While compiling a Java class from command line with javac, .java is used, however .class is not used while trying to run the main method in a class with the java command.

For example, for compiling a class named HelloWorld we use javac HelloWorld.java, however to run that same class (supposing it has the main method), we write java HelloWorld and not java HelloWorld.class. Why is this so? The generated .class file has the bytecode for the same class, but still only the name is passed.

1 Answers1

2

As the man page of java says:

SYNOPSIS

java [ options ] class [ argument...  ]
java [ options ] -jar file.jar

PARAMETERS

options    Command-line options.
class      Name of the class to be invoked.
file.jar   Name  of the jar file to be invoked.  Used only with the -jar option.
argument   Argument passed to the main function.

Notice, the class part, so you are passing a class name to java, not a class file.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
  • Thats right @meskobalazs, however my particular confusion is still related to why Java does not allow passing `.class` file with `java` command. I mean this `.class` file still has the bytecode for that same class whose name we are passing with the `java` command. – Shubhankit Roy Feb 09 '15 at 11:58