Home > DeveloperSection > Blogs > File and Class Name different of a Java program.

File and Class Name different of a Java program.


Java Java  File  Class 
Ratings:
0 Comment(s)
 480  View(s)
Rate this:

File and Class Name different of a Java program.

 

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.

 


Don't want to miss updates? Please click the below button!

Follow MindStick