This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
/
javahelper.go
141 lines (117 loc) · 3.08 KB
/
javahelper.go
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
package util
/*
* Minecraft Forge
* Copyright (c) 2016.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
import (
"os"
"os/exec"
"strings"
"github.com/fatih/color"
"github.com/kardianos/osext"
)
func IsJavaInstalled() bool {
jv, err := exec.LookPath("java")
if err != nil {
color.Red("Java has not been found!")
return false
} else {
color.Green("Java has been found at: %s", jv)
return true
}
}
/*
* Launches with system jre
* target: the target jar
* args: the jar args
*
*/
func GenericSysJavaLauncher(target string) ([]byte, error) {
// color.Yellow("Forge wrapper is now launching %s", target)
color.Yellow("Now running from System JRE")
out, err := exec.Command("java", "-jar", target).CombinedOutput()
if err != nil {
println(err)
// panic(err)
}
return out, err
}
/*
* Launches the mojang jre
* target: the target jar
* args: the jar args
*
*/
func GenericMojangJavaLauncher(target string) ([]byte, error) {
checkForRuntime()
darwinJRE := getRuntimeJREDir() + "/bin/java"
winJRE := getRuntimeJREDir() + "/bin/java.exe"
color.Yellow("Now running from Mojang JRE")
switch GetThisPlatform() {
case "windows":
out, err := exec.Command(winJRE, "-jar", target).CombinedOutput()
return out, err
case "darwin":
out, err := exec.Command(darwinJRE, "-jar", target).CombinedOutput()
return out, err
case "linux":
color.Red("Sorry Mojang has not built a JRE for linux please download from go to " +
"http://openjdk.java.net/install/ or " +
"http://www.oracle.com/technetwork/java/javase/downloads/index.html to download the latest java 8.")
os.Exit(3)
}
return nil, nil
}
/*
* Check for java 8
*/
func IsJavaVersionValid() bool {
out, _ := exec.Command("java", "-version").CombinedOutput()
if strings.Contains(string(out), "1.8") {
return true
} else {
return false
}
}
func Wrapper(jar string) ([]byte, error) {
if IsJavaVersionValid() {
out, err := GenericSysJavaLauncher(jar)
return out, err
} else {
out, err := GenericMojangJavaLauncher(jar)
return out, err
}
}
/*
* Launches either the wrapper or an appended jar
*/
func ModedLauncher() {
binName, _ := osext.Executable()
CheckForLauncher()
_, err := Wrapper(binName)
if err != nil {
color.Yellow("Now launching in launcher mode")
CheckForLauncher()
nout, nerr := Wrapper(getMcDir() + "/launcher.jar")
if nerr != nil {
print(nerr)
}
print(string(nout))
} else {
color.Yellow("Launching in Installer mode")
}
}