Solved

Invoke other method than main(String[])

Posted on 2002-05-02
8
220 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
[X]
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
8 Comments
 
LVL 7

Expert Comment

by:tomboshell
ID: 6988005
Yes.  What is the problem?  What error are you getting?  You want to make another static method that is public and takes a string arrray as a parameter.  Should be no problem.  Just make sure that the static works on static variables and methods (or a constructor), otherwise you will get an error saying that you are trying to access a non-static something within a static method.  

Tom
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 6988018
No, you can't run anything besides main with the 'java' command. But you can do something like:

public class Test
{
   public static void main(String args[])
   {
      Test test = new Test();
      test.run();
   }

   public void run()
   {
      System.out.println("Running...");
   }
}

This way, you can call any public function of the class, static or not.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 6988047
Oh, to 'run' it.  Sorry, I misunderstood/misread the question.
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 35

Expert Comment

by:girionis
ID: 6988106
 As jlouwere pointed you cannot run any other method with the "java" command besides the "main" mehtd. But I am just curious. Why would you want to do it?
0
 
LVL 92

Expert Comment

by:objects
ID: 6988198
This is a duplicate question.
0
 
LVL 4

Accepted Solution

by:
Neutron earned 100 total points
ID: 6988214
hi rzvika2 :-)

I'm also curious about the purpose, anyway here is one way to do it using reflection:

-------8<-------cut here---------------------invoke.java-------
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class invoke
{
    public static void main( String[] args ) throws Throwable
    {
        if (args.length < 2)
        {
            System.out.println( "\nUSAGE: invoke <class> <method> {parameters}\n" );
            System.exit( 7 );
        }
       
        String className = args[0];
        String methodName = args[1];
       
        Class clazz = Class.forName( className );
       
        Method method = null;
        Class[] methodParameters = { args.getClass() };
       
        while (method==null && clazz!=null)
        {
            try
            {
                method = clazz.getDeclaredMethod( methodName, methodParameters );
            }
            catch( Throwable t )
            {
            }
           
            if (method != null)
            {
                if (!Modifier.isAbstract( method.getModifiers() ))
                {
                    if (Modifier.isPublic( method.getModifiers() ))
                    {
                        if (!Modifier.isStatic( method.getModifiers() ))
                        {
                            System.out.println( "Method "+methodName+" declared in the class "+clazz.getName()+" is not static." );
                            System.exit( 77 );
                        }
                    }
                    else
                    {
                        System.out.println( "Method "+methodName+" declared in the class "+clazz.getName()+" is not public." );
                        System.exit( 777 );
                    }
                }
                else
                {
                    System.out.println( "Method "+methodName+" declared in the class "+clazz.getName()+" is abstract." );
                    System.exit( 7777 );
                }
            }
           
            if (method == null)
            {
                if (clazz != Object.class)
                {
                    clazz = clazz.getSuperclass();
                }
                else
                {
                    clazz = null;
                }
            }
        } // while
       
        if (method != null)
        {
            int rest = args.length-2;
            String[] restArgs = new String[Math.max( rest, 0 )];
           
            if (rest > 0)
            {
                System.arraycopy( args, 2, restArgs, 0, rest );
            }
           
            method.invoke( null, new Object[]{ restArgs } );
        }
    }
   
} /* invoke */
-------8<-------cut here---------------------invoke.java-------

-------8<-------cut here---------------------example.java-------
public class example
{
    public static void subsidiary( String[] args )
    {
        System.out.println( "H, W!" );
       
        for (int i=0; i<args.length; i++)
        {
            System.out.println( args[i] );
        }
    }
   
} /* example */
-------8<-------cut here---------------------example.java-------

Hopefully this is what you had in mind.
To run this you can use following cmd-line:

java invoke example subsidiary 1 2 3 4 5 6 7

...which should behave just as if you ran

java example 1 2 3 4 5 6 7

...having 'subsidiary' as the default application method.

Best wishes,
    </ntr> :)
0
 

Author Comment

by:rzvika2
ID: 6990820
Thanks for the effort !
(By the way, I need it for supplying some methods (to enter the java program from command line) in a specific class that do different things).
0
 
LVL 4

Expert Comment

by:Neutron
ID: 6993175
No problem :)
I hope that the duplicate question objects mentioned didn't have a reply similar to mine. If so, I would be glad to share the points :-)

Greetings,
    </ntr> :)
0

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 Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

707 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