?
Solved

Invoke other method than main(String[])

Posted on 2002-05-02
8
Medium Priority
?
223 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 400 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

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

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 11 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