如何在Golang中使用Jar文件?

是否可以在Go代码中使用jar文件类方法。 如果是这样,请转发给我相同的链接。 我在</ p>


GoDoc / github.com / code.google </ p>
</ blockquote>

中搜索了相同的内容 没有这样的软件包构建。</ p>
</ div>

展开原文

原文

Is it possible to use jar file class methods in Go code. If so than please forward me the link for the same. I have searched the same in

GoDoc/github.com/code.google

But there was no such package build.

jar
douke9379
douke9379 抱歉,那不可能。Java与Go无关。您到底想对jar文件做什么?如果您只想读出它,请记住jar文件只是一个带有不同后缀的zip文件。
6 年多之前 回复

2个回答



只有在极其古怪的意义上才是“可能的”,即您理论上可以在Go中编写某种JVM环境来运行JVM字节码, </ p>

从任何实际意义上讲,Java(和Scala等)以完全不同的方式编译Go的方式,这样做是不可行的。 Go具有自己的运行时,这尤其使情况变得复杂,运行时也使逆过程变得复杂(从Java运行Go函数)。</ p>

如果您需要与Go中的Java代码进行通信,我建议您研究一下 RPC ,您可以将Java程序作为独立的进程启动,并通过本地网络协同调用方法。 这需要一点努力,但不是很多。 当然,比编写框架真正从Go调用Java方法要容易得多。</ p>

另一个选择是使用 os / exec </ code>调用Java程序, 它们输出到文件或命令行,然后读取该输出并进行解析,但这比RPC脆弱得多,并且容易出错和出现安全漏洞。</ p>

Evan在 这些注释,您可以将JNI(Java本机接口)与C入口点一起使用。 因此,使用Cgo您可以启动JVM并调用Java代码。 站点似乎 提供有关如何在C和C ++中进行设置的教程。 我对从这个方向使用JNI不太熟悉,因此我既不推荐它也不劝阻您使用它。 不过,它确实</ strong>引入了C依赖性,因此请注意这一点。</ p>
</ div>

展开原文

原文

It's "possible" only in the extremely pedantic sense that you could theoretically write some sort of JVM environment in Go that can run JVM bytecode that happens to interact with memory allocated by Go.

In any practical sense, Java (and Scala etc) compile in completely different ways to Go and it's not feasible to do this. This is especially complicated by Go having its own runtime which complicates the inverse as well (running Go functions from Java).

If you need to communicate with Java code from Go, I advise looking into RPC, you can launch a Java program as a separate process and cooperatively call methods over your local network. This takes a little effort, but not very much. It's certainly easier than writing the framework to truly invoke Java methods from Go would be.

The other option is to invoke Java programs by using os/exec, having them output to a file or command line, and then reading in that output and parsing it, but that's a lot more fragile than RPC and prone to errors and security holes.

As Evan mentions in the comments, you can use the JNI (Java Native Interface) with a C entrypoint. So using Cgo you can launch the JVM and call Java code. This site seems to have a tutorial on how to set it up in C and C++. I'm not very familiar with using JNI from this direction so I can't really recommend it nor dissuade you from using it. It does introduce a C dependency into your build process though, so be aware of that.

duanmaifu3428
duanmaifu3428 尖刺似乎死了。
3 年多之前 回复
dongwen3410
dongwen3410 您链接到的JNI绑定生成器已移至github.com/skanjo/jag
3 年多之前 回复
doubian0284
doubian0284 -实际上我并不知道这种方法有效,我认为JNI就像Cgo:必须以Java程序作为入口点来调用它。 我将添加。
6 年多之前 回复
duanluo9369
duanluo9369 第三种选择是使用Java本机接口(en.wikipedia.org/wiki/Java_native_interface)和CGo(golang.org/cmd/cgo)编写C桥
6 年多之前 回复



我有一个类似的用例。 我将kafka用作消息总线。 生产者在scala中,而消费者是一个go应​​用程序。 这可以无缝运行。 最好的部分是,它可以扩展任何分布式设置。 </ p>
</ div>

展开原文

原文

I have a similar usecase. I use kafka as a message bus. The producer is in scala and consumer is a go application. This works seamlessly. Best part of this is, it can scale on any distributed setup.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐