Table of contents
1.
Introduction
2.
What is a “Java Standalone Application”?
3.
Features of Java Standalone Application
4.
How to create a Java Standalone Application?
5.
Frequently Asked Questions
5.1.
How can we execute Java source code?
5.2.
What are the advantages of Java Standalone applications over web-based applications?
5.3.
What are GUI applications and how can we create them using Java?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Java Standalone Application

Author Nikhil Joshi
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey Ninjas!, Welcome back.

We all use desktop applications in our daily life. When you have to do a lot of calculations, you directly open the calculator app. When you have to write some notes, you directly open the Notepad app. Similarly, If you want to get a reminder, you quickly set up an alarm using the Alarm app. These all are nothing but examples of Java Standalone applications.

java-standalone-application-ogi

In this article, titled “Java Standalone Application”, we will discuss what standalone applications are and how we can make “Java Standalone Application” by taking a simple example of a daily expense tracker app.

Also read, Duck Number in Java and Hashcode Method in Java.

What is a “Java Standalone Application”?

As the name suggests, a “Java Standalone Application” is an application that can stand on its own. In other words, It does not require any other resource for its execution. 

“Java Standalone Application” does not require the Internet. 

It runs on a local machine. It takes its input from the local machine as well saves its data to that local machine. Apart from the Internet, “Java standalone application” does not require any other process for its functionality. 

They are true “Aatmnirbhar”.

Also read, Swap Function in Java

Features of Java Standalone Application

Below are mentioned features of Java Standalone application: 

features-of-standalone-app
  1. Installation: Java Standalone Application requires Installation. Using them through a browser like we use web applications is impossible.
     
  2. Self-contained: Java Standalone Applications are self-contained. They contain all libraries and code files required to run the application. They run smoothly and don’t have any external dependencies.
     
  3. Offline operation: Java Standalone Application works in offline mode. They don’t require any kind of network connectivity for their functioning.
     
  4. User-Interface: Generally, Java Standalone Applications have Graphical User Interface. It is not mandatory, though; we can create command-line standalone applications, also.
     
  5. Data Storage: They store data on the local machine, making it easy to access and less vulnerable to attacks.
     
  6. Performance and Security: Java Standalone applications work more smoothly than web apps since they don’t require connectivity or other process dependencies. Their code is not exposed to the web, which makes them more secure.

How to create a Java Standalone Application?

The following are steps to create a Java Standalone Application:-

1. Create a logic file: First, you must create a Java source code file containing your business logic. In our example, the Java source code file will contain code for the expense tracker.

Following is our source code file ExpenseTracker.java
 

import java.util.ArrayList;
import java.util.Scanner;

public class ExpenseTracker {
    
    /*Record class object will contain record of each transaction
    * money will store the amount used in transaction ,-ve money stands for expense , +ve stands for income
    * recordInfo will store information about that string
    * for example , money = 200 , recordInfo = barber shop. */
    static class Record{
        int money;
        String recordInfo;

        // constructor for Record object
        Record(int money, String recordInfo){
            
            this.money = money;
            this.recordInfo = recordInfo;
        }
        
        // helpful in case we want to print record object
        @Override
        public String toString() {
            return "Money : "+money+"\n"+"record Info: "+recordInfo+".";
        }
    }
    
    // will store all records
    static ArrayList<Record> recordList = new ArrayList<>();
    // will store total balance , either +ve or -ve
    static int totalBalance;

    private static int getTotalBalance() {
        return totalBalance;
    }
    
    /* add a new record in recordList and update the total balance*/
    private static void addRecord(Record r){
        recordList.add(r);
        totalBalance += r.money;
    }

    /* remove the said record from recordList and update the total balance */
    private static boolean removeRecord(int i){
        try{
            Record removed = recordList.remove(i);
            totalBalance -= removed.money;
        }
        catch (Exception e){
            return false;
        }


        return true;
    }

    /* print all the record , one by one*/
    static void printRecords(){
        for(Record r : recordList){
            System.out.println(r);
        }
        System.out.println();
    }

    /* helper fn to print choice list */
    private static void printMainFn(){
        System.out.println("Hi... I'm your expense manager");
        System.out.println("Choose one out of following option :");
        System.out.println("1. add a record");
        System.out.println("2. delete a record");
        System.out.println("3. view all records");
        System.out.println("4. Tell me my balance");
        System.out.println("5. exit");
        System.out.println("Type 1 for option 1 and so on ...");
    }

    public static void main(String[] args) {
        /* Scanner for taking input from user*/
        Scanner sc = new Scanner(System.in); 
        
        // run loop until user exit out by choosing option 5
        while (true) {
            // print choice list
            printMainFn(); 
            // ask for user choice 
            String choice = sc.nextLine();


            
            if (choice.equals("1")) {
                /* user want to add a new record*/
                System.out.println("add a new record");
                System.out.println("In first line add amount. +ve for income , -ve for expense");
                System.out.println("In second line, give an information statement about it");


                /* take amount of transaction input from user in string form*/
                String moneyInStringForm = sc.nextLine();
                /* convert that amount to Integer form*/
                int money = Integer.parseInt(moneyInStringForm);
                sc.nextLine();
                /* ask user for statement about transaction */
                String statement = sc.nextLine();
                System.out.println("statement is : "+statement);


                // make a new record
                Record newRecord = new Record(money,statement);
                addRecord(newRecord);


                System.out.println("Updated total balance is ... "+getTotalBalance());


            } else if (choice.equals("2")) {
                /* user want to delete a record , print all record so user can decide*/
                System.out.println("Following are all records , choose a record index (0-based) to delete");
                /* ask user record it want to delete*/
                String toDeleteIndexInStringForm = sc.nextLine();
                int toDeleteIndex = Integer.parseInt(toDeleteIndexInStringForm);


                /* delete the said record*/
                boolean isDeleted = removeRecord(toDeleteIndex);
                if(isDeleted){
                    System.out.println("Record is deleted ...");
                    System.out.println("Updated balance is "+getTotalBalance());
                }
                else{
                    /* can not delete the record, may be due to wrong index*/
                    System.out.println("fail to remove the record ...");
                }
                
            } else if (choice.equals("3")) {
                /* print main choice list */
                printRecords();
            } else if (choice.equals("4")) {
                /* print total balance*/
                System.out.println("Your balance is "+getTotalBalance());
            } else if (choice.equals("5")) {
                /* user choose to exit from app*/
                break;
            } else {
                /* user give wrong input*/
                System.out.println("Wrong input , again type correct input");
            }


            System.out.println();
            sc.nextLine();
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


2. Compile your Java source code file to your class file. Use command javac ExpenseTracker.java

Before command:

before-compilation-Expense-tracker-folder-structure

After command:

after-compilation-expense-tracker

Notice, ExpenseTracker, and Record class is created after execution of the command.
 

3. Create another source code Main.java

import java.io.IOException;

public class Main{
    public static void main(String[] args) {
        try{
            Runtime rt = Runtime.getRuntime();
            rt.exec("cmd.exe /c start cmd.exe /k \"java ExpenseTracker\"");
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


We are making an object of Runtime and then asking it to run ExpenseTracker.class.

4. Compile Main.java using javac Main.java.

before-compilation-main-folder-structure

Notice Main.class is created after the execution of the command.

Note: This command may give a message for depreciated API in case of a newer version of Java. Just run using javac Main.java -Xlint:deprecation 
 

5. Run the following command to create a jar file for MyExpenseTracker application.

jar cfe MyExpenseTracker.jar Main *.class

after-creating-jar-file-structure

Notice, Jar file for the application is created.

6. MyExpenseTracker.jar will be created. Now just double-click on that icon to open your app.

app-visual

Now, you can use ExpenseTracker.

Also Read, addition of two numbers in java

Frequently Asked Questions

How can we execute Java source code?

Java source code file is first compiled using java Filename.java command. It will create a separate .class file for each class present in Filename. Then we can java Filename.class command to run our file.   

What are the advantages of Java Standalone applications over web-based applications?

Java Standalone applications have better performance than web-based applications. They don’t require internet connectivity. They are fast. Since they store data on local machines, they can be used for applications where data security is very important.

What are GUI applications and how can we create them using Java?

A graphical user interface (GUI) application contains different visual features such as windows, menus, and checkboxes. These graphic features make the application more interactive and easier for the user. We can use libraries like JavaFx and Swing to make GUI applications using Java.

Conclusion

In conclusion, Java standalone applications can work on their own. Neither they require connectivity to the Internet nor support from other applications for their functionality. They run on local machines, which makes them less vulnerable to attacks. This also makes them function smoothly. But this also makes it non-scalable. They are less flexible, and they don’t have the ability to share resources among people. Hence, we should use standalone applications keeping advantages and disadvantages in our mind.

Recommended Reading:

Difference Between Analog and Digital Computer

Don’t know which is the best Java compiler? Check it out here. You can also read about Java Development Kit (JDK) here.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Learning!

Live masterclass