gpt4 book ai didi

java - 如何使用运行时命令从另一个 JAR 启动一个 JAR?

转载 作者:行者123 更新时间:2023-12-01 06:16:44 24 4
gpt4 key购买 nike

这是我正在使用的代码:

public class CLASSNAME extends JFrame
{

public static String MYAPP = new String(Util.getWorkingDirectory() + File.separator + "MYAPP.jar");
public static File MYJAR = new File(MYAPP); //used in other sections of the code

然后,当我调用运行时命令时,它不会启动,它可以在 Linux 上运行,但不能在 Windows 上运行(也可能在 MAC 上运行):

public static final void launching() throws Exception {
try {

Runtime.getRuntime().exec (MYAPP);
System.exit(0);
} catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}
}

编辑:这就是我使代码工作的方式(希望对其他人有帮助):

public class CLASSNAME extends JFrame
{

public static String MYAPP = new String(Util.getWorkingDirectory() + File.separator + "MYAPP.jar");

//THE JFRAME CODE AND THEN:

public static final void launching() throws Exception {
try {
String osName = System.getProperty("os.name");

if(osName.startsWith("Win")) {
Runtime.getRuntime().exec (new String[]{"javaw","-jar",MYAPP});
System.exit(0);
}
if(osName.startsWith("Linux")) {
Runtime.getRuntime().exec (new String[]{"java","-jar",MYAPP});
System.exit(0);
}
if(osName.startsWith("Mac")) {
Runtime.getRuntime().exec (new String[]{"java","-jar",MYAPP});
System.exit(0);;
}


}catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}
}

这样在linux上即使没有被标记为可运行位也可以运行MYAPP

最佳答案

Runtime.getRuntime().exec(command)

如果你能在终端或者cmd中运行该命令,那就对了。

关于java - 如何使用运行时命令从另一个 JAR 启动一个 JAR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22830876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com