use*_*111 2 java runtime runtime.exec
我正在尝试将我制作的2个简单程序合并到一个.jar中.我将两个.jars打包成一个新的并使用Runtime.getRuntime().exec方法来执行它们.
码:
public class main {
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("cmd /c proj1.jar");
} catch(Exception exce){
/*handle exception*/
try {
Runtime.getRuntime().exec("cmd /c proj2.jar");
} catch(Exception exc){
/*handle exception*/
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是只执行proj1.jar而proj2.jar不运行.我是java新手,不知道为什么会这样.我该如何解决?我想要执行这两个文件.
你的问题是,如果第一个文件抛出一个exeception,你的第二个文件才会被执行.你在找这个:
public class main {
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("cmd /c proj1.jar");
Runtime.getRuntime().exec("cmd /c proj2.jar");
} catch(Exception exce){
/*handle exception*/
}
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您绝对必须单独处理异常,请执行以下操作:
public class main {
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("cmd /c proj1.jar");
} catch(Exception exce){
/*handle exception*/
}
try {
Runtime.getRuntime().exec("cmd /c proj2.jar");
} catch (Exception e) {
//handle the exception
}
}
}
Run Code Online (Sandbox Code Playgroud)