英文:
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{
"-Xmx64m",
"-Dmy.property=value ", //<--trailing space
...
}
The extra space stops further parsing from continuing leading to a missing classpath.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论