If the file name and class name of a java program is different and you have to execute the program using Command Line, will it run?

The answer is “YES”, but the thing is that you have to “compile” your “file name” but when “executing or running” your program you have to give the “class name” to get the output. Let’s try to understand with the help of an example.

Suppose the file name is “FirstProgram” and the name of the class containing the program code is “HelloWorld”.


 Now, to run this program to get the output, first we have to compile the code to generate the corresponding “.class” file. For this we have to give the command:

javac <filename.java>, i.e. javac FirstProgram.java



This will compile your program code and if no error is found, it will generate a bytecode file with extension .class. In this case it will generate HelloWorld.class file in the same folder where your java program file is stored.



Next to run this program you have to give the class name along with the “java” command to execute the program. Like:

java <classname>, i.e. java HelloWorld


This will give the output of your program. 

If file name is made to execute, it will throw an error.


  Modified On Mar-13-2018 04:06:01 AM

Leave Comment