Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Jar file in Golang?

Tags:

jar

go

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.

like image 573
user Avatar asked May 26 '14 14:05

user


People also ask

Is it possible to read a JAR file in Golang?

If you just want to read it out, remember that a jar file is just a zip with a different suffix. Show activity on this post. 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.

How to create a dockerfile for your Golang application?

We need to create a Dockerfile for our GoLang application. Simply create a file in the project directory called Dockerfile. This name is preferred since it clearly tells what it does. The docker file is a set of instructions that we telling docker to do when running that application.

What is Io/ioutil in Golang?

Golang offers a vast inbuilt library that can be used to perform read and write operations on files. In order to read from files on the local system, the io/ioutil module is put to use. The io/ioutil module is also used to write content to the file.

How to read a file in Go language?

Reading a file in GoLang 1. Open a file for reading The first step is to open the file for reading. We can use the os package Open () function to open the file. We also must make sure the file is closed after the operation is done. So, we can use the defer keyword to send the function execution at last.


1 Answers

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.

like image 154
Linear Avatar answered Oct 05 '22 04:10

Linear