Setting up Java Competitive Programming Environment - GeeksforGeeks

10 May, 2022
An operating system is required to be installed on your system. here we will be discussing the setup in windows. However, you can choose any operating system. Install JDK (Java Development Kit) JDK, is a program that allows you to write Java code from the comfort of your desktop. It contains a variety of tools that are very useful for creating, running, and optimizing your Java code. Follow the steps to download JDK:
Download the latest version of JDK.
After downloading, install the JDK by following through the prompts after running the installer.
There are quite a number of good text editors available these days like VS Code, Sublime Text, Atom, Notepad++, etc. However, this article will use Sublime Text 3 because it being lightweight, minimally aesthetic, and high functionalities. Download sublime text and install it to set up a java build system.
Step A: Setting up a Java Build System
Follow the steps below to set up the build system for JAVA so that you can compile java code where here e will be demonstrating over windows operating systems
Go to command prompt and type “where java” and copy the path of the JDK bin folder which looks like “C:\Program Files\Java\jdk1.8.0_251\bin\”Now, open the sublime text and go to Tools > Build System > New Build System.A new file must appear where now you need to paste the JSON object below in that fileReplace the path variable with the path that you got in Step 1
Go to command prompt and type “where java” and copy the path of the JDK bin folder which looks like “C:\Program Files\Java\jdk1.8.0_251\bin\”
Now, open the sublime text and go to Tools > Build System > New Build System.
A new file must appear where now you need to paste the JSON object below in that file
Replace the path variable with the path that you got in Step 1
{ 
"cmd": ["javac", "$file_name", "&&", "java" ,"$file_base_name"],  
"selector": "source.java",
"file_regex": "^\\s*File \"(...*?)\", line ([0-9]*)", 
"path" :  "C:\\Program Files\\Java\\jdk1.8.0_251\\bin\\",
"shell":true
}
Now follow these three simple steps
Save the file by pressing CTRL + S and save by giving a name for example “MYJAVA” to the build system at the folder which is prompted.Remember that the file extension of the build system should be “sublime-build” otherwise you will not be able to see the option for the build system which you created.Go to Tools > Build System, you must see a build system with the name with which you saved the file in Step 4, in this case, “MYJAVA”.Mark that build system as tick by clicking on it.
Save the file by pressing CTRL + S and save by giving a name for example “MYJAVA” to the build system at the folder which is prompted.
Remember that the file extension of the build system should be “sublime-build” otherwise you will not be able to see the option for the build system which you created.
Go to Tools > Build System, you must see a build system with the name with which you saved the file in Step 4, in this case, “MYJAVA”.
Mark that build system as tick by clicking on it.
If the above steps are followed then the build system is ready to use. Now the task is in
Step B: Set up the sublime tabs
1. During contests, it becomes tedious to switch between the tabs, so you can set up the tabs so that you can view each one. Follow the steps to take so:
Close all the tabs if any file opened.
Go to View > layout > Columns 3. You will see the layout something like below.
2. Be in the first column and go to View > Groups > Max Column 2. You will see the layout something like below.
3. In this set up the left window will contain the code file and the right upper window will contain the input file and the left lower window will contain the output file as we will see further.
Illustration: Write the hello world program
1. Create a folder that will contain three files, a java file named hello.java, an input file named input.txt which will be used for taking inputs, and an output file used for storing the outputs named output.txt, make sure all the three files are in the same folder.
2. In the screen set up that we did earlier go to file > open file and open the java file in the left window and open the input file in the right upper window and open the output file in the right lower window. Something like below
3. Paste the below standard template for JAVA containing the hello world program in the java file make sure that the file name and the class name are the same and make the class public. The code will take a string input from “input.txt” and will print it in the “output.txt” by appending it to “world!”.
Example:
Java
// Java Program that is been setup in Sublime Text// for Competitive Coding  // Importing input output classesimport java.io.*;// Importing Scanner class from java.util packageimport java.util.Scanner;  // Main Classclass hello {      // Main driver method    public static void main(String[] args)    {          // Setting up the input stream        // You can use buffered reader too        Scanner read = new Scanner(System.in);          // If You Are Running Your Code        // in Sublime Text then set The        // System Out to output.txt and        // Input Stream to input.txt        // otherwise leave it as standard        // ones for ONLINE JUDGE        if (System.getProperty("ONLINE_JUDGE") == null) {            // Try block to check for exceptions            try {                // Sets the Output Stream                // to output.txt                System.setOut(new PrintStream(                    new FileOutputStream("output.txt")));                  // Change the input stream                // to input.txt                read = new Scanner(new File("input.txt"));            }              // Catch block to handle the exceptions            catch (Exception e) {            }        }          // Your Code Start Here          // Read input        String inp = read.nextLine();          // Print output        System.out.println(inp + " World!");    }}
  
Output:
 
Write something like “Hello” in the “input.txt”.
Make sure you have selected the correct build system that we built earlier in Tools > Build System.
Now hit CTRL + B or go to Tools > Build to compile your code.
Your code must compile and something should get printed in your “output.txt” file. Something like below.
 
If a window like the above is seen then your setup is complete and ready to code JAVA in sublime and submit your code on ONLINE JUDGES without worrying about changing the input and output streams as it is already been taken care of. One can go to preferences and change the font or theme as you like.
 
rajrk2024
Java-Competitive-Programming
Competitive Programming
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multistage Graph (Shortest Path)
Breadth First Traversal ( BFS ) on a 2D array
Check whether bitwise AND of a number with any subset of an array is zero or not
Shortest path in a directed graph by Dijkstra’s algorithm
5 Best Books for Competitive Programming
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
Arrays.sort() in Java with examples