我正在尝试使用此代码以调试模式重新启动 java 程序
public static final String SUN_JAVA_COMMAND = "sun.java.command";
public static void restartApplication() throws IOException
{
try
{
String java = System.getProperty("java.home") + "/bin/java";
final StringBuffer cmd = new StringBuffer("\"" + java + "\" ");
String[] mainCommand = System.getProperty(SUN_JAVA_COMMAND).split(" ");
if (mainCommand[0].endsWith(".jar"))
{
cmd.append("-jar " + new File(mainCommand[0]).getPath());
}
else
{
cmd.append("-cp \"" + System.getProperty("java.class.path") + "\" " + mainCommand[0]);
}
cmd.append("");
for (int i = 1; i < mainCommand.length; i++)
{
cmd.append(" ");
cmd.append(mainCommand[i]);
}
cmd.append(" -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000");
Runtime.getRuntime().exec(cmd.toString());
System.exit(0);
}
catch (Exception e)
{
throw new IOException("Error while trying to restart the application", e);
}
}
但是,每次应用程序从这个 main 方法启动时
public static void main(String[] args) throws Exception
{
boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0;
JOptionPane.showMessageDialog(null, isDebug);
Utility.restartApplication();
}
joptionpane 总是显示消息 false 为什么这不起作用
是 isDebug 中的问题还是重启应用程序的问题;我认为问题是重新启动。