Skip to content

Commit

Permalink
add project
Browse files Browse the repository at this point in the history
  • Loading branch information
JoniVR committed Sep 30, 2018
1 parent 3f7bd81 commit ae94195
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
102 changes: 102 additions & 0 deletions JavaVersionSwitcher.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/bash

# MIT License
#
# Copyright (c) 2018 Joni Van Roost
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

if [ `id -u` -ne 0 ]
then
echo "Permission denied. Please start with sudo."
exit 1
fi

echo "Select a Java version you want to use:"

# Collect the folders in the array $folders
folders=( /Library/Java/JavaVirtualMachines/* )

# Enable extended globbing. This lets us use @(foo|bar) to
# match either 'foo' or 'bar'.
shopt -s extglob

# Start building the string to match against.
string="@(${folders[0]}"

# Add the rest of the folders to the string
for((i=1;i<${#folders[@]};i++))
do
string+="|${folders[$i]}"
done

# Close the parenthesis. $string is now @(folder1|folder2|...|folderN)
string+=")"

# Show the menu. This will list all folders and the string "quit"
select selected_java_version in "${folders[@]}" "quit"
do
case $selected_java_version in
# If the choice is one of the folders (if it matches $string)
$string)
echo "Setting Java version from \"$selected_java_version\" as current version."

# Loop through java versions again
for java_version in ${folders[@]}
do
# If there's no info.plist or info.plist.disabled file, notify the user.
if [ ! -f "$java_version/Contents/Info.plist" ] &&
[ ! -f "$java_version/Contents/Info.plist.disabled" ]
then
echo "No Info.plist or Info.plist.disabled file found for $java_version. \n Please check this installation."
break
fi

# When a java version does not match the selected version
# if there is an Info.plist file, rename -> Info.plist.disabled
if [ "$java_version" != "$selected_java_version" ] &&
[ -f "$java_version/Contents/Info.plist" ]
then
`mv $java_version/Contents/Info.plist $java_version/Contents/Info.plist.disabled`

# When a java version matches the selected version
# if there is no Info.plist file but there is an Info.plist.disabled, rename -> Info.plist
elif [ "$java_version" == "$selected_java_version" ] &&
[ ! -f "$selected_java_version/Contents/Info.plist" ] &&
[ -f "$selected_java_version/Contents/Info.plist.disabled" ]
then
`mv $java_version/Contents/Info.plist.disabled $java_version/Contents/Info.plist`
fi
done

break;
;;

"quit")
# Exit
exit;;
*)
selected_java_version=""
echo "Please choose a number from 1 to $((${#folders[@]}+1))";;
esac
done

echo "Done!"

exit 0
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# JavaVersionSwitcher
A bash script for easily switching which Java version your system should be using on macOS.

![example](https://github.com/JoniVR/JavaVersionSwitcher/blob/master/example.png)

## What is it?
A script that will show you a list with currently installed Java versions on your system to pick one from to use.

## How does it work?
It works by renaming the `Info.plist` files of all other versions inside their respective `Library/Java/JavaVirtualMachines/jdk{version}.jdk/Contents/` folders to `Info.plist.disabled`, so that only the selected version will have an `Info.plist` file, this way, only the selected version will work.

## How to use?
1. Download or clone the script to your computer
2. Open a terminal window, navigate to the location where you saved the file (`cd /<folder>/`)
3. Run `chmod +x JavaVersionSwitcher.sh` to allow the user to run the script
4. Run the script: `sudo ./JavaVersionSwitcher.sh`

## Warning
This script modifies your files (`Info.plist` files inside Java installation folders), this means that Java versions won't work unless you either run the script again and select a version or rename the `Info.plist.disabled` file to `Info.plist` inside `Library/Java/JavaVirtualMachines/jdk{version}.jdk/Contents/`. Use at your own risk.

## Author
Joni Van Roost, [email protected]

## License
JavaVersionSwitcher is available under the MIT license. See the [LICENSE](https://github.com/JoniVR/JavaVersionSwitcher/blob/master/LICENSE) file for more info.

## More
Feel free to submit a pull request, open an issue or fork this project. Any help is always appreciated.
Binary file added example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ae94195

Please sign in to comment.