• 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

Do you need a main method for your application to run?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you answered "Yes" ... consider the following code.
File: Something.java
<CODE>
import java.awt.*;
import java.awt.event.*;
public class Something extends Frame{


public Something(){

setLayout( new FlowLayout() );
add( new Button("Button 1") );
add( new Button("Button 2") );
setBounds( 2, 2, 400, 400 );
setVisible( true );

}


public void go(){

System.out.println("GO!!");

}

}
</CODE>
File: StaticRunner.java
<CODE>
public class StaticRunner{

private int x = 1;

Something me = new Something();

static{
new Something().go();
}


}
</CODE>
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope. If you put anything inside a static block, it will get executed by the JVM.
Eg.

-Peter
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that the only time that you NEED a main is if you are creating an application to be run from the operating system (like the DOS prompt) as opposed to from a browser. Without one you get an
Exception in Thread "main" java.lang.NoSuchMethodError : main
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course you need a main method; you may not need one in your class file, but that's just a user-level entry point for initialization. Every time you run the JVM, you invoke the main() that really matters.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, does that mean that you should just include an empty main()?
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, That means that if you are making an application you need a functioning main(). However if you are using an applet or servlet etc. Then the JVM in the browser already has a main() so you don't need to supply it.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you may have gotten the grasp of this already, but I thought I'd just throw this in anyway.
This is from "A Programmer's Guide To Java Certification" by Mughal, Rasmussen:
"Only one public class definition per source file can be defined, and it requires that the file name match the public class. If the public class name is NewApp then the filename must be NewApp.java"
Then later on...
"The Java interpreter executes a method call main in the class specified on the command line. This is the standard way in which a standalone application is invoked."
And one more...
"The main() method always has public accessibility so that the interpreter can call it. It is a static method belonging to the class. It does not return a value, i.e. it is declared void. It always has an array of String objects as its only formal parameter. This array contains any arguments passed to it on the command line."
Okay, so, basically, you can put in a main method if you want the class containing it to be how the application is invoked.
Example:

You can execute this by using: java MyApp
You can compile this by using: javac MyApp.java
The MyApp corresponds to the class file that contains a main() method.
class Biff is just there, it does nothing, but also has no main method. There are times you might want to put a main method in a class so you can test just that class itself, and then remove the method later so that you can use the class to instantiate objects from another class. This kind of thing is often done in GUI programming with swing and what not to test visual components a bit at a time.
Jason
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cindy,
Since the JVM is exits after executing the static intializer block, I really don't mind the exception "java.lang.NoSuchMethodError" being thrown. I tried catching the exception in the static initializer block, but that didn't work. Furthermore, there wouldn't have been much I could have done with the Exception other than to catch it and not display the message to the console.
Back to the topic of this thread which posted the question, "Do you need a main metod for your application to run?" Technically, no. You can run a JAVA application by "tricking" it to start everything from the static initializer block (and put up with the exception message you pointed out). To get around the exception, I could have added a main method that does absolutely nothing.
E.g. Taking my above example one step further,

Why anyone would want to do this is beyond me. However, I have had occasion where I've used the static initializer block to load data into a cache that would be use later by the class. When the class got loaded by the JVM, the cache would get populated. By the time I needed the data inside the class method, it was already available.
-Peter

[This message has been edited by Peter Tran (edited January 15, 2001).]
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like you've had some pretty clever use out of that static initializer.
The problem is that you can not trick Java into running your static initializer unless something starts the ball rolling. If you are running from the DOS prompt the JRE looks for a main() to kick things off.
If you are executing from a browser, then the JVM IS the application (it has the main())and your code is a "sub-application" that the JVM calls. In that case you will never get the
Exception in Thread "main" java.lang.NoSuchMethodError : main
message. Because it was already found before your code got loaded.
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cindy,
I'm not sure I follow your reasoning when you say, "The problem is that you can not trick Java into running your static initializer unless something starts the ball rolling. If you are running from the DOS prompt the JRE looks for a main() to kick things off."
If you look at my first example where I don't have the canonical public static void main(String[] argv) method, I can still get the JRE to execute the System.out.println("Look ma, no main() calling me.") method. Granted I get the exception, java.lang.NoSuchMethodError.
When the JVM loads a class, it will execute any initializer block in the order they occur in the class file before executing any method of the class (including the main() method).
In RHE 1st edition, chapter 3 Test question #5 (if I may quote it here)

I get x = 3 for the answer.
Now if I remove the main() method and add a static initializer block at the end to print out the value of x,

I get:
x = 3
Exception in thread "main" java.lang.NoSuchMethodError: main
Ignoring the exception for the moment, you do have to admit that the JRE did execute the statements in the class without a main() method. Essentially, I did "trick" the JRE to run the statements when it loaded the class via the static initializer block.
-Peter

[This message has been edited by Peter Tran (edited January 15, 2001).]
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK,
I see what you are saying. From Landon's first example I assumed you were talking about running from a browser (should never assume stuff) and that was the question about main() that we were discussing.
But, yeah, if you run you static initializer from the DOS prompt you do "trick" the JRE into doing the display.
[This message has been edited by Cindy Glass (edited January 16, 2001).]
 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hm. Up to this point, I thought Cindy and I were kind of making the same argument, more or less. What I was trying to say, in the spirit of the original, mildly obnoxious question, is that having a main() in a class file is "merely" an entry point for application code. There's a main() that gets invoked every time you call the interpreter.
So this question to me comes down to personal preferences for parsing its meaning. The way I prefer to look at it, the only reason static initializers run at all is because the "true" main runs regardless of the class code. Static initializers are NOT surrogate main methods -- unless you simply want to argue what main() "really is".
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael,
Don't get me wrong. I would never use a static initializer to start my program. Personally, I think every class should have a main() method to provide a way to unit test that class. I was intentionally trying to answer the question in the topic: "Do you need a main method for your application to run?" From my earlier arguments, you can see what I think the answer is.
-Peter
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I guess the question is: if your static initializer causes things to happen, can you really say that your "application ran", or can you just say that your "application initialized"??
[This message has been edited by Cindy Glass (edited January 17, 2001).]
 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter - I wasn't meaning to pick nits directly. We're just answering the question different ways, and that's fun.
I have to admit, though, I'm still surprised from time to time by the intensity so many of the posters here approach a mastery of these subtleties. I have to suppress the urge to say "why on earth would you worry about that?"
I see Cindy and I are on the same page, to wit: what does it mean to run. Run-time interpreter phenomenology. Oy.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Philosophical conversations around the java campfire.
Marshmellows anyone?
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cindy,
I'll take one with a Hershey wedges between two gram crackers.
Thanks,
-Peter
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, bottom line, you expert types:
Is including a main() method in a Java application an absolute REQUIREMENT, or merely an excellently good idea (so we don't confuse grandma)?
Ed
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ed poor:
So, bottom line, you expert types:
Is including a main() method in a Java application an absolute REQUIREMENT, or merely an excellently good idea (so we don't confuse grandma)?
Ed


Consider it an ABSOLUTE REQUIREMENT the items shown in this post are just what you could commonly expect under unusual conditions. To properly identify the class that you wish to start with in an application, include a
public static void main(String args[])
Hope this helps
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic