Results 1 to 4 of 4
- 12-05-2010, 09:10 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 1
- Rep Power
- 0
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
- 12-06-2010, 01:41 AM #2
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.
- 12-06-2010, 06:18 AM #3
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
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();
}
}
- 12-06-2010, 08:30 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Compiling/Running Project in Command Line: "Could not find main class" Error
By Yasemin Gokce in forum New To JavaReplies: 1Last Post: 06-30-2009, 02:32 PM -
different multiple public class and main class
By mr idiot in forum New To JavaReplies: 2Last Post: 01-01-2009, 12:10 PM -
executing a method in main class
By rangers27 in forum New To JavaReplies: 3Last Post: 07-19-2008, 05:04 AM -
How to create main class link to another two class?
By pearllymary78 in forum New To JavaReplies: 6Last Post: 07-16-2008, 11:02 PM -
Call a main method from within a running program
By zoe in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks