Run main() in one Java app from another Java app

Hello, this is probably dumb question. I am pretty new to Java. I am totally hooked on NetBeans 6 IDE for creating desktop apps. I know this is not a NetBeans forum, but I think this is a Java question.

I have spent a day trying to do somthing with Java, that must be pretty basic. I created two small Java projects in NetBeans, and I just want one to run, by clicking a button on another one.

- I have tried in lots of way, but usally the code has syntax errors. The line of code below compiled, but did not do anything...

new otherpackage.OtherGUIApp();

I am thankful for any help, thanks.

[623 byte] By [fuzzyJavaBraina] at [2007-11-27 5:49:04]
# 1

new otherpackage.OtherGUIApp(); - calls the constructor. IF it doesn't do anything in the constructor, it won't do anything calling it.

otherpackage.OtherGUIApp().main(new String[]{}); - calls the main method, which is perfectly legitimate. Just know that it doesn't run the other app as a separate application. Terminating the first app will kill the second.

bsampieria at 2007-7-12 15:35:06 > top of Java-index,Java Essentials,New To Java...
# 2

new Thread(new Runnable(){

public void run() {

OtherApp.main(null);

}

}).start();

BeanDippera at 2007-7-12 15:35:06 > top of Java-index,Java Essentials,New To Java...
# 3
still runs in the same JVM, of course.
bsampieria at 2007-7-12 15:35:06 > top of Java-index,Java Essentials,New To Java...
# 4

> I have spent a day trying to do somthing with Java,

> that must be pretty basic. I created two small Java

> projects in NetBeans, and I just want one to run, by

> clicking a button on another one.

You might want to try the above suggestions but then re-think your design.

Either you have two apps or your don't. If you have two then it is unlikely that you need to run one from the another. If you don't really have two apps then you should create your code so there is only one app.

If you really need two apps and you really need to run the second from the first then you probably want to look at Runtime.exec().

jschella at 2007-7-12 15:35:06 > top of Java-index,Java Essentials,New To Java...
# 5

Thanks for all of your replys. The reason I have two apps is because with the beans binding in NetBeans 6 Preview, is is really easy to create a GUI by creating a new application. Also, it I thought it would be easier to maintain as the app grows larger. I guess those might not be a logical reasons, because there is probably performance reasons why I should create one app?

This code compiled without errors, but got the errors listed below...

otherprojectpackage.OtherGUIApp.main(new String[]{});

After the app is running and I click my button to run the second app, I get the following...

SEVERE: Application failed to launch

SEVERE: Application failed to launch

java.lang.IllegalStateException: application has been launched

at application.ApplicationContext.setApplicationClass(ApplicationContext.java:122)

at application.Application$1.run(Application.java:160)

at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

This code compiled without errors, but got the errors listed below...

new Thread(new Runnable(){

public void run() {

otherprojectpackage.OtherApp.main(null);

}

}).start();

After the app is running and I click my button to run the second app, I get the following...

SEVERE: Application failed to launch

java.lang.IllegalStateException: application has been launched

at application.ApplicationContext.setApplicationClass(ApplicationContext.java:122)

at application.Application$1.run(Application.java:160)

at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Note: I am working with two separate projects, but I included the OtherGUIApp project as a Library in the app I am trying to call it from.

I also tried adding the packages form the otherprojectpackage.OtherApp into the one main app in NetBeans. So I copied the packages and entity components from otherprojectpackage.OtherApp and pasted them into my Main app, but that didn't seem to work either.

Thanks again for any advice.

fuzzyJavaBraina at 2007-7-12 15:35:06 > top of Java-index,Java Essentials,New To Java...