doulu8537 2014-05-26 14:24
浏览 3084
已采纳

如何在Golang中使用Jar文件?

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.

  • 写回答

2条回答 默认 最新

  • doutenglou6588 2014-05-26 14:36
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站