Programming Tools & Computer Science I

CSC 185 & 201, Spring 2013, Northern Virginia Community College

Week 4: Java and the command line

2013-02-05

We'll cover two of the simplest ways of giving input to your programs: command line arguments and environment variables.

Command line arguments

Every program you run on the command line is given access to a list of everything that you type after the program name -- those are the command line arguments. Even if you don't run a program from the command line, there are ways of specifying the command line arguments; for an example in Windows, right-click on any program shortcut and view its properties to see what command line arguments are specified for it.

In Java, the command line arguments are accessed through the String[] args array that you see in every program's public static void main(String[] args) declaration. Here's a simple example that prints all the arguments labeled by their index in the args array:


public class Args {
    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println(i + ": " + args[i]);
        }
    }
}

Just in case you need a quick review of how to compile and run:

You might need to update your PATH environment variable so that the shell can find javac. To do that, you can run the command set PATH=%PATH%;C:\Program Files\Java\jdk1.7.0\bin (possibly with a slightly different path for the jdk directory).

Environment variables

Every program is also given a dictionary of named values called environment variables. When a process is created, it inherits its own copy of the environment variables from its parent. Environment variables provide a convenient way to provide sets of input values that will often remain the same when running several programs.

An important example on major operating systems is the PATH variable, which lists the directories that are searched for programs when you type a program name on the command line.

In Windows, you can see the values of environment variables in a cmd shell by using the set or echo commands. set PATH will display the value of the PATH variable (and any other variable whose name starts with "PATH"). echo %PATH% will print the value of just the PATH variable. In general, echo prints exactly what you type on the command line, with variables enclosed in % filled in. Use the /? flag to see more information: set /?.

In Java, you can access environment variables through the System.getenv methods. Here's an example that prints out your PATH setting:


public class EnvPath {
    public static void main(String[] args) {
        System.out.println(System.getenv("PATH"));
    }
}

Setting environment variables that are passed to new processes is also possible, but requires some more advanced concepts and classes. This example (in Windows) launches a new command line shell with some new environment variables set:


import java.io.IOException;
import java.util.Map;

public class EnvSet {
    public static void main(String[] args) {
        ProcessBuilder proc = new ProcessBuilder();
        // Configure to start a new command shell
        // with given command line arguments passed:
        proc.command("cmd", "/c", "start");
        // Modify environment to contain new variables:
        Map env = proc.environment();
        env.put("MON", "Meatloaf");
        env.put("TUES", "Taco");
        env.put("WED", "Wienerschnitzel");
        // Run the command, or report an error if it failed
        try {
            proc.start();
        } catch (IOException ex) {
            System.err.println("Error starting cmd:" + ex);
        }
    }
}