-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathinstall_dependencies.sh
More file actions
executable file
·957 lines (870 loc) · 38.3 KB
/
install_dependencies.sh
File metadata and controls
executable file
·957 lines (870 loc) · 38.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
#!/usr/bin/env bash
set -e
OS="$(uname -s)"
ARCH="$(uname -m)"
echo "========================================"
echo " TRON Java Dependencies Installer"
echo "========================================"
echo ""
echo ">>> Environment Detection"
if [[ "$OS" == "Darwin" ]]; then
echo " OS: macOS $OS"
else
echo " OS: $OS"
fi
echo " Architecture: $ARCH"
# Validate OS and architecture support first
if [[ "$OS" != "Darwin" && "$OS" != "Linux" ]]; then
echo "Error: Unsupported OS $OS"
exit 1
elif [[ "$OS" == "Darwin" ]]; then
if [[ "$ARCH" != "x86_64" && "$ARCH" != "arm64" ]]; then
echo "Error: Unsupported architecture for macOS: $ARCH"
exit 1
fi
else
if [[ "$ARCH" != "x86_64" && "$ARCH" != "aarch64" && "$ARCH" != "arm64" ]]; then
echo "Error: Unsupported architecture for Linux: $ARCH"
exit 1
fi
fi
echo ""
echo ">>> Tested platforms:"
echo " - macOS x86_64 (JDK 8)"
echo " - macOS arm64 (JDK 17)"
echo " - Linux x86_64 (generic, including Ubuntu) (JDK 8)"
echo " - Linux arm64/aarch64 (generic, including Ubuntu) (JDK 17)"
echo " Note: Other platforms may require manual installation if errors occur"
echo ""
echo ">>> This script will install the following components if not already installed:"
if [[ "$OS" == "Darwin" ]]; then
echo " 1. Homebrew to download and install JDK (macOS only)"
echo " 2. Git for cloning Github repository"
else
echo " 1. Git for cloning Github repository"
fi
if [[ "$OS" == "Darwin" ]]; then
if [[ "$ARCH" == "x86_64" ]]; then
echo " 3. OpenJDK 8 (required for x86_64 architecture)"
else
echo " 3. OpenJDK 17 (required for arm64 architecture)"
fi
else
if [[ "$ARCH" == "x86_64" ]]; then
echo " 2. OpenJDK 8 (required for x86_64 architecture)"
else
echo " 2. OpenJDK 17 (required for arm64/aarch64 architecture)"
fi
fi
echo ""
# Function to ask for user confirmation
ask_confirmation() {
while true; do
read -p "Do you want to continue? (y/N): " yn
case $yn in
[Yy]* ) return 0;;
[Nn]* | "" ) echo "Installation cancelled."; exit 0;;
* ) echo "Please answer yes (y) or no (n).";;
esac
done
}
# Function to check Java version
check_java_version() {
if command -v java &> /dev/null; then
local java_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2)
echo " Current Java version: $java_version"
# Check if it's JDK 8 (version starts with 1.8)
if [[ "$java_version" =~ ^1\.8\. ]]; then
echo " JDK 8 is installed."
return 0
# Check if it's JDK 17 (version starts with 17)
elif [[ "$java_version" =~ ^17\. ]]; then
echo " JDK 17 is installed."
return 1
else
echo " Different Java version detected: $java_version"
return 2
fi
else
return 3
fi
}
# Function to ask for JDK installation confirmation
ask_jdk_confirmation() {
local current_version="$1"
local required_version="$2"
local arch="$3"
echo ""
echo "JDK Version Mismatch Detected!"
echo " Current version: $current_version"
echo " Current installation path: $(which java 2>/dev/null || echo 'Not found')"
if command -v java &> /dev/null && [[ -n "$JAVA_HOME" ]]; then
echo " Current JAVA_HOME: $JAVA_HOME"
fi
echo " Required version for $arch: $required_version"
echo " This script will install $required_version alongside your existing installation."
echo " Your current Java installation will not be removed."
echo ""
while true; do
read -p "Do you want to install $required_version? (y/N): " yn
case $yn in
[Yy]* ) return 0;;
[Nn]* | "" ) echo "JDK installation cancelled. Exiting."; exit 0;;
* ) echo "Please answer yes (y) or no (n).";;
esac
done
}
# First, check and install Git (needed for cloning repository)
echo ">>> Checking Git installation..."
if ! command -v git &> /dev/null; then
echo " Git is not installed."
while true; do
read -p "Do you want to install Git (required for cloning the java-tron repository)? (y/N): " yn
case $yn in
[Yy]* )
echo ">>> Installing Git..."
INSTALL_GIT=true
break;;
[Nn]* | "" )
echo "Git installation cancelled. You'll need Git to clone the java-tron repository."
echo "You can install Git manually later and then clone the repository."
INSTALL_GIT=false
break;;
* ) echo "Please answer yes (y) or no (n).";;
esac
done
else
echo "Git is already installed: $(git --version)"
INSTALL_GIT=false
fi
echo ""
echo ">>> Checking existing Java installation..."
set +e # Temporarily disable exit on error
check_java_version
java_status=$?
set -e # Re-enable exit on error
# Determine required JDK version based on architecture
if [[ "$OS" == "Darwin" ]]; then
if [[ "$ARCH" == "x86_64" ]]; then
required_jdk="JDK 8"
required_status=0
elif [[ "$ARCH" == "arm64" ]]; then
required_jdk="JDK 17"
required_status=1
fi
elif [[ "$OS" == "Linux" ]]; then
if [[ "$ARCH" == "x86_64" ]]; then
required_jdk="JDK 8"
required_status=0
elif [[ "$ARCH" == "aarch64" ]] || [[ "$ARCH" == "arm64" ]]; then
required_jdk="JDK 17"
required_status=1
fi
fi
# Check if correct JDK version is already installed
if [[ $java_status -eq $required_status ]]; then
echo " You can skip the Java installation part."
echo ""
if [[ "$INSTALL_GIT" == "false" ]]; then
echo "Both Git and Java JDK are ready for TRON development!"
echo ""
exit 0
else
echo ">>> Proceeding with Git installation only..."
SKIP_JAVA_INSTALL=true
fi
elif [[ $java_status -eq 0 ]] || [[ $java_status -eq 1 ]] || [[ $java_status -eq 2 ]]; then
# Different JDK version is installed, ask for confirmation
current_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2)
ask_jdk_confirmation "$current_version" "$required_jdk" "$ARCH"
SKIP_JAVA_INSTALL=false
else
# No Java installation found, ask for general confirmation
echo ""
echo "No Java installation detected!"
echo " This script will install $required_jdk which is required for $ARCH architecture."
echo ""
ask_confirmation
SKIP_JAVA_INSTALL=false
fi
# Function to show permanent Java configuration instructions
show_permanent_java_config() {
local jdk_version="$1"
local os_type="$2"
local java_home="$3"
local java_bin_path="$4"
echo ""
echo " To make JDK $jdk_version permanent:"
if [[ "$os_type" == "Darwin" ]]; then
echo " # Add to ~/.zshrc or ~/.bash_profile:"
echo " echo 'export JAVA_HOME=\"$java_home\"' >> ~/.zshrc"
echo " echo 'export PATH=\"\$JAVA_HOME/bin:\$PATH\"' >> ~/.zshrc"
echo " # Then run below command:"
echo " source ~/.zshrc"
echo ""
echo " # Or use jenv for Java version management:"
echo " brew install jenv"
echo " jenv add $java_home"
elif [[ "$os_type" == "Linux" ]]; then
echo " # Method 1: Add to ~/.bashrc:"
echo " echo 'export JAVA_HOME=\"$java_home\"' >> ~/.bashrc"
echo " echo 'export PATH=\"\$JAVA_HOME/bin:\$PATH\"' >> ~/.bashrc"
echo " source ~/.bashrc"
echo ""
echo " # Method 2: Use update-alternatives (recommended):"
if [[ "$PKG_MANAGER" == "apt-get" ]]; then
echo " sudo update-alternatives --install /usr/bin/java java $java_bin_path/java 1"
echo " sudo update-alternatives --install /usr/bin/javac javac $java_bin_path/javac 1"
echo " sudo update-alternatives --config java"
else
echo " sudo alternatives --install /usr/bin/java java $java_bin_path/java 1"
echo " sudo alternatives --install /usr/bin/javac javac $java_bin_path/javac 1"
echo " sudo alternatives --config java"
fi
fi
echo ""
}
# Function to show Java environment application instructions
show_java_env_instructions() {
local java_home="$1"
local java_bin_path="$2"
echo ""
echo " ✓ Java environment has been applied to this script session."
echo " To apply Java environment to your current terminal session:"
echo " source ./tron_java_env.sh"
echo ""
echo " Or run these commands directly:"
echo " export JAVA_HOME=\"$java_home\""
echo " export PATH=\"$java_bin_path:\$PATH\""
echo ""
echo " Note: You may need to open a new terminal or run 'source ./tron_java_env.sh'"
echo " if 'java -version' doesn't work immediately after this script completes."
}
# Function to get Java paths based on OS and architecture
get_java_paths() {
local jdk_version="$1"
local os_type="$2"
local arch="$3"
local java_home=""
if [[ "$os_type" == "Darwin" ]]; then
# macOS paths - try to detect actual Homebrew installation
if [[ "$jdk_version" == "8" ]]; then
# Try multiple possible paths for JDK 8
for path in "/usr/local/opt/openjdk@8" "/opt/homebrew/opt/openjdk@8"; do
if [[ -d "$path" ]]; then
java_home="$path"
break
fi
done
# If not found, use brew --prefix to get the correct path
if [[ -z "$java_home" ]] && command -v brew &> /dev/null; then
local brew_prefix=$(brew --prefix openjdk@8 2>/dev/null || echo "")
if [[ -n "$brew_prefix" && -d "$brew_prefix" ]]; then
java_home="$brew_prefix"
fi
fi
elif [[ "$jdk_version" == "17" ]]; then
# Try multiple possible paths for JDK 17
for path in "/opt/homebrew/opt/openjdk@17" "/usr/local/opt/openjdk@17"; do
if [[ -d "$path" ]]; then
java_home="$path"
break
fi
done
# If not found, use brew --prefix to get the correct path
if [[ -z "$java_home" ]] && command -v brew &> /dev/null; then
local brew_prefix=$(brew --prefix openjdk@17 2>/dev/null || echo "")
if [[ -n "$brew_prefix" && -d "$brew_prefix" ]]; then
java_home="$brew_prefix"
fi
fi
fi
elif [[ "$os_type" == "Linux" ]]; then
# Linux paths - provide generic path for manual configuration
java_home="/usr/lib/jvm/java-$jdk_version-openjdk"
fi
echo "$java_home"
}
# Unified Java environment configuration function
configure_java_environment() {
local jdk_version="$1"
local os_type="$2"
local arch="$3"
local java_home=""
local java_bin_path=""
echo ""
echo ">>> Configuring Java environment for JDK $jdk_version..."
# Ask user for confirmation before changing environment
echo ""
echo "This will modify your Java environment settings:"
echo " - Set JAVA_HOME to the new JDK $jdk_version installation"
echo " - Update PATH to include the new Java binaries"
echo " - Create a script (tron_java_env.sh) for easy environment setup"
echo ""
while true; do
read -p "Do you want to configure the Java environment for JDK $jdk_version? (y/N): " yn
case $yn in
[Yy]* )
echo ">>> Proceeding with Java environment configuration..."
break;;
[Nn]* | "" )
echo "Java environment configuration skipped."
echo "You may need to manually set JAVA_HOME and PATH for JDK $jdk_version"
echo ""
echo "Manual configuration commands:"
# Get the expected Java path
local expected_java_home=$(get_java_paths "$jdk_version" "$os_type" "$arch")
local expected_java_bin_path="$expected_java_home/bin"
echo " export JAVA_HOME=\"$expected_java_home\""
echo " export PATH=\"\$JAVA_HOME/bin:\$PATH\""
if [[ "$os_type" == "Linux" ]]; then
echo ""
echo "Note: Actual path may vary depending on your distribution."
echo "Common paths include:"
echo " /usr/lib/jvm/java-$jdk_version-openjdk-amd64 (Ubuntu/Debian)"
echo " /usr/lib/jvm/java-1.$jdk_version.0-openjdk (RHEL/CentOS)"
fi
# Create tron_java_env.sh even when user skips configuration
echo ""
echo ">>> Creating tron_java_env.sh for manual use later..."
local env_script="./tron_java_env.sh"
cat > "$env_script" << EOF
#!/bin/bash
# TRON Java Environment Configuration
# Generated by install_dependencies.sh on $(date)
export JAVA_HOME="$expected_java_home"
export PATH="$expected_java_bin_path:\$PATH"
echo "Java environment configured:"
echo " JAVA_HOME: \$JAVA_HOME"
echo " Java version: \$(java -version 2>&1 | head -n 1)"
EOF
chmod +x "$env_script"
echo " ✓ Created $env_script"
# Show the same application instructions as automatic configuration
show_java_env_instructions "$expected_java_home" "$expected_java_bin_path"
# Show permanent configuration instructions
show_permanent_java_config "$jdk_version" "$os_type" "$expected_java_home" "$expected_java_bin_path"
echo ""
return 1;;
* ) echo "Please answer yes (y) or no (n).";;
esac
done
# Determine Java paths based on OS and architecture
if [[ "$os_type" == "Darwin" ]]; then
# Use the helper function for macOS
java_home=$(get_java_paths "$jdk_version" "$os_type" "$arch")
java_bin_path="$java_home/bin"
# Debug output for macOS
echo " Detected Java path: $java_home"
if [[ ! -d "$java_home" ]]; then
echo " Warning: Java home directory not found at: $java_home"
echo " Attempting to find JDK installation..."
# Try to find the installation using brew
if command -v brew &> /dev/null; then
local brew_list=$(brew list --formula | grep "openjdk@$jdk_version" || echo "")
if [[ -n "$brew_list" ]]; then
echo " Found Homebrew package: $brew_list"
local brew_prefix=$(brew --prefix openjdk@$jdk_version 2>/dev/null || echo "")
if [[ -n "$brew_prefix" && -d "$brew_prefix" ]]; then
java_home="$brew_prefix"
java_bin_path="$java_home/bin"
echo " Updated Java path to: $java_home"
fi
else
echo " Error: openjdk@$jdk_version not found in Homebrew packages"
echo " Try running: brew list | grep openjdk"
return 1
fi
fi
fi
elif [[ "$os_type" == "Linux" ]]; then
# Linux paths - try to find the actual installation
if [[ "$jdk_version" == "8" ]]; then
if [[ "$PKG_MANAGER" == "apt-get" ]]; then
if [[ "$arch" == "aarch64" ]] || [[ "$arch" == "arm64" ]]; then
java_home="/usr/lib/jvm/java-8-openjdk-arm64"
else
java_home="/usr/lib/jvm/java-8-openjdk-amd64"
fi
else
# RHEL/CentOS/Amazon Linux - try multiple possible paths
for path in "/usr/lib/jvm/java-1.8.0-amazon-corretto" "/usr/lib/jvm/java-1.8.0-openjdk"; do
if [[ -d "$path" ]]; then
java_home="$path"
break
fi
done
fi
elif [[ "$jdk_version" == "17" ]]; then
if [[ "$PKG_MANAGER" == "apt-get" ]]; then
if [[ "$arch" == "aarch64" ]] || [[ "$arch" == "arm64" ]]; then
java_home="/usr/lib/jvm/java-17-openjdk-arm64"
else
java_home="/usr/lib/jvm/java-17-openjdk-amd64"
fi
else
# RHEL/CentOS/Amazon Linux - try multiple possible paths
for path in "/usr/lib/jvm/java-17-amazon-corretto" "/usr/lib/jvm/java-17-openjdk"; do
if [[ -d "$path" ]]; then
java_home="$path"
break
fi
done
fi
fi
java_bin_path="$java_home/bin"
fi
# Set environment variables for current session
if [[ -d "$java_home" ]]; then
export JAVA_HOME="$java_home"
export PATH="$java_bin_path:$PATH"
echo " JAVA_HOME set to: $JAVA_HOME"
echo " PATH updated to include: $java_bin_path"
echo " Environment temporarily configured for JDK $jdk_version"
# Create a source script for the user's current shell
local env_script="./tron_java_env.sh"
cat > "$env_script" << EOF
#!/bin/bash
# TRON Java Environment Configuration
# Generated by install_dependencies.sh on $(date)
export JAVA_HOME="$java_home"
export PATH="$java_bin_path:\$PATH"
echo "Java environment configured:"
echo " JAVA_HOME: \$JAVA_HOME"
echo " Java version: \$(java -version 2>&1 | head -n 1)"
EOF
chmod +x "$env_script"
echo ""
echo " ✓ Created $env_script"
echo ""
echo " Applying Java environment to current shell session..."
# Source the environment script to apply it immediately
source "$env_script"
show_java_env_instructions "$java_home" "$java_bin_path"
else
echo " Could not find Java installation at expected path: $java_home"
echo " You may need to set JAVA_HOME manually"
return 1
fi
# Provide OS-specific permanent configuration instructions
show_permanent_java_config "$jdk_version" "$os_type" "$java_home" "$java_bin_path"
return 0
}
echo "----------------------------------------"
install_macos() {
if ! command -v brew &> /dev/null; then
echo ">>> Homebrew not found."
echo " Homebrew is required to install Java on macOS."
echo ""
while true; do
read -p "Do you want to install Homebrew? (y/N): " yn
case $yn in
[Yy]* )
echo ">>> Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
break;;
[Nn]* | "" )
echo "Homebrew installation cancelled."
echo "Cannot proceed with Java installation without Homebrew on macOS."
echo "Please install Homebrew manually or use alternative Java installation methods."
exit 1;;
* ) echo "Please answer yes (y) or no (n).";;
esac
done
# Add Homebrew to PATH for the current session (Apple Silicon vs Intel)
if [[ "$ARCH" == "arm64" ]]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
else
eval "$(/usr/local/bin/brew shellenv)"
fi
else
echo ">>> Homebrew is already installed."
fi
echo ">>> Updating Homebrew..."
brew update
# Install Git if needed
if [[ "$INSTALL_GIT" == "true" ]]; then
echo ">>> Installing Git..."
brew install git
echo " Git installed successfully: $(git --version)"
fi
# Skip Java installation if flag is set
if [[ "$SKIP_JAVA_INSTALL" == "true" ]]; then
echo ">>> Skipping Java installation (correct version already detected)."
return 0
fi
if [[ "$ARCH" == "x86_64" ]]; then
echo ">>> Architecture is x86_64. Checking for JDK 8..."
set +e # Temporarily disable exit on error
check_java_version
local java_status=$?
set -e # Re-enable exit on error
if [[ $java_status -eq 0 ]]; then
echo ">>> JDK 8 is already installed. Skipping installation."
else
if [[ $java_status -eq 1 ]]; then
echo ">>> Installing JDK 8 alongside existing JDK 17..."
elif [[ $java_status -eq 2 ]]; then
echo ">>> Installing JDK 8 alongside existing Java installation..."
else
echo ">>> Installing JDK 8..."
fi
if brew install openjdk@8; then
echo ">>> JDK 8 installation completed successfully."
# Use unified Java environment configuration
if configure_java_environment "8" "Darwin" "$ARCH"; then
echo "Environment has been updated! Java 8 is now configured."
else
echo "Java 8 installed but environment not configured. You may need to set JAVA_HOME manually."
fi
else
echo "Error: Failed to install JDK 8 via Homebrew."
echo "Please try installing manually with: brew install openjdk@8"
exit 1
fi
fi
elif [[ "$ARCH" == "arm64" ]]; then
echo ">>> Architecture is arm64. Checking for JDK 17..."
set +e # Temporarily disable exit on error
check_java_version
local java_status=$?
set -e # Re-enable exit on error
if [[ $java_status -eq 1 ]]; then
echo ">>> JDK 17 is already installed. Skipping installation."
else
if [[ $java_status -eq 0 ]]; then
echo ">>> Installing JDK 17 alongside existing JDK 8..."
elif [[ $java_status -eq 2 ]]; then
echo ">>> Installing JDK 17 alongside existing Java installation..."
else
echo ">>> Installing JDK 17..."
fi
if brew install openjdk@17; then
echo ">>> JDK 17 installation completed successfully."
# Use unified Java environment configuration
if configure_java_environment "17" "Darwin" "$ARCH"; then
echo "Environment has been updated! Java 17 is now configured."
else
echo "Java 17 installed but environment not configured. You may need to set JAVA_HOME manually."
fi
else
echo "Error: Failed to install JDK 17 via Homebrew."
echo "Please try installing manually with: brew install openjdk@17"
exit 1
fi
fi
else
echo "Error: Unsupported architecture for macOS script: $ARCH"
exit 1
fi
}
install_linux() {
if command -v dnf &> /dev/null; then
PKG_MANAGER="dnf"
INSTALL_CMD="sudo dnf install -y"
UPDATE_CMD="sudo dnf check-update"
elif command -v yum &> /dev/null; then
PKG_MANAGER="yum"
INSTALL_CMD="sudo yum install -y"
UPDATE_CMD="sudo yum check-update"
elif command -v apt-get &> /dev/null; then
PKG_MANAGER="apt-get"
INSTALL_CMD="sudo apt-get install -y"
UPDATE_CMD="sudo apt-get update"
else
echo "Error: Unsupported package manager. Only apt-get (Debian/Ubuntu) and yum/dnf (RHEL/CentOS/Amazon Linux) are currently supported."
exit 1
fi
echo ">>> Updating package index ($PKG_MANAGER)..."
$UPDATE_CMD || true
# Install Git if needed
if [[ "$INSTALL_GIT" == "true" ]]; then
echo ">>> Installing Git..."
$INSTALL_CMD git
echo " Git installed successfully: $(git --version)"
fi
# Skip Java installation if flag is set
if [[ "$SKIP_JAVA_INSTALL" == "true" ]]; then
echo ">>> Skipping Java installation (correct version already detected)."
return 0
fi
install_first_available() {
local target_version="$1"
shift
local installed_package=""
for pkg in "$@"; do
echo " Attempting to install: $pkg"
if $INSTALL_CMD "$pkg"; then
installed_package="$pkg"
echo " Successfully installed: $pkg"
break
else
echo " Failed to install: $pkg"
fi
done
if [[ -n "$installed_package" ]]; then
# Verify what version was actually installed
echo " Verifying installed Java version..."
if command -v java &> /dev/null; then
local actual_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2)
echo " Installed Java version: $actual_version"
# Check if the installed version matches what we expected
if [[ "$target_version" == "8" ]]; then
if [[ "$actual_version" =~ ^1\.8\. ]]; then
echo " ✓ JDK 8 installed successfully"
return 0
else
echo " ✗ Expected JDK 8 but got: $actual_version"
echo " This may happen if JDK 8 is not available in your distribution"
return 2
fi
elif [[ "$target_version" == "17" ]]; then
if [[ "$actual_version" =~ ^17\. ]]; then
echo " ✓ JDK 17 installed successfully"
return 0
else
echo " ✗ Expected JDK 17 but got: $actual_version"
return 2
fi
fi
else
echo " ✗ Java command not found after installation"
return 1
fi
else
return 1
fi
}
if [[ "$ARCH" == "x86_64" ]]; then
echo ">>> Architecture is x86_64. Checking for JDK 8..."
set +e # Temporarily disable exit on error
check_java_version
local java_status=$?
set -e # Re-enable exit on error
if [[ $java_status -eq 0 ]]; then
echo ">>> JDK 8 is already installed. Skipping installation."
else
if [[ $java_status -eq 1 ]]; then
echo ">>> Installing JDK 8 alongside existing JDK 17..."
elif [[ $java_status -eq 2 ]]; then
echo ">>> Installing JDK 8 alongside existing Java installation..."
else
echo ">>> Installing JDK 8..."
fi
if [[ "$PKG_MANAGER" == "apt-get" ]]; then
if install_first_available "8" openjdk-8-jdk; then
install_result=0
else
install_result=$?
fi
else
if install_first_available "8" java-1.8.0-amazon-corretto-devel java-1.8.0-openjdk-devel; then
install_result=0
else
install_result=$?
fi
fi
if [[ $install_result -eq 0 ]]; then
# Use unified Java environment configuration
if configure_java_environment "8" "Linux" "$ARCH"; then
echo "Environment has been updated! Java 8 is now configured."
else
echo "Java 8 installed but environment not configured. You may need to set JAVA_HOME manually."
fi
elif [[ $install_result -eq 2 ]]; then
# JDK 8 package is installed but default version is different
# Need to switch to JDK 8 using update-alternatives
local actual_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2)
echo ">>> JDK 8 package is installed, but system default is: $actual_version"
echo ">>> Switching system default to JDK 8 using update-alternatives..."
# Try to switch to JDK 8
if [[ "$PKG_MANAGER" == "apt-get" ]]; then
local jdk8_path="/usr/lib/jvm/java-8-openjdk-amd64"
if [[ -d "$jdk8_path" ]]; then
echo " Found JDK 8 at: $jdk8_path"
# Set JDK 8 as default using update-alternatives
sudo update-alternatives --set java "$jdk8_path/jre/bin/java" 2>/dev/null || \
sudo update-alternatives --set java "$jdk8_path/bin/java" 2>/dev/null || \
echo " Note: Could not auto-switch. Please run: sudo update-alternatives --config java"
sudo update-alternatives --set javac "$jdk8_path/bin/javac" 2>/dev/null || \
echo " Note: Could not auto-switch javac. Please run: sudo update-alternatives --config javac"
# Verify the switch
local new_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2)
if [[ "$new_version" =~ ^1\.8\. ]]; then
echo " ✓ Successfully switched to JDK 8: $new_version"
# Now configure environment for JDK 8
if configure_java_environment "8" "Linux" "$ARCH"; then
echo "Environment has been updated! Java 8 is now configured."
else
echo "Java 8 is active but environment not configured. You may need to set JAVA_HOME manually."
fi
else
echo " ✗ Auto-switch failed. Current version: $new_version"
echo " Please manually switch to JDK 8:"
echo " sudo update-alternatives --config java"
echo " sudo update-alternatives --config javac"
echo " Then configure environment for JDK 8"
fi
else
echo " ✗ JDK 8 directory not found at expected location: $jdk8_path"
echo " Please manually locate and configure JDK 8"
fi
else
# For yum/dnf systems
echo " Please manually switch to JDK 8:"
echo " sudo alternatives --config java"
echo " sudo alternatives --config javac"
echo ""
echo " After switching, you can configure the environment."
# Still create tron_java_env.sh for manual use
if configure_java_environment "8" "Linux" "$ARCH"; then
echo "Environment configuration completed for JDK 8."
else
echo "tron_java_env.sh has been created for manual use."
echo "After switching to JDK 8, run: source ./tron_java_env.sh"
fi
fi
else
echo "Error: Unable to install any JDK on $PKG_MANAGER"
exit 1
fi
fi
elif [[ "$ARCH" == "aarch64" ]] || [[ "$ARCH" == "arm64" ]]; then
echo ">>> Architecture is arm64/aarch64. Checking for JDK 17..."
set +e # Temporarily disable exit on error
check_java_version
local java_status=$?
set -e # Re-enable exit on error
if [[ $java_status -eq 1 ]]; then
echo ">>> JDK 17 is already installed. Skipping installation."
else
if [[ $java_status -eq 0 ]]; then
echo ">>> Installing JDK 17 alongside existing JDK 8..."
elif [[ $java_status -eq 2 ]]; then
echo ">>> Installing JDK 17 alongside existing Java installation..."
else
echo ">>> Installing JDK 17..."
fi
if [[ "$PKG_MANAGER" == "apt-get" ]]; then
if install_first_available "17" openjdk-17-jdk; then
install_result=0
else
install_result=$?
fi
else
if install_first_available "17" java-17-amazon-corretto-devel java-17-openjdk-devel; then
install_result=0
else
install_result=$?
fi
fi
if [[ $install_result -eq 0 ]]; then
# Use unified Java environment configuration
if configure_java_environment "17" "Linux" "$ARCH"; then
echo "Environment has been updated! Java 17 is now configured."
else
echo "Java 17 installed but environment not configured. You may need to set JAVA_HOME manually."
fi
elif [[ $install_result -eq 2 ]]; then
# JDK 17 package is installed but default version is different
# Need to switch to JDK 17 using update-alternatives
local actual_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2)
echo ">>> JDK 17 package is installed, but system default is: $actual_version"
echo ">>> Switching system default to JDK 17 using update-alternatives..."
# Try to switch to JDK 17
if [[ "$PKG_MANAGER" == "apt-get" ]]; then
local jdk17_path=""
if [[ "$ARCH" == "aarch64" ]] || [[ "$ARCH" == "arm64" ]]; then
jdk17_path="/usr/lib/jvm/java-17-openjdk-arm64"
else
jdk17_path="/usr/lib/jvm/java-17-openjdk-amd64"
fi
if [[ -d "$jdk17_path" ]]; then
echo " Found JDK 17 at: $jdk17_path"
# Set JDK 17 as default using update-alternatives
sudo update-alternatives --set java "$jdk17_path/bin/java" 2>/dev/null || \
echo " Note: Could not auto-switch. Please run: sudo update-alternatives --config java"
sudo update-alternatives --set javac "$jdk17_path/bin/javac" 2>/dev/null || \
echo " Note: Could not auto-switch javac. Please run: sudo update-alternatives --config javac"
# Verify the switch
local new_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2)
if [[ "$new_version" =~ ^17\. ]]; then
echo " ✓ Successfully switched to JDK 17: $new_version"
# Now configure environment for JDK 17
if configure_java_environment "17" "Linux" "$ARCH"; then
echo "Environment has been updated! Java 17 is now configured."
else
echo "Java 17 is active but environment not configured. You may need to set JAVA_HOME manually."
fi
else
echo " ✗ Auto-switch failed. Current version: $new_version"
echo " Please manually switch to JDK 17:"
echo " sudo update-alternatives --config java"
echo " sudo update-alternatives --config javac"
echo " Then configure environment for JDK 17"
fi
else
echo " ✗ JDK 17 directory not found at expected location: $jdk17_path"
echo " Please manually locate and configure JDK 17"
fi
else
# For yum/dnf systems
echo " Please manually switch to JDK 17:"
echo " sudo alternatives --config java"
echo " sudo alternatives --config javac"
echo ""
echo " After switching, you can configure the environment."
# Still create tron_java_env.sh for manual use
if configure_java_environment "17" "Linux" "$ARCH"; then
echo "Environment configuration completed for JDK 17."
else
echo "tron_java_env.sh has been created for manual use."
echo "After switching to JDK 17, run: source ./tron_java_env.sh"
fi
fi
else
echo "Error: Unable to install any JDK on $PKG_MANAGER"
exit 1
fi
fi
else
echo "Error: Unsupported architecture for Linux script: $ARCH"
exit 1
fi
}
if [[ "$OS" == "Darwin" ]]; then
install_macos
elif [[ "$OS" == "Linux" ]]; then
install_linux
else
echo "Error: Unsupported Operating System: $OS"
exit 1
fi
echo "----------------------------------------"
echo "Installation completed successfully!"
echo ""
echo ">>> Verification Commands:"
echo " git --version"
echo " java -version"
echo ""
# Verify that Java is actually working
echo ">>> Verifying Java installation..."
if command -v java &> /dev/null; then
echo " Java command found: $(which java)"
if java -version &> /dev/null; then
echo " Java version: $(java -version 2>&1 | head -n 1)"
else
echo " ✗ Java command exists but cannot run properly"
echo " Please run: source ./tron_java_env.sh"
fi
else
echo " ✗ Java command not found in PATH"
echo " Please run: source ./tron_java_env.sh"
echo " If that doesn't work, check the Java environment configuration above."
fi
echo ""
echo ">>> Troubleshooting:"
echo " - If 'java -version' shows incorrect version, run: source ./tron_java_env.sh"
echo " - For permanent configuration, follow the instructions shown above."
echo ""
echo ""