• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 1
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting, but the crucial question is -- why would you want to do that in a real application?!
 
Ranch Hand
Posts: 177
Hibernate Python Linux
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. ;)
 
Saloon Keeper
Posts: 7594
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Punit: Why not write a small test application to check it out yourself?
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
;-)
 
Ranch Hand
Posts: 763
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But how ?

Won't it throw a java.lang.NoSuchMethodError: main Exception in thread "main"
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Enthuware Software Support
Posts: 4822
52
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 7594
177
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 343
Mac OS X Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 440
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic