-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
59 lines (49 loc) · 1.1 KB
/
main.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
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
flag "github.com/spf13/pflag"
)
const cmdStr = `#!/bin/sh
exec java %s -jar "$0" "$@"
exit $?
`
func main() {
var (
exeFileName string
javaOptions string
)
flag.StringVar(&exeFileName, "name", "", "exe file name")
flag.StringVar(&javaOptions, "jop", "", "java options")
flag.Parse()
if len(flag.Args()) == 0 {
log.Fatal("Not enough arguments. ex)jarexe standalone.jar")
}
jarFileName := flag.Args()[0]
jarFile, err := os.Open(jarFileName)
if err != nil {
log.Fatalf("error: %s", err.Error())
}
defer jarFile.Close()
if exeFileName == "" {
exeFileName = filepath.Base(jarFileName)
if ext := filepath.Ext(exeFileName); ext == ".jar" {
exeFileName = exeFileName[:len(exeFileName)-4]
}
}
shStr := fmt.Sprintf(cmdStr, javaOptions)
reader := io.MultiReader(strings.NewReader(shStr), jarFile)
b, err := ioutil.ReadAll(reader)
if err != nil {
log.Fatalf("error: %s", err.Error())
}
err = ioutil.WriteFile(exeFileName, b, os.ModePerm)
if err != nil {
log.Fatalf("error: %s", err.Error())
}
}