Problem with classloader

J

jerry

Code:
Class[] argTypes = { String[].class };
java.lang.reflect.Method method =
ClassLoader.getSystemClassLoader().loadClass(className).getDeclaredMethod("main",
argTypes);
Object[] args = { new String[0] };
method.invoke(null, args);

what is wrong with this code? i have a java application (JPanel) and
it work as a main window and then, im trying to launch a java
application (JFrame) but when i close the JFrame window, the main
window also terminated.

is somebody know this?


thanks.
 
A

Andrew Thompson

jerry wrote:
...
..i have a java application (JPanel) and
it work as a main window and then, im trying to launch a java
application (JFrame) but when i close the JFrame window, the main
window also terminated.

is somebody know this?

Look at the method
javax.swing.JFrame.setDefaultCloseOperation(int).
The default is JFrame.DO_NOTHING_ON_CLOSE, but I
suspect your IDE has other 'ideas'.

By the way. Sentences in English should start with a single
Upper Case letter to help the reader tell where they start and
end. Also, the word I or the abbreviation I'm, should also start
with upper case. 'I' is always upper case.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

jerry said:
Code:
Class[] argTypes = { String[].class };
java.lang.reflect.Method method =
ClassLoader.getSystemClassLoader().loadClass(className).getDeclaredMethod("main",
argTypes);
Object[] args = { new String[0] };
method.invoke(null, args);

what is wrong with this code? i have a java application (JPanel) and
it work as a main window and then, im trying to launch a java
application (JFrame) but when i close the JFrame window, the main
window also terminated.

The two "things" are running in the same process. If closing the
JFrane result in a a call to System.exit, then the entire process
is exited.

But you can control that - see Andrews posts for some Swing hints.

Arne
 
J

jerry

jerry wrote:

..



Look at the method
javax.swing.JFrame.setDefaultCloseOperation(int).
The default is JFrame.DO_NOTHING_ON_CLOSE, but I
suspect your IDE has other 'ideas'.

By the way. Sentences in English should start with a single
Upper Case letter to help the reader tell where they start and
end. Also, the word I or the abbreviation I'm, should also start
with upper case. 'I' is always upper case.



Thank you for correction, I really appreciate it.

I tried the code above and it seems work to me. But my problem is,
I've created a program that will launch
application from jar file, so means I don't have access on it. This
jar file is a collection
of window application.

If the application(JFrame/Other Object) has this code:
(probably a child window)
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt)
{
System.exit(0);
}
});
the main window (Launcher) exit too.
 
A

Andrew Thompson

jerry wrote:
...
I tried the code above and it seems work to me. But my problem is,
I've created a program that will launch
application from jar file, so means I don't have access on it. This
jar file is a collection
of window application. ...
...
the main window (Launcher) exit too.

Unless you implement an 'ExitManager'* to prevent
the other applications from doing that.

*
<http://groups.google.com/group/comp.lang.java.programmer/msg/f29ab45389d9f5f2
Whether the other apps. handle that gracefully is
another matter. You may need to explicitly
setVisble(false) and take other measures.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
 
A

Andrew Thompson

Andrew said:
...

Unless you implement an 'ExitManager'* ...

Or, I realise now as I reread Arne's reply, start the other
apps. in entirely new Process(es).

OTOH - I think the current approach using the code you
have + an ExitManager, has more control over the resulting
application.
 
J

jerry

Look at the method
javax.swing.JFrame.setDefaultCloseOperation(int).
The default is JFrame.DO_NOTHING_ON_CLOSE, but I
suspect your IDE has other 'ideas'.

By the way. Sentences in English should start with a single
Upper Case letter to help the reader tell where they start and
end. Also, the word I or the abbreviation I'm, should also start
with upper case. 'I' is always upper case.


Thank you for correction, I really appreciate it.

I tried the code above and it seems work to me. But my problem is,
I've created a program that will launch
application from jar file, so means I don't have access on it. This
jar file is a collection
of window application.

If the application(JFrame) has this code:
(probably an application that will launch from main window)
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt)
{
System.exit(0);
}
});
the main window (Launcher) exit too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top