我正在尝试创建一个Java程序,用户可以在其中从计算机中选择任何文件.class或.jar文件.然后我的程序会弹出一个JInternalFrame与JEditorPane它作为控制台,捕捉来自用户的程序的任何控制台输出.当用户的程序关闭(调用System.exit(int status);)时,我的程序不能随之关闭.我的程序可能还有一个功能,如立即停止用户程序的按钮和IDE的其他功能.我的程序不需要编译Java代码,只需运行.class和.jar文件.
我已经制作了这个程序的一个小测试版本,其中我从一个包中获得了两个特定文件,并让用户单击两个按钮中的一个,每个按钮代表两个程序中的一个.按一下按钮会调用以下方法:
private void run(Class runnable)
{
java.lang.reflect.Method[] m = runnable.getMethods();
boolean hasMain = false;
for (int i = 0; i < m.length; i++)
{
if (m[i].getName().equals("main") && m[i].getParameterTypes()[0].isArray() && m[i].getParameterTypes()[0].getName().contains("java.lang.String"))
try
{
Object invoke = m[i].invoke(null, (Object)globalArgs);
hasMain = true;
hub.setExtendedState(Hub.ICONIFIED);
numPrograms++;
}
catch (Throwable t)
{
java.util.logging.Logger.getLogger(Hub.class.getName()).log(java.util.logging.Level.SEVERE, null, t);
javax.swing.JOptionPane.showMessageDialog(null, "Could not run " + runnable.getName(), "Error in invocation", javax.swing.JOptionPane.ERROR_MESSAGE);
}
finally
{
break;
}
}
if (!hasMain)
javax.swing.JOptionPane.showMessageDialog(null, runnable.getName()
+ " does not have a public static main method that\nreturns void and takes in an array of Strings",
"No main method", javax.swing.JOptionPane.ERROR_MESSAGE);
}
Run Code Online (Sandbox Code Playgroud)
此方法成功调用程序的main方法并运行所述程序的副本.但是,当此集线器已启动的任何程序调用该System.exit(int status)命令时,集线器也会关闭.另外,我对如何捕获控制台输出没有丝毫的线索.
有没有人有任何经验或建议他们愿意分享,以帮助我制作一个功能齐全的程序,可以......
.jar文件可能有多个带有main(String[] args)方法的类)System.exit(int status);以便集线器程序处理内部程序的退出new java.io.PrintStream().println(Object o)和类似的调用并将其输出放在一个JEditorPaneJFrame内部程序的所有内容用于JInternalFrames并将它们放入a中JDesktopPane如果您不希望其他程序(通过它的主方法调用)能够关闭您正在运行的JVM,那么您可以看到三个选项:
设置SecurityManager以防止System.exit调用:
public class Test {
public static void main(String args[]) {
SecurityManager sm = System.getSecurityManager();
System.setSecurityManager(new SecurityManager() {
@Override
public void checkExit(int status) {
throw new SecurityException("Client program exited.");
}
});
try {
System.out.println("hello");
System.exit(0);
System.out.println("world");
} catch (SecurityException se) {
System.out.println(se.getMessage());
}
}
}
Run Code Online (Sandbox Code Playgroud)
印刷品:
hello
Client program exited.
Run Code Online (Sandbox Code Playgroud)
这可能是最好的解决方案.这是应用程序服务器阻止任意servlet终止整个服务器的方式.
例如,使用单独的JVM运行其他程序 ProcessBuilder
import java.io.*;
public class Test {
public static void main(String args[]) throws IOException {
ProcessBuilder pb = new ProcessBuilder("java", "other.Program");
pb.redirectErrorStream();
Process p = pb.start();
InputStream is = p.getInputStream();
int ch;
while ((ch = is.read()) != -1)
System.out.print((char) ch);
is.close();
System.out.println("Client program done.");
}
}
Run Code Online (Sandbox Code Playgroud)
不要禁止终止JVM,而是添加关闭钩子来清理"集线器"并正常退出.(如果您一次运行一个"外部"程序,此选项可能才有意义.)
import java.io.*;
public class Test {
public static void main(String args[]) throws IOException {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Uninitializing hub...");
System.out.println("Exiting gracefully.");
}
});
// Run client program
System.out.println("Running... running... running...");
System.exit(0);
}
}
Run Code Online (Sandbox Code Playgroud)
打印:
Running... running... running...
Uninitializing hub...
Exiting gracefully.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
269 次 |
| 最近记录: |