---
title: "File and Class Name different of a Java program."  
description: "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 “YE"  
author: "Devesh Kumar Singh"  
published: 2015-10-28  
updated: 2018-03-13  
canonical: https://www.mindstick.com/blog/10971/file-and-class-name-different-of-a-java-program  
category: "java"  
tags: ["java", "file", "class"]  
reading_time: 2 minutes  

---

# File and Class Name different of a Java program.

If the [file name](https://www.mindstick.com/interview/34109/how-do-you-generate-a-unique-file-name-to-avoid-overwriting-an-existing-one) and [class name](https://www.mindstick.com/forum/12871/how-to-get-class-name) of a java program is different and you have to [execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) the program using [Command Line](https://www.mindstick.com/forum/158085/how-do-i-pass-command-line-arguments-to-a-node-js-program), 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](https://answers.mindstick.com/qa/33494/how-do-you-find-any-view-element-into-your-program-explain-for-example) 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](https://www.mindstick.com/interview/33814/what-will-be-the-output-of-following-program-code) is “HelloWorld”.

![File and Class Name different of a Java program.](https://www.mindstick.com/blogs/08ebb0bb-64da-4e65-b3b5-4f3529b4c8af/images/25b8e09b-fc02-44a5-9013-56963aec71f9.png)

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.](https://www.mindstick.com/blogs/08ebb0bb-64da-4e65-b3b5-4f3529b4c8af/images/35276ff7-e7b7-4703-8ebf-9750d443e2f6.png)**

This will compile your program code and if no error is found, it will generate a bytecode file with [extension](https://www.mindstick.com/articles/22/extension-method) .class. In this case it will generate **HelloWorld.class** file in the same folder where your java program [file is stored](https://answers.mindstick.com/qa/96834/how-to-find-where-a-file-is-stored-in-linux).

![File and Class Name different of a Java program.](https://www.mindstick.com/blogs/08ebb0bb-64da-4e65-b3b5-4f3529b4c8af/images/88aa1c0d-818e-4b3a-89ab-c0fa7ed57ea7.png)

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.](https://www.mindstick.com/blogs/08ebb0bb-64da-4e65-b3b5-4f3529b4c8af/images/a9739601-b31b-4bd6-bc30-edf338f257cd.png)**

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.](https://www.mindstick.com/blogs/08ebb0bb-64da-4e65-b3b5-4f3529b4c8af/images/d277a0ed-cf30-44bf-afc6-dea4e727d3ed.png)**

---

Original Source: https://www.mindstick.com/blog/10971/file-and-class-name-different-of-a-java-program

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
