Solved

Invoke other method than main(String[])

Posted on 2002-05-02
7
210 Views
Last Modified: 2010-03-31
Hi!
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?
Thanks!
0
Comment
Question by:rzvika2
7 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
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)
   {
      MyClass.methodName(args);
   }
}
0
 

Expert Comment

by:paku
Comment Utility
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)
     {
          mine(args);
          System.out.println("This is main method");
     }
}

hope above code will help you..

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
1. that doesn't actually answer the question
2. it's pretty much copied from my comment.

Lets keep it original please :)
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:rzvika2
Comment Utility
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...

Thanks.
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
> 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.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
Comment Utility
> 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.

0
 

Author Comment

by:rzvika2
Comment Utility
girionis:
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now