How Can You Run Java Program Without main Method?

Connect with

Oracle JavaHow can you run Java Program without main method? Answer can be Yes or No, it depends upon the environment, where you want to run and what?

This question can be asked for all level of interview irrespective of 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 main method or where you can not run without main method. Broadly, you can classified into 2 main group: 1. Managed environment and 2. Non-Managed environment.

1. Managed Environment

Broadly, there are following environment which comes under managed environment. If you consider for managed environment and asking whether you can run java class without main method, answer is YES. here, answer is YES , then how it can be, due to its life-cycle methods.

1.1 Servlet:

Servlet is also a java class and it usually run in a managed environment by servlet container, where servlet container managed all the life cycle methods.

  • 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 in web.xml file e.g.

    1 
    
  • service() and
  • This method gets call on every request to fulfill the request.

  • destroy():
  • this gets call by servlet container when servlet container not required further.

1.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()

1.2 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.

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 way to invoke main method of java class for different development environment.

package com.mysoftkey;

/**
 * This class is used to test to invoke main method via 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.");
	}

 }
}

2.1 By using Java command line

 java MainClazz
 

2.2 By using Maven

maven without parameters

 mvn exec:java -Dexec.mainClass="com.mysoftkey.MainClazz"

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

 main method invoked, Param(s) : 
  ranjeet
  rakesh
 

2.4 By using executable jar file

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.


Connect with

Leave a Reply

Your email address will not be published. Required fields are marked *