Skip to main content

Saving A Java Program


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.

Comments

Popular posts from this blog

Android Swipe Views with Tabs

In this post we are going to learn about how to integrate the android tab view with the fragments using ViewPager and ActionBar class. For displaying the tabs on the top of the screen you need to interact with the android action bar, this is because the tab views is connected with the action bar. Ads by Google In this example application we make three tabs called "java", "php" and ".Net" and there are three seperate fragement view for each of these tabs. First you need to add the ViewPager into the activity_main.xml file. <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.view.ViewPager> Now create three layout files for the fragments. 1. java_layout.xml <?xml version="1.0" encod

Android MySQL Database Operations

In this post we are going to learn about how to connect to MySQL Database from your Android Application and perform database operations. Here we create an android app that contain some login Activity through which the user can retrieve information from database and a registration Activity through which the user can add information into the database.  First you need to have the following components installed in your development machine.  1. Database : Here we use the MySQL database. 2. Web Server : Here we use the Apache Web Server. 3. Server side Scripting Language :   Here we use PHP for server side scripting. 4. Android Development environment : You must install android sdk and android studio.   I recommend you to download and install WAMPSERVER. The wamp server installer contains the following components. Apache Server Application MySQL Database PHP/phpMyAdmin First we have to create the database and table in MySQL. You can use the phpMyAdmin for mange yo

"please correct the errors on this form" adsense error simple solution

Many of the bloggers now facing the problem with their adsense widgets. Whenever try to add the new  adsense link unit using the blogger widgets, it shows the error " please correct the errors on this form " as shown bellow. In blogger you can add the adsense units in two ways. First one is adding the adsense using the blogger widgets(Most of the beginners doing this) and the second method is to obtain the adsense code from the adsense login page and place it into the targeted position on the blog. To improve the revenue from adsense you have to place the appropriate ad unit into the right position. The adsense link units are very important for increasing the adsense revenue. So due to this error many of the bloggers are unable to place the link units.  Here is the solution for the problem. 1. Login into your adsense and blogger account. 2. Now make sure that only two adsense widgets (units) are present on your blog, this is because Google allows onl