How do I terminate a Java application?
Category: java.lang, viewed: 2523 time(s).
In an application we sometimes want terminate the execution of our application, for instance because it cannot find the required resource.
To terminate it we can use exit(status) method in java.lang.System class or in the java.lang.Runtime class. When terminating an application we need to provide a status code, a non-zero status assigned for any abnormal termination.
package org.kodejava.examples.lang; import java.io.File; public class AppTerminate { public static void main(String[] args) { File file = new File("config.xml"); int errCode = 0; if (!file.exists()) { errCode = 1; } else { errCode = 0; } // When the error code is not zero go terminate if (errCode > 0) { System.exit(errCode); } } } |
The call to System.exit(status) is equals to Runtime.getRuntime().exit(status). Actually the System class will delegate the termination process to the Runtime class.
Related Examples
- How do I read system property as an integer?
- How do I decode string to integer?
- How do I insert a string in the StringBuilder?
- How do I remove substring from StringBuilder?
- How do I reverse a string by word?
- How do I convert varargs to an array?
- How do I remove trailing white space from a string?
- How do I remove leading white space from a string?
- How do I create a method that accept varargs in Java?
- How do I know a class of an object?
|
|