I am trying to merge 2 simple programs I have made to one .jar. I packed both .jars into a new one and used Runtime.getRuntime().exec method to execute them.
Code:
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*/
}
}
}
}
The problem is that only proj1.jar is executed and proj2.jar doesn't run. I am new to java and don't know why this happens. How do I fix this? I want both files to be executed.
Your problem is that your second file is ONLY executed if the first throws an exeception. You're looking for this:
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*/
}
}
}
Or, if you absolutely must handle the exceptions separately, this:
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
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.