aspose file tools*
The moose likes Java in General and the fly likes executing class without  main() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "executing class without  main()" Watch "executing class without  main()" New topic
Author

executing class without main()

Poonam Kadu
Ranch Hand

Joined: Jul 21, 2006
Posts: 49
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

Joined: Feb 02, 2003
Posts: 46
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

Joined: Oct 13, 2005
Posts: 38788
    
  23
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

Joined: Aug 16, 2005
Posts: 14146
    
  18

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeremy Botha
Ranch Hand

Joined: Feb 16, 2005
Posts: 125
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


McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41819
    
  62
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.


Ping & DNS - my free Android networking tools app
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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 ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: executing class without main()