Learn how can you run Java Program without main method? Yes, or No, it depends upon the environment, where you want to run and what?
1. Overview to Run Java Program Without main Method
This question can be asked for all levels of interview irrespective of the number of years of experience. Whether you are freshers or experience this is very much important to know where you can run your application without a main method or where you can not run without a main method. Broadly, you can be classified into 2 main groups:
1. Managed environment and
2. Non-Managed environment.
2. Managed Environment
You have to understand managed environment first in order to understand to ‘run Java program without main method’. Broadly, there is the following environment which comes under a managed environment. If you consider for the managed the environment and asking whether you can run a java class without main method, the answer is YES. Here, the answer is YES, then how it can be, due to its life-cycle methods.
2.1 Servlet: Managed environment
Java Servlet is also a java class and it usually run in a managed environment by servlet container, where servlet container manages all the life cycle methods of Java Servlet.
- init(),
This method gets called by servlet container to load first time in memory. You can say when first request comes for the servlet on servlet container or on start-up if you have provided element web.xml
file e.g.
1
service()
andThis method gets call on every request to fulfill the request.
destroy()
:this gets call by servlet container when servlet container not required further.
In nutshel, Servlet is a mananged environment where we run java program without main method.
2.2 Applet: Managed environment
Ans: YES, In Applet environment you can run java code without main method i.e public static void main(String[] arg)
. Apple runs in browser and it have have following life-cycle methods, which controls their execution flow.
init()
,start()
stop()
In nutshel, Java Applet is a mananged environment where we can run java program without main method.
2.3 Midlet: Managed Environment
In Midlet environment also you can say YES, as this is too managed environment. Midlet run on mobile devices, Midlet has own life-cycle methods: startApp()
, pauseApp()
and destroyApp()
, to start, pause and shut-down mobile applications respectively. In nutshel, Midlet is a mananged environment where we run java program without main method.
2. Non-managed Environment
Answer is NO, for non-managed environment. First of all let us understand non-managed environment, here, non-managed environment referring which does not have any container or any life cycle to perform anything.
There are different ways to invoke the main method of the java class for a different development environments.
package com.mysoftkey; /** * This class is used to test to invoke the main method in different way. * * @author ranjeet.jha * */ public class MainClazz { public static void main(String[] args) { if((args != null && args.length > 0)) { System.out.println("main method invoked, Param(s) : " ); for(String s : args) { System.out.println(" " + s); } } else { System.out.println(" main method invoked without params."); } } }
In nutshel, this is non-managed environments Java class with main method, so you can’t run java program without main method.
2.1 By using Java command line
java MainClazz
2.2 By using Maven
You can run java program with main method using Apache Maven command.
2.2.1 maven without parameters
mvn exec:java -Dexec.mainClass="com.mysoftkey.MainClazz"
2.2.2 maven with parameters
mvn exec:java -Dexec.mainClass="com.mysoftkey.MainClazz" -Dexec.args="arg0 arg1"
you can add the following plugin
into the pom.xml
org.codehaus.mojo exec-maven-plugin 1.2.1 java com.mysoftkey.MainClazz arg0 arg1
2.3 By using Eclipse
In eclipse:
– select class i.e. MainClazz
– select ‘Run As’
– select ‘Java Application’ or ‘Alt + Shift + X , J
‘
– if you want any parameter ‘Run configuration’ -> select arguments tab and provide with blank space separated
Console output of main method
main method invoked, Param(s) : ranjeet rakesh
2.4 By using executable jar file
You can also run Java program with main method, by click jar file or invoke by mvn which one you suite you. If you want to know step-by-step process how to make executable jar file and run in your environment in order to fulfill your business requirement.
I hope you enjoyed this post about how can you run a java program without main method, visit Core Java tutorial for more blog posts.
Visit Oracle Java Official Site for more details.