• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

executing class without main()

 
Poonam Kadu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • 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.
 
Biswajit Paria
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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).
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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:
 
Jeremy Botha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic