• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

executing class without main()

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody
Is " public static void main(String args[])" always necessary to run a java class?
Is there any way in which I can run a class without the main method?

Cheers,
Poonam K.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is there any way in which I can run a class without the main method?


NO. JVM requires main() method to run a class. In other way you can do ...like,if you have a class called Test class having main method, then you can create a thread object of the class that you want to run not having main() method...then call start() and it's run method will be executed..(provided your class should implement Runnable interface).
 
Marshal
Posts: 80775
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Terminology: You cannot run a class at all, but you can call its methods.

As Bisjawit Paria says, you must always use a main method to start a Java application, except possibly when using BlueJ.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are tricks to run a program without a main(...) method, but in any normal program you should always have a main(...) method. Here is an example - do not use something like this in any serious program:
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Technically, when you run applications inside an application server, your classes are running without a main method.

Of course, the application server itself probably has one.

J
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Technically, when you run applications inside an application server, your classes are running without a main method.



Yeah, but that's stretching the meaning of "running" an application a bit, i think. In that sense, an "application" could take many forms (applets, EJB, servlets, RMI, web service, ...), none of which have a main method.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm rather surprised no-one has mentioned this yet...

To run a Java program, you need a native program that will instantiate a JVM and tell it to run method(s) in Java class(es). One might call that program a Java launcher. By far the most common one is the "java[.exe]" program that comes with a JRE or JDK.

The main() method is only special when the Java program is run via "java[.exe]" launcher. Another launcher might have entirely different behaviour.

You can obtain, or write for yourself, another Java launcher. You do this via a part of the Java Native Interface (JNI) called the Java Invocation Interface (JII?). A user-written Java launcher would typically be a C or C++ program (other languages are probably possible), which calls native functions in the JII to instantiate a JVM, find some Java classes and execute Java methods.

As an example, I wrote a Java launcher that does some licensing checks before decrypting some Java code. The encrypted Java code won't run with "java[.exe]" command. For obvious reasons, can't tell you more!

See JII documentation.
[ July 03, 2007: Message edited by: Peter Chase ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic