java [-options] class [args]
java [-options] -jar jarfile [args]javaw [-options] class [args]
javaw [-options] -jar jarfile [args]
java命令传递三组参数:选项、主类名(或者JAR文件名)和main()方法参数。选项由减号(–)开头。javaw命令和java命令几乎一样,唯一的差别在于,javaw命令不显示命令行窗口
package main
import (
"flag"
"fmt"
"os"
)
type Cmd struct {
helpFlag bool
versionFlag bool
cpOption string
class string
args []string
}
func parseCmd() *Cmd {
cmd := &Cmd{}
flag.Usage = printUsage
// flag.TypeVar(Type指针, flag名, 默认值, 帮助信息)
flag.BoolVar(&cmd.helpFlag,"help",false,"print help message")
flag.BoolVar(&cmd.helpFlag,"?",false,"print help message")
flag.BoolVar(&cmd.versionFlag,"version",false,"print version message")
flag.StringVar(&cmd.cpOption,"classpath","","classpath")
flag.StringVar(&cmd.cpOption,"cp","","classpath")
flag.Parse()
args := flag.Args()
if len(args) > 0 {
cmd.class = args[0]
cmd.args = args[1:]
}
return cmd
}
func printUsage() {
fmt.Printf("Usage: %s [-options] class [args...]\n", os.Args[0])
}
package main
import "fmt"
func main() {
cmd := parseCmd()
if cmd.versionFlag{
fmt.Println("version 0.0.1")
}else if cmd.helpFlag || cmd.class == "" {
printUsage()
}else{
startJVM(cmd)
}
}
func startJVM(cmd *Cmd) {
fmt.Printf("classpath:%s class:%s args:%v\n",cmd.cpOption, cmd.class, cmd.args)
}
通过 go install编译,在设置go的工作环境时,通过setx GOPATH E:\go\workplace设置会生效