• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
surendra varma
Greenhorn
Posts: 1
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting, but the crucial question is -- why would you want to do that in a real application?!
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. ;)
 
Tim Moores
Bartender
Posts: 2895
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1014
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit: Why not write a small test application to check it out yourself?
 
Punit Jain
Ranch Hand
Posts: 1014
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay....
i done...
static always first, than main.
and if more than one static, than in sequence as they are....
;-)
 
Jigar Naik
Ranch Hand
Posts: 762
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how ?

Won't it throw a java.lang.NoSuchMethodError: main Exception in thread "main"
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The System.exit(0) will cause the JVM to shut down before it gets a chance to throw the error.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3764
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Palak Mathur
Ranch Hand
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ivan Jozsef Balazs
Rancher
Posts: 981
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2895
46
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic