Category Archives: Java

Select different available java and javac versions in Ubuntu with update-alternatives

It’s possible to have more than one version of Java in the same machine.

If you have more than one Java JRE or JDK installed you can switch between them with the update-alternatives command:

$ sudo /usr/sbin/update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                           Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java   1061      auto mode
  1            /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java   1061      manual mode
  2            /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java   1051      manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java to provide /usr/bin/java (java) in manual mode.

You can do just the same for javac:

$ sudo /usr/sbin/update-alternatives --config javac

There are 2 choices for the alternative javac (providing /usr/bin/javac).

  Selection    Path                                        Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-6-openjdk-i386/bin/javac   1061      auto mode
  1            /usr/lib/jvm/java-6-openjdk-i386/bin/javac   1061      manual mode
  2            /usr/lib/jvm/java-7-openjdk-i386/bin/javac   1051      manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-7-openjdk-i386/bin/javac to provide /usr/bin/javac (javac) in manual mode.

You can check wich version is currently in use running:

$ java -version
java version "1.7.0_25"
OpenJDK Runtime Environment (IcedTea 2.3.10) (7u25-2.3.10-1ubuntu0.12.04.2)
OpenJDK Server VM (build 23.7-b01, mixed mode)
$ javac -version
javac 1.7.0_25

Run a command from a Java application dealing properly with stdin, stdout and stderr

If you want to run an external command from a Java application you have to deal properly with the input, output and error file descriptors or it won’t work. The key is to read the output and error buffers. This is a simple example on how to do this:

import java.io.*;

public class CmdExec {

  public static void main(String argv[]) {
    try {
      String line;
      OutputStream stdin = null;
      InputStream stderr = null;
      InputStream stdout = null;

      // launch the command and grab stdin/stdout and stderr
      Process process = Runtime.getRuntime().exec("ls -la");
      stdin = process.getOutputStream();
      stderr = process.getErrorStream();
      stdout = process.getInputStream();

      // You could write to sdtin too but it's useless for the ls we are doing ;)
      line = "param1" + "\n";   
      stdin.write(line.getBytes() );
      stdin.flush();

      line = "param2" + "\n";
      stdin.write(line.getBytes() );
      stdin.flush();

      stdin.close();
      
      // clean up if any output in stdout
      BufferedReader brCleanUp = new BufferedReader(new InputStreamReader(stdout));
      while ((line = brCleanUp.readLine()) != null) {
        System.out.println ("[Stdout] " + line);
      }
      brCleanUp.close();
      
      // clean up if any output in stderr
      brCleanUp = 
        new BufferedReader (new InputStreamReader (stderr));
      while ((line = brCleanUp.readLine ()) != null) {
        //System.out.println ("[Stderr] " + line);
      }
      brCleanUp.close();
    } catch (Exception err) {
      err.printStackTrace();
    }
  }
}