how to find "main class" in order to run a program

H

hthukral.mickey

hi..
As per example in case of any JDK ..demo examples are given.. one
can run it from command line by knowing which is the main class file
(i.e. where main class is being declared)
But how to find that out is a problem i'm facing ..
 
A

Andrew Thompson

On Feb 21, 9:32 am, (e-mail address removed) wrote:
....
But how to find that out is a problem i'm facing ..

Also, communication skills.
(Don't forget to ask a question)

Andrew T.
 
L

Lew


You might find /Thinking in Java/ by Bruce Eckel very helpful on these kinds
of problems. It does a right fine job of helping one understand the
fundamentals of the Java language. Earlier versions of the book are available
for free on line.

There is not exactly a concept of "main class" in Java. In JAR files there is
a so-called "manifest" that tells a "java -jar" command what class to run, but
nothing prevents running the main() method of a different class in that same
JAR using other syntax.

There is a concept of a main() method in a class, and any public class that
has a properly defined main() method is a "main class".

If you are running a Java application from someone else, their documentation
should tell you what class to run. If it is your application, then you get to
choose any class for which you wrote a main() method (obeying the rules of
that method).

- Lew
 
M

Martin Gregorie

hi..
As per example in case of any JDK ..demo examples are given.. one
can run it from command line by knowing which is the main class file
(i.e. where main class is being declared)
But how to find that out is a problem i'm facing ..
grep
 
O

Oliver Wong

Lew said:
You might find /Thinking in Java/ by Bruce Eckel very helpful on these
kinds of problems. It does a right fine job of helping one understand the
fundamentals of the Java language. Earlier versions of the book are
available for free on line.

There is not exactly a concept of "main class" in Java. In JAR files there
is a so-called "manifest" that tells a "java -jar" command what class to
run, but nothing prevents running the main() method of a different class
in that same JAR using other syntax.

There is a concept of a main() method in a class, and any public class
that has a properly defined main() method is a "main class".

If you are running a Java application from someone else, their
documentation should tell you what class to run. If it is your
application, then you get to choose any class for which you wrote a main()
method (obeying the rules of that method).

On the other hand, a couple of IDEs have a concept of a "main class"
(Eclipse is one of them), and that concept basically refers to which class's
main method the IDE should run when you tell it to run the application. Note
that every "run configuration" (that's the term Eclipse uses) has exactly
one main class, but a given project may have multiple main classes (which
implies multiple "run configurations" can be associated with a single
project).

So as Lew said, if it's your application, then you, as the designer,
just choose a class to be your main class. If you're inheriting code from
someone else, then you just gotta search for static void methods called
"main" which take an array of String as an argument (different IDEs have
different ways of searching through the code base). If you gotta do this
programmatically, then you iterate through all the candidate classes, using
reflection to check for the existence of such a method.

- Oliver
 
H

hthukral.mickey

You might find /Thinking in Java/ by Bruce Eckel very helpful on these kinds
of problems. It does a right fine job of helping one understand the
fundamentals of the Java language. Earlier versions of the book are available
for free on line.

There is not exactly a concept of "main class" in Java. In JAR files there is
a so-called "manifest" that tells a "java -jar" command what class to run, but
nothing prevents running the main() method of a different class in that same
JAR using other syntax.

There is a concept of a main() method in a class, and any public class that
has a properly defined main() method is a "main class".

If you are running a Java application from someone else, their documentation
should tell you what class to run. If it is your application, then you get to
choose any class for which you wrote a main() method (obeying the rules of
that method).

- Lew

thanks LEW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top