CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@Sahithi Pallavi • 17 Nov, 2009 • 1 like
My friend asked me this question.!

Do a java program runs without the main method? If NO, why? What is the need of main method there in the java program?

Guys..! tell me the answer. I dont know the reason, but I know that java program must needs main method..

CAn anyone tell me the reason?
And correct me If am wrong?
@faizaan • 17 Nov, 2009 Re: Do a Java program runs without the Main method?

My point of view is that,

If we create an applet in java than it can run without having main method,but if we develop an application than it do requires main method.

Reason why we require main method is because we write public static void main() i.e main method is a static method & so we can call it without creating object of that class . so when we compile (i.e javac filename.java) java compiler needs to use this main method without creating any object of that class.

please correct me if i am wrong.
@Manish Goyal • 17 Nov, 2009 • 1 like I think main() method is must in java ...
since java is pure object oriented language....and also main() method is an entry point...through which execution of program begins............
@sarveshgupta • 17 Nov, 2009 @pallavi: Faizan is right it is possible to have a program to start execution without entering into main first.

This is because main is always the entry gate for the program. but if we are able to provide any other initiation point there is no need of main

as in case of an applet in which there is no main method, it has init
@Sahithi Pallavi • 18 Nov, 2009 Thanks a ton guys...!

So, main method is must to run a java program...!
@Sahithi Pallavi • 19 Nov, 2009 guys..! My friend told me that we can write and run a java program without using the main method.
i.e By using static

she gave this example program...!

public void static( )
public class
{
public void static( )
{
System.out.println( "Without main method" );
System.exit(0);
}
}
@Manish Goyal • 19 Nov, 2009 • 1 like
sahithi pallavi
guys..! My friend told me that we can write and run a java program without using the main method.
i.e By using static

she gave this example program...!

public void static( )
public class
{
public void static( )
{
System.out.println( "Without main method" );
System.exit(0);
}
}
😕😕😕😕
have you execute it...is it running successfully
@sarveshgupta • 20 Nov, 2009 @Pallavi: try the code yourself as I am doubtful this program will run.

And I told you that Java Applet programs don't generally have the main method.

they have paint() and init() method.
@ms_cs • 20 Nov, 2009 The java program can run without main method with this static method
@sarveshgupta • 20 Nov, 2009
ms_cs
The java program can run without main method with this static method
But where is the function name in this program.

With which name you will save the file?
@Manish Goyal • 20 Nov, 2009 Yeah i am totally confused with this...where is the name of function
as static is the reserved keyword..how can you use it...
if this is possible.
then what would we say it,,,,it's feature or limitation??
@sookie • 21 Nov, 2009 • 2 likes If I am not wrong ms_cs mean to say by using 'static block' not 'static method' because for calling a static method, one need to have an entry point while 'static block' is automatically called whenever class is loaded in JVM. "Static blocks" declared in any Java class are always called before the main() method. You can also note that why static variables or methods are declared outside "main" method.

Let's see an example to avoid any confusions
STEP 1: Create a class like followng in any of the locations of your sytem. Let me keep it "I:\"

/**
 *
 * @author sookie
 */
public class ProgramWithoutMainMethod {
 static
{
    System.out.println("Look at me ! I am running without Main method");      
    System.exit(0);
    
}
}
STEP 2: Now open command prompt and go to"bin" directory of your installed JDK and enter following command. It compiles the program
javac I:\ProgramWithoutMainMethod.java
[​IMG]

STEP 3: Now let's simply run the program.
java -classpath I:\ ProgramWithoutMainMethod
[​IMG]

Now you all might be wondering that why do I need to add this additional line "System.exit(0); " right? See, as soon as my "static block" reaches its closing brace, it tries to search for main() method which as of now you all now is must for running a Java program so in order to stop my program from looking for main() method, I would simply call System.exit(0); and terminate the entire Java program. So I am not giving it a chance to look for any main() method.

Hoping now you all might be cleared about the things discussing over here.

PS: Before directly asking questions [specially related to Java] - A tip try to search web, it is having so much information that you will learn 100 other things with only 1 question. 😀
@Manish Goyal • 21 Nov, 2009 thanks sookie ........for clarifying this concept
@ms_cs • 21 Nov, 2009 sookie: you are right..nice explanation
@accoolaryan • 30 Nov, 2009 yes we can run a java program without a main method .as main method is necessary but if we use a block named static ,every statement in it will be executed without main
try it
but it can give a n error so use System.exit(0) at the end of the file
as
public Nomain{
static{
System.out.println("printing without main");
}
System.exit(0);
}
@rama_krish627 • 30 Nov, 2009 107627@ccc108:~> javac crazy1.java
crazy1.java:1: 'class' or 'interface' expected
public void static( )
^
crazy1.java:2: <identifier> expected
public class
^
crazy1.java:9: '{' expected
}
^
3 errors

These are the errors in this program.
can you tell me is it working properly?
If yes tell me how.
@sarveshgupta • 30 Nov, 2009 ya nice explanation sookie
@sookie • 30 Nov, 2009
rama_krish627
107627@ccc108:~> javac crazy1.java
crazy1.java:1: 'class' or 'interface' expected
public void static( )
^
crazy1.java:2: <identifier> expected
public class
^
crazy1.java:9: '{' expected
}
^
3 errors

These are the errors in this program.
can you tell me is it working properly?
If yes tell me how.
Pardon my ignorance but I couldn't see where is your program crazy1.java ?

I am just assuming
You are using "class" keyword in your program and name of the file and class marked as public are same.
@techno_ishant • 01 Dec, 2009 java program can run without main()
For Example APPLET...😁
@rahulgakhar • 07 May, 2010 ya u can run a java program without main

try this

public class A
{
static
{
System.out.println( "Without main method" );
System.exit(0);
}
}
@naveen029 • 13 May, 2010 as you know,java achieves platform independency. so it uses Java Virtual Machiene(JVM) for platform independency.so java developer also connect main() method to this jvm.
so when we run a program JVM does not find any entry gate(i.e main() method) into the program.lets take example..suppose you have a house connecting 2 to three rooms but atleast u requires a entry gate. so it is also necessary to have a main() methos like main() (entry gate in to run your program)i.e from where JVM can start.
@rahulgakhar • 13 May, 2010 but my friend told me we can run without main..............
bt he doesnt know how...........
@Sahithi Pallavi • 13 May, 2010
rahulgakhar
but my friend told me we can run without main..............
bt he doesnt know how...........
yes, we can. By using static block.
Go through the previous posts of this thread.
@Amit Porwal • 18 Oct, 2010 Re: Do a Java program runs without the Main method?

We can run the program without using the main function in java......
and we can print any value any string....


if we want to print anythings in java without using main....
then we use the static block.....

with the use of static block program compile successfully but occur a run time error...
for avoiding these run time error we can use the System.exit(0);


Example............

class staticEx 
{
        static 
        {
                System.out.println("Inside Static Block");
                System.exit(0);
         }
}
AMIT PORWAL, BANGALORE(soft. eng.)


Moderator Note:: We recommend all communications to happen on CE!!
Please donot post email ids.
@crazy_raja • 08 Aug, 2011 class Hello
{
static
{
System.out.println("This is the Java Program That Runs Without Main Method");
System.exit(0);
}
}


Try this one....it works
@Ricky008 • 08 Aug, 2011
sahithi pallavi
My friend asked me this question.!

Do a java program runs without the main method? If NO, why? What is the need of main method there in the java program?

Guys..! tell me the answer. I dont know the reason, but I know that java program must needs main method..

CAn anyone tell me the reason?
And correct me If am wrong?
Yes, you can but do not count on it when you develop your application. In Java, a static initializer gets executed as soon as the class is loaded, even before the main method is called. Here is an example,

public class WithoutMain {
static {
System.out.println( "Hello World" );
System.exit(0);
}
}
The JVM finds and loads the class into memory when you run this code, the static initializer is executed during the loading and initialization of the class. System.exit(0) is called at the end of the static block to terminates the program. If not, then the JVM would have next used reflection on that class to find the main() method. If it does not find the main method, it throws an exception.
@karan20m • 09 Aug, 2011 if we dont write main() in the progrm then the compiler doesnt know where to start the program..
@gayathri anbu • 09 Aug, 2011 i have tried this.. it is not executing successfully..
@gayathri anbu • 09 Aug, 2011 You can write a runnable program which does not have main method at all. During run time jvm will search for the main method after exiting from this block. If it does not find the main method, it throws an exception. To avoid the exception System.exit(0); statement is used which terminates the program at the end of the static block itself.
[TABLE]
[TR]
[TD]class MainMethodNot
{
static
{
System.out.println("This java program have run without the run method");
System.exit(0);

}
}[/TD]
[/TR]
[/TABLE]

@karthikswapna1 • 20 Oct, 2011 yes of course the bellow program is executing
Sada
guys..! My friend told me that we can write and run a java program without using the main method.
i.e By using static

she gave this example program...!

public void static( )
public class
{
public void static( )
{
System.out.println( "Without main method" );
System.exit(0);
}
}
@vinci • 21 Oct, 2011 in reality this program wont work .my compiler gives me an error saying that main method not defined
@Sahithi Pallavi • 23 Oct, 2011
vinci
in reality this program wont work .my compiler gives me an error saying that main method not defined
will work, Vinci. Check once again.

And see this explanation,
If I am not wrong ms_cs mean to say by using 'static block' not 'static method' because for calling a static method, one need to have an entry point while 'static block' is automatically called whenever class is loaded in JVM. "Static blocks" declared in any Java class are always called before the main() method. You can also note that why static variables or methods are declared outside "main" method.

Let's see an example to avoid any confusions
STEP 1: Create a class like followng in any of the locations of your sytem. Let me keep it "I:\"

Code:

/** * * @author sookie */ public class ProgramWithoutMainMethod { static { System.out.println("Look at me ! I am running without Main method"); System.exit(0); } }
STEP 2: Now open command prompt and go to"bin" directory of your installed JDK and enter following command. It compiles the program
javac I:\ProgramWithoutMainMethod.java
[​IMG]

STEP 3: Now let's simply run the program.
java -classpath I:\ ProgramWithoutMainMethod
[​IMG]

Now you all might be wondering that why do I need to add this additional line "System.exit(0); " right? See, as soon as my "static block" reaches its closing brace, it tries to search for main() method which as of now you all now is must for running a Java program so in order to stop my program from looking for main() method, I would simply call System.exit(0); and terminate the entire Java program. So I am not giving it a chance to look for any main() method.

Hoping now you all might be cleared about the things discussing over here.

PS: Before directly asking questions [specially related to Java] - A tip try to search web, it is having so much information that you will learn 100 other things with only 1 question. 😀
@jeklin110 • 03 Nov, 2011 [TABLE]
[TR]
[TD="class: votecell"][/TD]
[TD="class: answercell"][/TD]
[/TR]
[/TABLE]
Hi,
This is just convention. In fact, even the name main(), and the arguments passed in are purely convention.
When you run java.exe (or javaw.exe on Windows), what is really happening is a coupleo of Java Native Interface (JNI) calls. These calls load the DLL that is really the JVM (that's right - java.exe is NOT the JVM). JNI is the tool that we use when we have to bridge between the virtual machine world, and the world of C, C++, etc... The reverse is also true - it is not possible (at least to my knowledge) to actually get a JVM running without using JNI.
----------
Struts2 Jquery
@jeklin110 • 03 Nov, 2011 Hi
Welcome to you good question asked by your friends i am finding answer for you..
---------
@balaji MCA • 01 Mar, 2012 Yes We can run the java program with out main method using static class name alone is enough to run a program in java sum operation only needs main clause
@csmentor • 16 Mar, 2012 Hi,
In order to run a JAVA program INDEPENDENTLY you need to have main() method. You can create java classes without main method but they can be used in another program but cannot be run independently.

Hope this helps.
@greatcoder • 16 Mar, 2012 Sada
I will tell u very conceptual answer.
1) Main method is a method just like any other method defined by us. Suppose you make a method to add two numbers: add()
2) U know the name of the method and can call it like add() and the program flow goes to add() method.
3) Just think what happens when we excute the Java Program.
4) We invoke the JVM and pass the name of the class to run
5) What will JVM do? It will create an instance of the class we passed to it and will call a method "main()". Thus There must be an entry point so that a program can begin executing and in Java such a method from which the execution will start is named as "main()" method. After all JVM must know the method which it should call to start program execution...

Hope this helps
Regards
GC
@priya.123 • 18 May, 2012
Sada
My friend asked me this question.!

Do a java program runs without the main method? If NO, why? What is the need of main method there in the java program?

Guys..! tell me the answer. I dont know the reason, but I know that java program must needs main method..

CAn anyone tell me the reason?
And correct me If am wrong?
-----------------------------------------------------------------------------------
You can run java program without main method with the help of static block
Because it executes as soon as class loaded.
Also memory is initialized to static block at the time of class compiled
But still we require main() method because static block is called automatically whereas we can call main() method as on our require meant also to take command line argument we need main() method
@Anoop Kumar • 18 May, 2012 Static block trick, no more applicable for JDK 1.7+😏
@Sahithi Pallavi • 19 May, 2012
ianoop
Static block trick, no more applicable for JDK 1.7+😏
Is it so? 😕
@ashie • 16 Jun, 2012 yes sada ...we can run java program without main...by using static block
if a static block is present in the program,then jvm execute it first,and then search for main method...
though if main method is not found..it gave an error..bt that we can eliminate by using system.exit inside block method
@KACHA PIYUSH • 17 Jun, 2012 // PAIN-KILLER SOLUTION OF ALL LOGIC PROGRAM
// SAVE IT Demo1 and this is successfully run my frd
public class Demo1
{
static
{
System.out.println("yeah sure painkiller");
System.exit(0);

}
}
@suraz • 19 Jun, 2012 • 2 likes Program without main() used to run earlier,as static block is executed first and within static block you write sop and then terminate using system.exit(0).
New version of java doesnot allow executing program without main()

in jdk 1.7(build1.7.0-ea-b85),It gives run time Exception.Check it out
@Sahithi Pallavi • 19 Jun, 2012 yeah, so is there no chance to run a program in jdk1.7 without main()?
@Ankit Niranjan • 01 Oct, 2012
Sada
guys..! My friend told me that we can write and run a java program without using the main method.
i.e By using static

she gave this example program...!

public void static( )
public class
{
public void static( )
{
System.out.println( "Without main method" );
System.exit(0);
}
}
but hw we can run this program??????
@Sahithi Pallavi • 01 Oct, 2012
Ankit Niranjan
but hw we can run this program??????
please go through the above posts. You will find the explanation.
@Extreme Java • 17 Mar, 2013 Yes System.exit() is the right thing to do at the end of static block.
@Rashmi Yadav • 18 Jul, 2014 • 1 like
Sada
My friend asked me this question.!

Do a java program runs without the main method? If NO, why? What is the need of main method there in the java program?

Guys..! tell me the answer. I dont know the reason, but I know that java program must needs main method..

CAn anyone tell me the reason?
And correct me If am wrong?
In java program ,the program execution starts from main method.But in java program all code must be declared in class. to access class we have to create an object for this.But to start execution all of this we have to execute main method .so have declared class as public static in which main method is saved.and we declare this class as file name to access direct main method without creating object of class in which the main method is saved.
@Deepika Bansal • 19 Jul, 2014 Great information friends. Thank you all.
My question is that is there any specific problem field(s) to skip the main function and keep the program running using static block as stated above; because as per my view, we cannot have much of the versatility and complex functionality with the ystatic block as with the main function.
So is there any special field of using static block like this?
@Rashmi Yadav • 19 Jul, 2014
Deepika Bansal
Great information friends. Thank you all.
My question is that is there any specific problem field(s) to skip the main function and keep the program running using static block as stated above; because as per my view, we cannot have much of the versatility and complex functionality with the ystatic block as with the main function.
So is there any special field of using static block like this?
If you want to execute a program you must include main method.if main method is not included in the program then the program will not execute and javac will give error.and there is nothing to replace the main method.you have to include main method becoz main method is the reason of execution .you cant skip it with any other thing.
@madhuri thalluri • 24 Jul, 2014 hello friends..
I tried the above code in ecllipse..bt its showing error as follows
so plz hlp me out..
@Anoop Kumar • 24 Jul, 2014
madhuri thalluri
hello friends..
I tried the above code in ecllipse..bt its showing error as follows
so plz hlp me out..
Check the following reply in this thread..
Anoop Kumar
Static block trick, no more applicable for JDK 1.7+😏
@Divisha Madupalli • 12 Jan, 2020

Yes, Java program can run without main method:

You have to use static blocks like :

public final class Test {    
static 
{       
 System.out.println("FIND");
}
}

o/p: FIND

This works with all the versions before JAVA 7.

@Karshil Sheth • 20 Jan, 2020 • 2 likes

Yes of course it is possible. You can use static blocks for that. Just put the code in the static block and execute it. The process by which java compiler executes the program is as follows:

  1. JVM loads your class.
  2. After arranging all the blocks and it runs static block.
  3. It then finally stirs across the main() method and it uses it. 


But for directly running through command line using "javac" its not possible. As JVM uses the main method to execute your program through command line and it can't find one. so its not possible in command line but possible via static block while running from idle.

@Mohana Chandra • 26 Jan, 2020

Yes, by using static blocks we can write a java program without main method.But the point to be remember is that is this works with all the versions of before Java 7.

6.9k views

Related Posts

@Nandan Rao · Jul 23, 2014

hi everyone, i need to rotate a horizontal disc which has bevel gear, the gear ratio of which is 1, the mass of disc along with its peripherals is around...
5.5k views

@Kaustubh Katdare · Aug 13, 2015

The Indian Engineering Services exam aka IES age limit is 21 to 30 years. Any aspiring candidate who wants to take the IES exam will have to be of or...
7.8k views

@n.kirubhakaran · Oct 27, 2012

How can we diff four wheeler's petrol & diesel engine by visually....
7k views

@Kaustubh Katdare · Aug 6, 2013

Resistive RAM aka RRAM is all set to kick the pulp out of NAND drives aka your regular flash drives. Crossbar, the chip maker that's been in the stealth mode...
7.4k views

@jbozee · Feb 13, 2009

am a student. and i need to know if it is possible to control a ceiling fan with any bluetooth device (e.g a mobile phone). and how can this be...
9.6k views