How to Define or Change Default Main Class in Java NetBeans

This article related to the jFrame class for Execution time to define as default. By Using Netbeans for Java programming, there is simple process to Define or Change Main Class in Java any time.

Suppose, When I was designing a GUI Swing application in java through NetBeans, Suppose, we add two jFrame class, First jFrame class has designed as entry form and Second has designed as Login class. And I Want to define Login Form class as main class in first priority. But, because we first use Entry Form jFrame, then during run time it default show this as Main Class in Java.

So, for this solution, SKOTechLearn describe that how to Define or Change Main Class in Java NetBeans.

How To Set or Change Any jFrame as Main Class in Java?

Here we will learn very interesting point that we can change main class with two simple ways.

  • Change Main Class in Java at Design Time in NetBeans
  • Change Main Class from Jar file After Build jar

Design or Add jFrame to Create Swing GUI Project in Java NetBeans

Change Main Class in Java at Design Time in NetBeans :

During design time, you just have to change some settings as described below:

  If you create a Project or design application in NetBeans, just go to “Projects”Tab in NetBeans.

  After that find your Project and target to right click and find “Properties” option from drop down list and click on it.

  You will find that, “Project Properties” window will display including your Project Name.

  In this Screen there is “Categories” tree view box containing many option. In this Tree view find “Run” option and select it. 

  Now, we will proceed in Changing Main Class in Java Process. So, Find “Main Class:” option. Press “Browse…” Button related to “Main Class:” option.

  Now “Browse Main Classes” Dialog Box will show. In this dialog box, you will find the number of class related to your project will be listed. Just select any one class which you want to define as Main class for your Project.

So, finally, the time to Learn through image description, which are described below.
Main Class Setting in NetBeans

(Simple Tips)How to Open other jFrame through existing jFrame in Java?

So, in this way you can easily change default main class in Java NetBeans.

 Change Main Class from Jar file After Build jar (Executable Jar File) :

Suppose, you have created your jar file or you have to build jar. For Some reason, you want to change the main class. So, what will you do? The answer is given in following way with step by step process.

  Go to the jar file’s location, and find your application’s jar file.

  Now, right click on jar file and open it as “archive”. I am using 7-Zip to open as archive.

  Jar archive contain two folder as “META-INF” and Project name folder. Go to “META-INF” folder and Extract “MANIFEST.MF”file from “META-INF” folder.

  Open “MANIFEST.MF” file in Notepad. In the last line of Notepad, you will find “Main Class:” containing project name with default class like bellow:

eg.: Main-Class: simple_java. Simple_Java_App

(Suppose, you have added two class on your application like: “TestJFrame.class” and “Second_Jframe.class”. And you want to make “Second_Jframe.class” as default main class.)

  So, replace “Simple_Java_App” with “Second_Jframe” like : “ Main-Class: simple_java.Second_Jframe” from Notepad. After that, save Notepad.

Now, If there is complication to understand this process, you can follow the image indication describe bellow.
Change Main Class through Jar MANIFEST
Change Main Class Through Jar File

  Next step to drag your modified “MANIFEST.MF” file and drop on your jar’s archive’s as “META-INF” folder. Then close archive like bellow.

Modified MANIFEST MF file save in Jar
Drag Modified MANIFEST.MF file on Jar's Archive

  Finally, Run jar file, you will see that your jar file will be execute with defining or modified main class.

How to Change any Class as Main Executable Class in Java?

So, the above given two process where you can find that how to Define or Change Main Class in Java NetBeans with Complete description through SKOTechLearn.

Also Read another Interesting Tips:

Auto Resizing or Auto Adjust Controls in Java Swing

jLabel Using and Designing Settings and Tips

1 comment: