Welcome to Kodejava

Kodejava website provides Java examples to use the Java API (Application Programming Interface) to build Java applications. In this website you will find a lot of examples grouped by the Java API packages or libraries.

You can find a solution in form of code snippets ranging from core Java, JDBC database programming, web frameworks, Servlet, JSP, etc, to help you solve your problems, and it is always free.

Enjoy your study, come and visit this web site regularly to find more and more examples on Java programming. We hope that learning from examples will speed up your learning into Java.

--
I Wayan Saryada
Webmaster


How do I terminate a Java application?

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.example.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.