aspose file tools*
The moose likes Features new in Java 7 and the fly likes can we develop java application without main method in jdk1.7? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Features new in Java 7
Bookmark "can we develop java application without main method in jdk1.7?" Watch "can we develop java application without main method in jdk1.7?" New topic
Author

can we develop java application without main method in jdk1.7?

surendra varma
Greenhorn

Joined: Feb 17, 2012
Posts: 1
In this post we are going to discuss whether is it possible to write a Java program without main method. Many people thinks that it is possible via applets but I am talking about standalone Java program.

And the answer is Yes. It is possible to write a Java program with out main method. Here is the sampe code.



Actually the funda is in Java program execution starts from static block. So I has placed my code in the static block { …. }. After the execution of the static block JVM searches for the main method. If the main method is absent it will throw main method not found exception. Hence I has terminated my program by calling the exit method so that no error messages are displayed.


The above program will not work from Java SE 7 onwards. In Jdk1.7 the above program will compile fine. But when you try to run it will give the followiing error.

Error: Main method not found in class StaticDemo, please define main method as:
public static void main(String[] args)

It seems that Java 7 specifications does not allow to execute a Java program without main method.

So the answer you have to tell is upto Java SE 6 it is possible to execute a Java program without main method. But from Java SE 7 onwards it is not possib
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

Interesting, but the crucial question is -- why would you want to do that in a real application?!
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

I was told that this is a popular interview question.
I was also told that you should stand up and run if ever asked such a question in an interview. ;)


Please correct my English.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Manuel Petermann wrote:I was also told that you should stand up and run if ever asked such a question in an interview. ;)

I concur. There is no good reason for asking this as a test for prospective employment, only bad ones.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

Tim Moores wrote:
Manuel Petermann wrote:I was also told that you should stand up and run if ever asked such a question in an interview. ;)

I concur. There is no good reason for asking this as a test for prospective employment, only bad ones.

Unless you decide to not employ any candidate who knows the answer.

I'm just joking, of course, but have you ever heard of overqualification?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1001
    
    2
if i have static and main both than which will be executed first (static i believe, isn't it??).
and if i have more than one static blocks than which will be executed first??
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

Punit: Why not write a small test application to check it out yourself?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1001
    
    2
okay....
i done...
static always first, than main.
and if more than one static, than in sequence as they are....
;-)
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759
But how ?

Won't it throw a java.lang.NoSuchMethodError: main Exception in thread "main"


Jigar Naik


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

The System.exit(0) will cause the JVM to shut down before it gets a chance to throw the error.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3325
    
    8
What do you mean by "run"? An EJB also runs, so does a servlet and neither have the main method.

Java program execution doesn't start with static blocks. Class loading and initialization starts with the static blocks. Once you have the class loaded, you can call main or any other method. Here, "you" could be an application container/server, a browser, a shell, or any another program such as "java"!

To take it even further, "program" is not an appropriate terminology in the java world. There is nothing like a C program in java. There is no start and there is no end of a program because there is no program. You just write Java classes and make their objects interact within an environment. That environment itself may be a "program", which might end after the method that it calls on a Java object ends!

Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

surendra varma wrote:In this post we are going to discuss whether is it possible to write a Java program without main method. Many people thinks that it is possible via applets but I am talking about standalone Java program.

And the answer is Yes. It is possible to write a Java program with out main method. Here is the sampe code.



Actually the funda is in Java program execution starts from static block. So I has placed my code in the static block { …. }. After the execution of the static block JVM searches for the main method. If the main method is absent it will throw main method not found exception. Hence I has terminated my program by calling the exit method so that no error messages are displayed.


The above program will not work from Java SE 7 onwards. In Jdk1.7 the above program will compile fine. But when you try to run it will give the followiing error.

Error: Main method not found in class StaticDemo, please define main method as:
public static void main(String[] args)

It seems that Java 7 specifications does not allow to execute a Java program without main method.

So the answer you have to tell is upto Java SE 6 it is possible to execute a Java program without main method. But from Java SE 7 onwards it is not possib


For me it is not working even in JAVA 6.


Palak Mathur | My Blog | TechJaunt | What is JavaRanch? | List of All FAQs
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 877
    
    5
Palak Mathur wrote:
For me it is not working even in JAVA 6.


Strange - for me it works like this:

C:\source\java\factor>java16 StaticDemo
this is static block

C:\source\java\factor>java17 StaticDemo

C:\source\java\factor>"C:\Program Files\Java\jdk1.7.0\bin\java.exe" StaticDemo
Error: Main method not found in class StaticDemo, please define the main method as:
public static void main(String[] args)
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

Ivan Jozsef Balazs wrote:
Palak Mathur wrote:
For me it is not working even in JAVA 6.


Strange - for me it works like this:

C:\source\java\factor>java16 StaticDemo
this is static block

C:\source\java\factor>java17 StaticDemo

C:\source\java\factor>"C:\Program Files\Java\jdk1.7.0\bin\java.exe" StaticDemo
Error: Main method not found in class StaticDemo, please define the main method as:
public static void main(String[] args)


I got it running through console but when I try to run it through Textpad or any other editor, it doesn't recognizes it.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

How exactly do you run it from Textpad? (There are several programs of that name, according to Google.) What output do you get when you try to run it from Textpad?
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

Martin Vajsar wrote:How exactly do you run it from Textpad? (There are several programs of that name, according to Google.) What output do you get when you try to run it from Textpad?


I am referring to this http://www.textpad.com/. And Google, just mentions different downloads for the same Textpad only.
In Textpad, you can configure it to compile and run Java. I am doing the same.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
It's amazing how much effort you guys put into discussing such an obscure "feature". Do you put as much effort into learning the actual new features?
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

Tim Moores wrote:It's amazing how much effort you guys put into discussing such an obscure "feature". Do you put as much effort into learning the actual new features?


Hi Tim,

Perceptions!
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

Palak Mathur wrote:I am referring to this http://www.textpad.com/. And Google, just mentions different downloads for the same Textpad only.
In Textpad, you can configure it to compile and run Java. I am doing the same.

Yes, I got a little bit distracted by the identical, yet seemingly different Textpads.

How exactly does your configuration of the Textpad to compile and run Java looks like? And what exactly is the output you're getting? (It does not make much sense for me to try it on my own, if I don't match your configuration, right? TellTheDetails (⇐ click) might make it more clear why I'm so stubborn on this.)
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

Tim Moores wrote:Do you put as much effort into learning the actual new features?

I'm trying not to as hard as I can: I'm still on Java 6 and will be for some time to come. I'm grinding my teeth whenever I have to put in a finally clause only to close the resources, already having the try-with-resources secret broken on me. I've decided not to subject myself to the Tantalus-like suffering by learning other tempting Java 7 improvements.

But seriously, I believe Palak is actually running Java 7 from his Textpad, and I'm just trying to disentangle that.
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

Martin Vajsar wrote:
Tim Moores wrote:Do you put as much effort into learning the actual new features?



But seriously, I believe Palak is actually running Java 7 from his Textpad, and I'm just trying to disentangle that.


I do not have Java7 on my machine ;) I will post the details in the evening. Will edit this post.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

Palak Mathur wrote:I do not have Java7 on my machine ;)

All the more interesting.

Please add a new reply, do not edit your existing post. People who are still interested and subscribed to this thread - like me - would not get a notification. Editing a post is best reserved for fixing typos and the like, if you add substantial information long after your post has been created, it may go unnoticed by folks who have already read the old post.
Esaki muthu
Greenhorn

Joined: Apr 05, 2012
Posts: 7
It does not execute without the main method even in java 6 or java 7 .It wil work if the main method is declared.


public class Sample
{
static
{
System.out.println("Hai ");
System.exit(0);
}
public static void main(String[] args)
{
}
}

The above code will work .
But without main method it will not work....To My Knowledge
Satyaprakash Joshii
Ranch Hand

Joined: Jun 18, 2012
Posts: 139
This can be done with static initilizer block...but what has JDK 7 to do with it.???..it was in previous versions too...
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Satyaprakash Joshii wrote:This can be done with static initilizer block...but what has JDK 7 to do with it.???..it was in previous versions too...

Did you read the entire thread ? The point is that it can be done in Java 6 and earlier but not in Java 7.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: can we develop java application without main method in jdk1.7?