CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2004
    Posts
    7

    Question Can't invoke method <main>

    import java.awt.*;
    import java.awt.event.*;
    import java.lang.*;
    import java.lang.reflect.*;
    import javax.swing.*;

    // Calling apps

    public class Debug extends javax.swing.JFrame
    {
    public Debug()
    {
    setTitle("java.lang.reflect.Method.invoke");
    setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
    getContentPane().setLayout(new BorderLayout(0,0));
    getContentPane().setBackground(new java.awt.Color(211,207,199));
    setSize(375,162);
    setVisible(false);
    }

    static public void main(String args[])
    {
    (new Debug()).setVisible(true);
    }

    void CallactionPerformed(java.awt.event.ActionEvent event) {
    String args[] = {""};
    PasswordDialog.main(args);
    }


    void invokeClass(Class cl)
    throws ClassNotFoundException,
    NoSuchMethodException,
    InvocationTargetException
    {
    // String args[] = new String[1];
    String args[] = {""};

    Class array[] = {java.lang.Class.forName("[Ljava.lang.String;")};

    java.lang.reflect.Method method = null;
    try {
    method = cl.getMethod("main", array);
    } catch (NullPointerException e) {

    } catch (NoSuchMethodException e) {

    }

    method.setAccessible(true);

    try {
    Object result = method.invoke(cl, args);
    } catch (IllegalAccessException e) {
    }
    }


    void pasteButton_actionPerformed(java.awt.event.ActionEvent event)
    {
    MyClass mc = new MyClass();
    Class cl = mc.getClass();

    try {
    invokeClass(cl);
    }
    catch (ClassNotFoundException e) {
    }
    catch (NoSuchMethodException e) {
    }
    catch (NoClassDefFoundError e) {
    }
    catch (InvocationTargetException e) {
    e.getTargetException().printStackTrace();
    }
    }
    }

    // Called apps

    import java.awt.*;

    import javax.swing.*;

    public class MyClass extends javax.swing.JDialog
    {
    public MyClass()
    {
    this((Frame)null);
    }

    public void setVisible(boolean b)
    {
    if (b)
    setLocation(50, 50);
    super.setVisible(b);
    }

    static public void main(String args[])
    {
    (new MyClass()).setVisible(true);
    }
    }

    // Runtime error

    JDK 1.2

    Exception occurred during event dispatching:
    java.lang.IllegalArgumentException: argument type mismatch
    at java.lang.reflect.Method.invoke(Native Method)
    at Debug.invokeClass(Debug.java:304)
    at Debug.InvokeactionPerformed(Debug.java:320)

    JDK 1.4

    java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:324)
    at Debug.invokeClass(Debug.java:304)
    at Debug.InvokeactionPerformed(Debug.java:320)
    Last edited by Yury; April 20th, 2004 at 12:34 PM.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    You can't get a runtime error with the code you posted because it doesn't even compile - not only is PasswordDialog undefined, but in your MyClass constructor you try to call this((Frame) null); when you don't have a constructor that takes a Frame.

    I suspect this line:
    Code:
    Class array[] = {java.lang.Class.forName("[Ljava.lang.String;")};
    doesn't help.

    If you post the code that actually compiles and produces the runtime error, I might take a bit more interest...

    It is better to have an approximate answer to the right question than an exact answer to the wrong one...
    J. Tukey
    Last edited by dlorde; April 20th, 2004 at 08:33 AM.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Apr 2004
    Posts
    7

    Post

    Well,

    Here is the compiled code.

    Note. If I use

    String args[] = new String[1];

    instead of

    String args[] = {""};

    Everything is OK.
    Attached Files Attached Files

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    Class array[] = {java.lang.Class.forName("[Ljava.lang.String;")};


    does this line mean you are attempting to invoke the main() method of a String? does String even have a main() ?
    C:\javawork\trash>java java.lang.String
    Exception in thread "main" java.lang.NoSuchMethodError: main
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    please note also, that the following statements are NOT equivalent:

    String args[] = new String[1];
    String args[] = {""};

    the following two equivalences are correct:

    PHP Code:
    String args[] = new String[1];
    String args[] = new String[]{null}; 
    or
    PHP Code:
    String args[] = {""};

    String args[] = new String[1];
    args[0] = ""
    -
    from my brief investigations, it appears that the invoke method expects two parameters, the second of which is the arguments
    The arguments are an array of type Object[], length of 1, and the value at index 0 must be null

    the following codes do not produce an error:

    Code:
    String[] args = new String[1];
    String[] args = new String[]{null};
    Object[] args = new Object[1];
    Object[] args = new Object[]{null};
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  6. #6
    Join Date
    Apr 2004
    Posts
    7
    Thanx a lot.

    I knew this, but the problem is:

    how can I pass parameters

    (like a MyClass.main(args) in attached code,
    where String args[] = {"Param1", "Param2",..})

    via

    method.invoke(class, args)

  7. #7
    Join Date
    Apr 2004
    Posts
    7

    Post Solution

    Dear cjard!

    You are not quite right about:

    "The arguments are an array of type Object[], length of 1, and the value at index 0 must be null"

    To pass parameters to <main> (as well as to any method with parameters String args[]) via Method.invoke is possible as

    follows:

    MyClass.main(new String args[] {"Param1", "Param2",..})

    is equivalent to

    method.invoke(new Object[] { new String args[] {"Param1", "Param2",..} })

    or more detailed

    String CallArgs[] = {"Param1", "Param2",..};
    MyClass.main(CallArgs);

    is equivalent to

    Class ParamType[] = {java.lang.Class.forName("[Ljava.lang.String;")};
    java.lang.reflect.Method method = null;
    try {
    method = MyClass.class.getMethod("main", ParamType);
    } catch (NullPointerException e) {};
    } catch (NoSuchMethodException e) {};

    Object InvokeArgs[] = new Object[] {CallArgs};

    try {
    Object result = method.invoke(MyClass.class, InvokeArgs);
    } catch (IllegalAccessException e) {};

    Best regards,

    Yury

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    ok.. just remember not to name your variables witha capital letter at the start (callArgs, invokeArgs.. not CallArgs, InvokeArgs)
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  9. #9
    Join Date
    Apr 2004
    Posts
    131

    Re: Can't invoke method <main>

    Originally posted by Yury
    static public void main(String args[])
    Don't you have to write public static void main in that specific order? I've never tried your order, but I've had issues in other areas when mixing up the order of things, like instance variables.

  10. #10
    Join Date
    Dec 2003
    Posts
    593

    Re: Re: Can't invoke method <main>

    Originally posted by Demonpants Software
    Don't you have to write public static void main in that specific order? I've never tried your order, but I've had issues in other areas when mixing up the order of things, like instance variables.
    Nope, static public void main(String[] args) is exactly the same as public static void main(String[] args). What you can't do (in any method) is static void public main(String[] args) as the return type is assumed to be the LAST modifier, so whatever comes after the return type is assumed to be the identifier. Since neither static or public are valid identifiers because they're keywords, that wouldn't be allowed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured