Invoke other method than main(String[])

Posted on 2002-05-02
Medium Priority
Last Modified: 2010-03-31
If I specify to a java program a class to run it runs its "public static void main(String[])" method.
Is there a way to run other "public static void methodName(String[])" method?
Question by:rzvika2
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 92

Expert Comment

ID: 6987903
No, that is the only entry point for a Java application.

You could simply write a wrapper class that calls your method:

public Wrapper
   public static void main(String[] args)

Expert Comment

ID: 6987956
Yes you can run other public static void methodName(String[] arg) in same class.

for example..

public class test
     public static void mine(String[] args)
          System.out.println("This is mine method");

     public static void main(String[] args)
          System.out.println("This is main method");

hope above code will help you..

LVL 92

Expert Comment

ID: 6987963
1. that doesn't actually answer the question
2. it's pretty much copied from my comment.

Lets keep it original please :)
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI


Author Comment

ID: 6987969
My big mistake...
I've ment that I want to run it from the comand line.
I don't want that the main method will call it.
My point is to create some methods - each one do different thing, and the user can invoke the relevant method directly from the commnad line.
BTW, I don't want to give the main(String[]) method parameter wich method to invoke...

LVL 35

Expert Comment

ID: 6988132
> My point is to create some methods - each one do different thing, and the user can invoke the relevant
method directly from the commnad line.

  I think you have misunderstood something here. When you call a class you call it by name and then it is the Virtual Machine that looks for the main method. This is default and you cannot change it (as objects pointed out as well). No matter what the name of your class is, it is the main method that will be called every time. If you have a class:

public class MyClass
    public static void main(Strign [] arguments)

and another one:

public class MyClass2
    public static void main(String [] arguments)

  in order to run them you do:

"java MyClass" and "java MyClass2" and not "java main". In a few words you *cannot invoke methods from the command line. Just classes*. So if you want your users to call different "names" then have different class names that all of them will have a "main" method.

  Hope it helps.
LVL 92

Accepted Solution

objects earned 300 total points
ID: 6988196
> My point is to create some methods - each one do
> different thing, and the user can invoke the relevant
> method directly from the commnad line.

As I first mentioned you cannot do this, main is the ONLY entry point for a Java application.
I was merely offering you a possibly workaround.


Author Comment

ID: 6993523
I've understood it and that's why I've asked this question.
The reason not to do different class for each purpose is because they should behave in a very common way with little differences.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question