Search code examples
linuxbashadmin

Find all Java versions in the system path


I needed to know a command to find all of the versions of Java in the system path. I knew that which -a java would print all places that the java executable can be found in the system path, but I then wanted to get the version info for each of these executables.


Solution

  • which -a java | xargs -I{} echo "echo {};{} -version;echo" | sh

    This will print the path to each of the java executables found in the system path, as well as the version information of that executable, and separate entries with a newline. It works like so:

    1. Pipe the output from which -a java into xargs
    2. Use xargs -I{} echo to construct bash commands, with each line in the input from which replacing {}
    3. Pipe the constructed bash commands into the system's default shell executor.

    A sample output on my machine gives

    /usr/bin/java
    java version "1.8.0_131"
    Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
    
    /usr/lib/jvm/java-8-oracle/bin/java
    java version "1.8.0_131"
    Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
    

    Note that if you want to know the versions of a different executable, you can modify the bash command that gets constructed to appropriately print version info. E.G. if you want to know all the versions of python you can run

    which -a python | xargs -I{} echo "echo {};{} --version;echo" | sh