ArtoAle
ArtoAle

Reputation: 2977

Close a window opened by a "jar" application started within my Java code

I have a (Swing + AWT) application which uses an external jar library (by calling a main method inside it). This external application opens a window each time an event occurs (e.g. a button is pressed). Please consider that I have no access to the external jar source code.

How can I close the previously opened window before calling the main again?

The actionPerformed looks like this:

private void anActionPerformed(java.awt.event.ActionEvent evt) {
           String [] argv = {"arg1","arg2","arg3"};
           com.somepackage.SomeClass.main(argv);
        }

Upvotes: 1

Views: 149

Answers (2)

Robin
Robin

Reputation: 36601

Perhaps not the safest way, but you might consider using the Window#getWindows method. If you know that a Window is created by calling the main method, you can ask for all Window instances right before calling the main and compare it to all Window instances afterwards. The new Window instance is the one you are looking for, and then you have a reference allowing to dispose it afterwards.

Upvotes: 1

user unknown
user unknown

Reputation: 36229

You can just guess - which might be wrong - that SomeClass.main calls a Ctor new SomeClass.

You could try to call it yourself:

SomeClass someclass = new SomeClass ();

It it is a JFrame, it is often initialized in main, and some properties are often set:

someclass.setSize (...
someclass.setLocation (...
someclass.setVisible (true);

If that works, you can store the someclass reference in a way, that you later can call

someclass.dispose ();
someclass = new SomeClass ();

Else you might search every class in the jar:

jar -tf some.jar

and use reflection and introspection to search for a ctor to call.

Or you try to extend SomeClass - maybe it isn't final and can be inherited.

class MyClass extends SomeClass 

Upvotes: 1

Related Questions