From 57cec3053ae9146de70eca40fbbed509ba07ba4d Mon Sep 17 00:00:00 2001 From: Pai2Chen Date: Fri, 28 Jan 2022 14:26:18 +0800 Subject: [PATCH] support both environment method and arg method --- main.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 6e41165..ac2623b 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,8 @@ func main() { args := os.Args proPath, _ := filepath.Abs(args[0]) basePath := filepath.Dir(proPath) + isEnv := os.Getenv("CLASSPATH") != "" + classPathIndex := 0 nClassPath := make([]string, 0) filepath.Walk(basePath+"/lwjgl/", func(path string, info fs.FileInfo, err error) error { @@ -21,16 +23,31 @@ func main() { return err }) - classPath := strings.Split(os.Getenv("CLASSPATH"), ":") + classPathStr := "" - for _, v := range classPath { + if isEnv { + classPathStr = os.Getenv("CLASSPATH") + } else { + for i, arg := range args { + if arg == "-cp" { + classPathIndex = i + 1 + classPathStr = args[i+1] + break + } + } + } + + for _, v := range strings.Split(classPathStr, ":") { if !strings.Contains(v, "lwjgl") { nClassPath = append(nClassPath, v) } } + args[classPathIndex] = strings.Join(nClassPath, ":") cmd := exec.Command(args[1], args[2:]...) - cmd.Env = append(os.Environ(), "CLASSPATH="+strings.Join(nClassPath, ":")) + if isEnv { + cmd.Env = append(os.Environ(), "CLASSPATH=") + } cmd.Stdout = os.Stdout //println(strings.Join(nClassPath, ":"))