How to start a process?


I try to start a JVM out of a Google Go program that looks like this:

package main
import "fmt"
import "os"
import "log"
func main() {

var name string

var path="-Xbootclasspath:jrex64\\lib\\rt.jar;"+

var args[] string=make([]string,4)
args[2]="-cp Ganesha_lib\\*"
args[3]="-jar Ganesha.jar"

var attr* os.ProcAttr


if err!=nil {
    fmt.Println("an error occurred.\n")


It's my first Go program. And i'm totally overwhelmed by getting the following error:

panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x0 pc=0x4278b5] goroutine 1 [running]: os.startProcess(0x4aacb4, 0x14, 0xf840001eb0, 0x500000005, 0x0, ...) C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist767862039/go/src/pkg/os/exec_posix.go:28 +0x152 os.StartProcess(0x4aacb4, 0x14, 0xf840001eb0, 0x500000005, 0x0, ...) C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist767862039/go/src/pkg/os/doc.go:24 +0x5c main.main() D:/MyGoProject/src/main.go:60 +0x23c goroutine 2 [syscall]: created by runtime.main C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist767862039/go/src/pkg/runtime/proc.c:221 Process finished with exit code 2

How do i have to interpret this error code? What went wrong? And how can i get the JVM startet - it's situated in a subdirectory of the Go executable file.

asked on Stack Overflow Dec 17, 2012 by dajood

3 Answers


It is normally recommended you don't use os.StartProcess directly. Instead, use os/exec which has a much easier interface. Here is how I would start a java subprocess and wait for it to complete.

package main

import (

func main() {
    var java = "\\jrex64\\bin\\java.exe"

    var path = []string{

    pathflag := "-Xbootclasspath:" + strings.Join(path, ";")
    cmd := exec.Command(java, "-verbose", pathflag, "-cp Ganesha_lib\\*", "-jar Ganesha.jar")
    err := cmd.Run()

    if err != nil {
        fmt.Println("an error occurred.\n")


In case you are curious, the reason you got that panic was that attr is a nil pointer. Instead, you could have done attr := new(os.ProcAttr).

answered on Stack Overflow Dec 17, 2012 by Stephen Weinberg • edited Dec 17, 2012 by Stephen Weinberg

The previous answers don't actually describe how to use os.StartProcess(). Here's an example:

cmdToRun := "/path/to/someCommand"
args := []string{"someCommand", "arg1"}
procAttr := new(os.ProcAttr)
procAttr.Files = []*os.File{os.Stdin, os.Stdout, os.Stderr}
if process, err := os.StartProcess(cmdToRun, args, procAttr); err != nil {
    fmt.Printf("ERROR Unable to run %s: %s\n", cmdToRun, err.Error())
} else {
    fmt.Printf("%s running as pid %d\n", cmdToRun, process.Pid)

Note that it's necessary to initialize the ProcAttr.Files field - if you don't, you may get an index out of bounds error deep in the os package. You can use pipes for the files if you want to provide input or process output from the new process in your own code. You may also want to specify the Dir (starting directory) and Env (environment variables) fields.

answered on Stack Overflow Jul 3, 2015 by David Tootill • edited Sep 6, 2019 by jochen


var attr* os.ProcAttr

proc, err := os.StartProcess(name, args, attr)

The attr variable is nil and when dereferenced in os.StartProcess it causes the error you see.

answered on Stack Overflow Dec 17, 2012 by zzzz

User contributions licensed under CC BY-SA 3.0