在Go中启动一个Java进程

huangapple go评论75阅读模式
英文:

Starting a Java-process in Go

问题

我正在尝试使用Go启动一个Java进程,但无法让Java识别到类路径。代码看起来有点像这样:

args := []string{
  "-Xmx64m",
  "-Dmy.property=value",
  "-cp",
  "lib/jar1.jar:lib/jar2.jar",
  "com.things.MyClass",
}
c := exec.Command(javaBinary, args...)

不幸的是,当我执行这段代码时,JVM会报错Error: Could not find or load main class。然而,如果我将c.Args的输出直接在终端中运行,似乎一切正常,这表明我可能以某种错误的方式启动了进程。

有没有更好的方法来做这个?

英文:

I am trying to start a Java-process using Go but am unable to get Java to recognise the classpath. The code looks somewhat like:

args := []string{
  "-Xmx64m",
  "-Dmy.property=value,
  "-cp",
  "lib/jar1.jar:lib/jar2.jar",
  "com.things.MyClass",
}
c := exec.Command(javaBinary, args...)

Unfortunately when executing this I get the dreaded Error: Could not find or load main class from the JVM. However if I take the output from c.Args and run it directly in a terminal it seems to work just fine, which to me indicates that I am somehow launching the process incorrectly.

Is there a better way of doing this?

答案1

得分: 1

请忽略这个问题,请注意,错误在 args 数组中有一个额外的空格:

args := []string{
  "-Xmx64m",
  "-Dmy.property=value ", //<--尾随空格
  ...
}

这个额外的空格导致进一步的解析停止,从而导致缺少类路径。

英文:

Disregard this question please, the error was an extra space in the args array:

args := []string{
  &quot;-Xmx64m&quot;,
  &quot;-Dmy.property=value &quot;, //&lt;--trailing space
  ...
}

The extra space stops further parsing from continuing leading to a missing classpath.

huangapple
  • 本文由 发表于 2015年4月10日 22:26:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/29563992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定