How to Run Java in Atom

Rupam Yadav Feb 02, 2024
  1. Use the script Package to Run Java in Atom Text Editor
  2. Configure Custom Build Scripts to Run Java in Atom Text Editor
  3. Use Terminal-Plus Package to Run Java in Atom Text Editor
  4. Conclusion
How to Run Java in Atom

Atom is an open-source text editor with all the features that a text editor should have. It is a cross-platform software that can be installed on all common operating systems like Windows, Linux, and Mac.

Running Java in Atom can be achieved through various methods, allowing developers to harness the power of this popular programming language within the Atom text editor. Here, we’ll explore several approaches, each with its own set of advantages and steps to execute Java code within Atom.

Use the script Package to Run Java in Atom Text Editor

Java, renowned for its versatility and widespread usage in software development, can be efficiently handled within the Atom text editor through the utilization of build system packages. These packages automate the process of compiling and executing Java code, enhancing the coding experience for Java developers.

While several build system packages are available in Atom, the script package stands out for its flexibility and customization options.

  • Download and install the latest Java Development Kit (JDK) from Oracle’s website.
  • Add the downloaded JDK’s path to the Environment Variables.
  • Download the Atom Text Editor from its official website and install it.
  • After installation, go to the Packages menu and look for Settings View in the dropdown.
  • In the Settings View, click the Install Packages/Themes option.
  • In the Install Packages window, search for the package script and look for the package by atom-community.

    The script package in Atom allows users to run code directly within the editor. Although it primarily supports languages like JavaScript, Python, and Ruby, with the right configurations, it can execute Java code too.

  • Click on the Install button and wait for a few moments to finish the download.
  • Once the installation is finished, head over to the File menu and create a New File.
  • In the newly created file, we write the following code to check if the code runs.
    class JavaExample {
      public static void main(String args[]) {
        System.out.println("Hello World");
      }
    }
    
  • Now, to run Java codes, we use the shortcut Ctrl+Shift+B (Windows/Linux) or Cmd+I (macOS), and the output should be the following:
    Hello World
    [Finished in 1.5s]
    

You can also install the script package in the terminal:

# Install the script package
apm install script

The script package facilitates the execution of Java code within Atom by integrating with the editor.

After installing the package, users can write Java code in a file with the .java extension and execute it via the package.

// Example Java code
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}

Save the Java code in a file, open it in Atom, and use the script package to execute it. Simply press the corresponding shortcut (Ctrl+Shift+B for Windows/Linux or Cmd+I for macOS) to run the Java code.

Configure Custom Build Scripts to Run Java in Atom Text Editor

Atom, with its flexibility and extensibility, allows developers to create custom-build scripts for various programming languages, including Java. Custom build scripts provide a more tailored approach, enabling developers to incorporate specific commands, configurations, and build processes according to their project requirements.

Prerequisites

Ensure the following prerequisites are met before setting up custom build scripts:

  • Atom Text Editor installed on your system.
  • Java Development Kit (JDK) installed and configured properly.

Steps to Configure Custom Build Scripts in Atom

  • Create a New Atom Project

    Launch Atom and open or create a directory for your Java project.

  • Create a package.json File

    In the root directory of your project, create a package.json file.

    {
        "name": "java-build",
        "version": "1.0.0",
        "scripts": {
            "start": "javac FileName.java && java FileName"
        }
    }
    

    Replace FileName with the name of your Java file (without the .java extension).

    • "name": Specifies the name of the project.
    • "version": Defines the version of the project.
    • "scripts": Contains the build script commands.
    • "start": Executes the build commands to compile and run the Java code.
  • Install Dependencies

    Install atom-build package in Atom if not already installed:

    • Go to File > Settings > Install.

    • Search for "atom-build" and click "Install".

  • Configure Atom-Build for Java

    Open the build.json file:

    • Navigate to File > Settings > Packages > atom-build > Settings.

    • Scroll down to "Custom Build Tools".

    • Click on "Edit Configuration" for the "Java" build tool.

    Add a new configuration block for Java:

    {
        "cmd": "npm start",
        "name": "Java Build",
        "args": []
    }
    
  • Executing the Custom Build Script

    Write your Java code in a .java file within Atom. To trigger the build process, use Ctrl+Alt+B (Windows/Linux) or Cmd+Alt+B (macOS).

Consider a simple Java program that calculates the sum of two numbers:

public class SumCalculator {
  public static void main(String[] args) {
    int num1 = 5;
    int num2 = 7;
    int sum = num1 + num2;
    System.out.println("The sum is: " + sum);
  }
}

Output in Atom:

The sum is: 12
[Finished in 0.484s]

In this code, the SumCalculator class contains a main method, the entry point of the program. It declares two integer variables, num1 and num2, with values 5 and 7, respectively.

The sum variable stores the addition of num1 and num2. Finally, the System.out.println() statement displays the computed sum on the console.

By configuring custom build scripts in Atom, developers can effectively compile and run Java code within the editor, offering flexibility and customization in the build process to meet project-specific requirements.

Use Terminal-Plus Package to Run Java in Atom Text Editor

Terminal-Plus is a versatile Atom package that enables users to access and utilize a terminal within the Atom text editor itself.

This feature empowers developers to execute commands, compile code, and run programs without switching between the editor and an external terminal window. Utilizing Terminal-Plus for Java facilitates a smooth and streamlined coding experience.

Prerequisites

Ensure the following prerequisites are met before using Terminal-Plus for Java in Atom:

  • Atom Text Editor installed on your system.
  • Java Development Kit (JDK) installed and configured properly.

Steps to Use Terminal-Plus for Java in Atom

  • Install Terminal-Plus:

    Open Atom and access the Package Manager; go to File > Settings > Install. Search for "terminal-plus" and install the package.

  • Open Terminal-Plus in Atom:

    To launch Terminal-Plus within Atom, press Ctrl+` (Backtick) or navigate to Packages > Terminal Plus > New Terminal.

  • Use the terminal to navigate to the directory containing your Java file:

    cd path/to/your/java/project
    
  • Compile Java Code:

    Compile the Java code using the javac command:

    javac FileName.java
    

    Replace FileName with the name of your Java file (without the .java extension).

  • Run Compiled Java Program:

    Execute the compiled Java program using the java command:

    java FileName
    

    Replace FileName with the actual Java file name (without the .java extension).

  • Understanding Output:

    Check the terminal for compilation errors or the output of the executed Java program.

Consider a simple Java program that calculates the sum of two numbers:

public class SumCalculator {
  public static void main(String[] args) {
    int num1 = 5;
    int num2 = 7;
    int sum = num1 + num2;
    System.out.println("The sum is: " + sum);
  }
}

Output in Atom:

The sum is: 12
[Finished in 0.484s]

In this code, the SumCalculator class contains a main method, the entry point of the program. It declares two integer variables, num1 and num2, with values 5 and 7, respectively.

The sum variable stores the addition of num1 and num2. Finally, the System.out.println() statement displays the computed sum on the console.

Using Terminal-Plus in Atom for Java provides a convenient and efficient way to compile and execute Java code without leaving the editor, ensuring a smoother development experience for Java projects.

Conclusion

Atom, a versatile and open-source text editor, offers various methods to run Java code seamlessly within its interface. Throughout this guide, we’ve explored three distinct approaches:

  • Utilizing the script Package: This method integrates code execution directly within Atom, providing a quick and straightforward way to compile and run Java code.
  • Configuring Custom Build Scripts: For developers seeking more control and customization over the build process, configuring custom build scripts in Atom allows tailored compilation and execution of Java code.
  • Using Terminal-Plus Package: Terminal-Plus offers an interactive terminal within Atom, enabling users to compile and execute Java code without leaving the editor.

Each method has its advantages, catering to different preferences and project requirements. The script package simplifies execution with its direct integration, while custom-build scripts offer fine-grained control.

Meanwhile, Terminal-Plus provides an interactive terminal experience, facilitating command execution within the editor.

Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn