blog

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

File and Class Name different of a Java program.

Devesh Kumar Singh2240 28-Oct-2015

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”.

File and Class Name different of a Java program.

 

 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

 

File and Class Name different of a Java program.

 

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.

 

File and Class Name different of a Java program.

 

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

 

File and Class Name different of a Java program.

This will give the output of your program. 

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

File and Class Name different of a Java program.

 


Updated 13-Mar-2018

Leave Comment

Comments

Liked By