OpenJDK自動安裝腳本 InstallOpenJDK.vbs

Oracle JDK 要收費了,Open JDK沒有安裝包,只有Zip,寫了個安裝腳本 InstallOpenJDK.vbsjava

 

 

 1 Rem *********************************
 2 Rem **File: InstallOpenJDK.vbs 
 3 Rem **Author: windfic
 4 Rem **Date: 2019-06-05
 5 Rem **Description: 自動安裝OpenJDK
 6 Rem *********************************
 7 
 8 
 9 Rem ==把指定目錄加入註冊表==
10 sub PathToRegPath(Path, Reg) 
11     Set oWshShell = WScript.CreateObject("WScript.Shell")
12     
13     OldPath = oWshShell.RegRead(Reg)
14     
15     if InStr(OldPath, Path)  <= 0 then
16         NewPath = OldPath & ";" & Path
17         oWshShell.RegWrite Reg, NewPath
18     end if
19 end sub
20 
21 Rem ==把指定目錄加入系統Path==
22 sub PathToSystemPath(Path) 
23     PathToRegPath Path, "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment\Path"
24 end sub
25 
26 Rem ==安裝JDK==
27 sub InstallJDK(Path) 
28     Set oWshShell = WScript.CreateObject("WScript.Shell")
29     Set oFs=WScript.CreateObject("Scripting.FileSystemObject")
30     
31     if oFs.FolderExists(Path) then
32         Home = oFs.GetAbsolutePathName(Path)
33     else
34         Set oFolder = oFs.GetFile(Wscript.ScriptFullName).ParentFolder
35         Home = oFolder.Path & "\" & Path
36     end if
37     
38     if oFs.FolderExists(Home) then
39         Rem 設置JAVA_HOME
40         oWshShell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment\JAVA_HOME", Home
41             
42         Rem 把bin加入系統目錄
43         PathToSystemPath "%JAVA_HOME%\bin"
44     else
45         MsgBox("目錄:" & Path & " 不存在。")
46     end if
47     
48 end sub 
49 
50 Rem ==安裝JRE==
51 sub InstallJRE(Path) 
52     Set oWshShell = WScript.CreateObject("WScript.Shell")
53     Set oFs=WScript.CreateObject("Scripting.FileSystemObject")
54     
55     if oFs.FolderExists(Path) then
56         Home = oFs.GetAbsolutePathName(Path)
57     else
58         Set oFolder = oFs.GetFile(Wscript.ScriptFullName).ParentFolder
59         Home = oFolder.Path & "\" & Path
60     end if
61     
62     if oFs.FolderExists(Home) then
63         Rem 生成jre目錄
64         oWshShell.CurrentDirectory = Home
65         CommandLine = "bin\jlink.exe --module-path jmods --add-modules java.desktop --output jre"
66         oWshShell.Exec(CommandLine)
67         
68         Rem 設置JRE_HOEM
69         Home = Home & "\jre"
70         oWshShell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment\JRE_HOEM", Home
71         
72         Rem 設置CLASSPATH
73         CLASSPATH = ".;%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib"
74         oWshShell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment\CLASSPATH", CLASSPATH
75         
76         Rem 把jre\bin加入系統目錄
77         PathToSystemPath "%JAVA_HOME%\jre\bin"
78     end if
79     
80 end sub 
81 
82 
83 Rem ========================主程序================================
84 
85 JavaHome = "D:\java\jdk-12"
86 
87 InstallJDK JavaHome
88 InstallJRE JavaHome
89 
90 MsgBox("執行完成。")

 注:環境變量設置方法來自網絡網絡

(完)spa

相關文章
相關標籤/搜索