Skip to content

Commit ae94195

Browse files
author
JoniVR
committed
add project
1 parent 3f7bd81 commit ae94195

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

JavaVersionSwitcher.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/bash
2+
3+
# MIT License
4+
#
5+
# Copyright (c) 2018 Joni Van Roost
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
25+
if [ `id -u` -ne 0 ]
26+
then
27+
echo "Permission denied. Please start with sudo."
28+
exit 1
29+
fi
30+
31+
echo "Select a Java version you want to use:"
32+
33+
# Collect the folders in the array $folders
34+
folders=( /Library/Java/JavaVirtualMachines/* )
35+
36+
# Enable extended globbing. This lets us use @(foo|bar) to
37+
# match either 'foo' or 'bar'.
38+
shopt -s extglob
39+
40+
# Start building the string to match against.
41+
string="@(${folders[0]}"
42+
43+
# Add the rest of the folders to the string
44+
for((i=1;i<${#folders[@]};i++))
45+
do
46+
string+="|${folders[$i]}"
47+
done
48+
49+
# Close the parenthesis. $string is now @(folder1|folder2|...|folderN)
50+
string+=")"
51+
52+
# Show the menu. This will list all folders and the string "quit"
53+
select selected_java_version in "${folders[@]}" "quit"
54+
do
55+
case $selected_java_version in
56+
# If the choice is one of the folders (if it matches $string)
57+
$string)
58+
echo "Setting Java version from \"$selected_java_version\" as current version."
59+
60+
# Loop through java versions again
61+
for java_version in ${folders[@]}
62+
do
63+
# If there's no info.plist or info.plist.disabled file, notify the user.
64+
if [ ! -f "$java_version/Contents/Info.plist" ] &&
65+
[ ! -f "$java_version/Contents/Info.plist.disabled" ]
66+
then
67+
echo "No Info.plist or Info.plist.disabled file found for $java_version. \n Please check this installation."
68+
break
69+
fi
70+
71+
# When a java version does not match the selected version
72+
# if there is an Info.plist file, rename -> Info.plist.disabled
73+
if [ "$java_version" != "$selected_java_version" ] &&
74+
[ -f "$java_version/Contents/Info.plist" ]
75+
then
76+
`mv $java_version/Contents/Info.plist $java_version/Contents/Info.plist.disabled`
77+
78+
# When a java version matches the selected version
79+
# if there is no Info.plist file but there is an Info.plist.disabled, rename -> Info.plist
80+
elif [ "$java_version" == "$selected_java_version" ] &&
81+
[ ! -f "$selected_java_version/Contents/Info.plist" ] &&
82+
[ -f "$selected_java_version/Contents/Info.plist.disabled" ]
83+
then
84+
`mv $java_version/Contents/Info.plist.disabled $java_version/Contents/Info.plist`
85+
fi
86+
done
87+
88+
break;
89+
;;
90+
91+
"quit")
92+
# Exit
93+
exit;;
94+
*)
95+
selected_java_version=""
96+
echo "Please choose a number from 1 to $((${#folders[@]}+1))";;
97+
esac
98+
done
99+
100+
echo "Done!"
101+
102+
exit 0

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# JavaVersionSwitcher
2+
A bash script for easily switching which Java version your system should be using on macOS.
3+
4+
![example](https://github.com/JoniVR/JavaVersionSwitcher/blob/master/example.png)
5+
6+
## What is it?
7+
A script that will show you a list with currently installed Java versions on your system to pick one from to use.
8+
9+
## How does it work?
10+
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.
11+
12+
## How to use?
13+
1. Download or clone the script to your computer
14+
2. Open a terminal window, navigate to the location where you saved the file (`cd /<folder>/`)
15+
3. Run `chmod +x JavaVersionSwitcher.sh` to allow the user to run the script
16+
4. Run the script: `sudo ./JavaVersionSwitcher.sh`
17+
18+
## Warning
19+
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.
20+
21+
## Author
22+
Joni Van Roost, [email protected]
23+
24+
## License
25+
JavaVersionSwitcher is available under the MIT license. See the [LICENSE](https://github.com/JoniVR/JavaVersionSwitcher/blob/master/LICENSE) file for more info.
26+
27+
## More
28+
Feel free to submit a pull request, open an issue or fork this project. Any help is always appreciated.

example.png

32.3 KB
Loading

0 commit comments

Comments
 (0)