Results 1 to 4 of 4
  1. #1
    tlrocketman is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default Running main method class from another main class

    I have currently made my prgm open the other prgm however I can not figure out how to close the 1st one without closing everything. Is this possible?


    This is my code block for opening my 2nd prgm with a main method

    try {
    Method mainMethod = Class.forName("class2").getDeclaredMethod("main",S tring[].class);
    Object[] argListForInvokedMain = new Object[1];
    argListForInvokedMain[0] = new String[0];

    mainMethod.invoke(null,

    argListForInvokedMain);
    }
    catch (ClassNotFoundException ex) {
    System.err.println("Class "+"class2"+" not found in classpath.");
    }
    catch (NoSuchMethodException ex) {
    System.err.println("Class "+"class2"+" does not define public static void main(String[])");
    }
    catch (InvocationTargetException ex) {
    System.err.println("Exception while executing "+"class2"+":"+ex.getTargetException());
    }
    catch (IllegalAccessException ex) {
    System.err.println("main(String[]) in class "+"class2"+" is not public");
    }






    }
    });


    :confused:Thanks

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    Invoking the second main program from within your first one like that using the method.invoke() is kind of like a function call, the second program runs inside the main one.
    If you want to make the second one work independently after the first one exits, you might be able to do something with the ProcessBuilder to create a system call executable, to have a new Java process launched as a child process. Though even then I am not sure how that would behave when the main process exits.

  3. #3
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    class A
    {
    public static void main(String ... args)
    {
    System.out.println("Hai from main A");
    }
    }

    public class B
    {
    public static void main(String ... args)
    {
    System.out.println("Hai from main B");
    A.main();

    }
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Call the other main( ... ) method in a separate Thread that is not a daemon thread. That thread will keep on running even when 'your' main( ... ) has terminated. Read the API documentation for the Thread class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 06-30-2009, 02:32 PM
  2. different multiple public class and main class
    By mr idiot in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 12:10 PM
  3. executing a method in main class
    By rangers27 in forum New To Java
    Replies: 3
    Last Post: 07-19-2008, 05:04 AM
  4. How to create main class link to another two class?
    By pearllymary78 in forum New To Java
    Replies: 6
    Last Post: 07-16-2008, 11:02 PM
  5. Replies: 1
    Last Post: 08-07-2007, 06:16 AM

Tags for this Thread

Posting Permissions

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