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?
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.
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
javac <filename.java>, i.e.
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.
java <classname>, i.e. java
give the output of your program.
If file name
is made to execute, it will throw an error.