Before saving a program in java, just keep the following points in mind
1. Every java program must be save in a file with .java extension.
2 .If the program contain only one class then you have to save the program in any appropriate name as you like. Consider the following example.
class SavingDemo
{
public static void main(String args[])
{
System.out.println("Display from SavingDemo class");
}
}
In the above example you can see that only one class is present, so it is possible to save the above program with any file name.
Suppose i save the above program with file name MySavingDemo.java. For compiling this file i use the following command.
javac MySavingDemo.java
You can see that the program is succeessfully compiled using this file name. So now it is the time to run this program. For running this program i use the following command.
java MyDeviceDemo
Here you can see that, the jvm will generates an error "Could not find or load main class".
Suppose i save this program in the following path of my computer. "D:\Java Programs"
By examining folder which contain this program you can see that there are two files are present, first one is the java source code (MySavingDemo.java) and second one is the class file or bytecode (SavingDemo.class.) This will reveal that when you compile a java program that contain only one class, the java bytecode is generated in the name of the class that contains the main method. In this program there is only one class is present with name SavingDemo and it holds the main method.
So here the byte code is generated with the name of the class SavingDemo.class. So inorder to run this program you must run it using the name of that class.
So here i am going to run the program using the command java SavingDemo.
Now you can see that the program run successfully and the output is obtained.
3. If the program contains more than one class, then the program must be save in a file with same name as the public class.
For more details consider the following example.
class SecondClass
{
public String secondDisplay()
{
return "Hello form Second Class";
}
}
class FirstClass
{
public String firstDisplay()
{
return "Hello form First Class";
}
}
public class MainClass
{
public static void main(String args[])
{
System.out.println("Hello from Main Class");
System.out.println(new SecondClass().secondDisplay());
System.out.println(new FirstClass().firstDisplay());
}
}
I save the file with name MainClass.java
Here you can see that there is only one public class is present (MainClass) and that class contains the main method. It is not possible for a java program having more than one public class. Make sure that your program contains only one public class and that class contains the main method of your program.
Now i am going to compile and run this program using the following commands.
javac MainClass.java
java MainClass
4. For better programming practice always keeps the java naming conventions.
javac MainClass.java
java MainClass
4. For better programming practice always keeps the java naming conventions.
Comments
Post a Comment