Executing JAR from Go

235 views
Skip to first unread message

Shivli Srivastava

unread,
Apr 15, 2024, 7:02:24 AM4/15/24
to golang-nuts
I created a jar where I want to run the main function of the Sign class . 

jar tf DigitalSign.jar

META-INF/MANIFEST.MF
org/
org/Sign.class
META-INF/
bcmail-jdk15on-1.59.jar
xml-apis-1.4.01.jar
xercesImpl-2.12.2.jar
bcutil-jdk18on-1.77.jar
bcprov-jdk18on-1.77.jar
bcpkix-jdk15on-1.55.jar
bcprov-jdk15on-1.59.jar

When I run the command on my terminal , everything works as fine but running it using exec.Command gives Error: Could not find or load main class org.Sign
Caused by: java.lang.ClassNotFoundException: org.Sign . 

What am I missing here ?

Shivli Srivastava

unread,
Apr 15, 2024, 7:04:28 AM4/15/24
to golang-nuts
this is my code

func runCommand(ctx context.Context) (string, error) {

if JVMFound(context.Background()) {
jarPath := "/Users/shivli.srivastava/Downloads/jars/bcmail-jdk15on-159.jar:" +
"/Users/shivli.srivastava/Downloads/jars/bcpkix-jdk15on-155.jar:" +
"/Users/shivli.srivastava/Downloads/jars/bcprov-jdk15on-155.jar:" +
"/Users/shivli.srivastava/Downloads/jars/xml-apis-1.4.01.jar:" +
"/Users/shivli.srivastava/Downloads/jars/xercesImpl-2.12.2.jar:" +
"DigitalSign.jar"

output, err := exec.Command("java", "-cp", jarPath, "org/Sign").CombinedOutput()
boot.Logger(ctx).Infow("", map[string]interface{}{
"output": string(output),
})
//cmd.Env = os.Environ()

if err != nil {
return "", err
}

return string(output), nil
}
return "", cerror.InternalGatewayError.New("").Wrap(fmt.Errorf("JVM Not Found"))
}

Tamás Gulácsi

unread,
Apr 16, 2024, 12:20:50 AM4/16/24
to golang-nuts
What is the _working_ command line?
Ain't something like ```java -cp ... -jar DigitalSign.jar org.Sign``` ?

Tamás Gulácsi

unread,
Apr 16, 2024, 12:21:52 AM4/16/24
to golang-nuts
Another possible error is not setting the CWD (cmd := exec.CommandComntext(); cmd.Dir = `the-dir-where-DigitalSign,jar-is`).

Kurtis Rader

unread,
Apr 16, 2024, 12:39:56 AM4/16/24
to Shivli Srivastava, golang-nuts
On Mon, Apr 15, 2024 at 4:02 AM Shivli Srivastava <shiv...@gmail.com> wrote:
When I run the command on my terminal , everything works as fine but running it using exec.Command gives Error: Could not find or load main class org.Sign
Caused by: java.lang.ClassNotFoundException: org.Sign .

Show us the command you ran interactively. Was it literally the following (since that is what your Go code runs):

java -cp /Users/shivli.srivastava/Downloads/jars/bcmail-jdk15on-159.jar:/Users/shivli.srivastava/Downloads/jars/bcpkix-jdk15on-155.jar:/Users/shivli.srivastava/Downloads/jars/bcprov-jdk15on-155.jar:/Users/shivli.srivastava/Downloads/jars/xml-apis-1.4.01.jar:/Users/shivli.srivastava/Downloads/jars/xercesImpl-2.12.2.jar:DigitalSign.jar org/Sign

If you copy/paste the above command into your interactive shell what happens?

--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Roland Müller

unread,
Apr 16, 2024, 1:35:12 AM4/16/24
to Kurtis Rader, Shivli Srivastava, golang-nuts
The class should be written as org.Sign instead of org/Sign.
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD9T7tZhc2s0P%2Bz6c9xCn2%3DnFiGKykVq0i4-%2Bg9C_xS7vQ%40mail.gmail.com.
>

Kurtis Rader

unread,
Apr 16, 2024, 1:45:13 AM4/16/24
to Roland Müller, Shivli Srivastava, golang-nuts
On Mon, Apr 15, 2024 at 10:34 PM Roland Müller <rol...@gmail.com> wrote:
The class should be written as org.Sign instead of org/Sign.

Agreed. My point was that the O.P. wrote "org/Sign" in their Go source code and didn't show us the command they ran interactively. There may be multiple problems with the O.P.'s Go code. But until they tell us what command they successfully ran at an interactive shell we can only guess at what is wrong with their Go source code.

Roland Müller

unread,
Apr 16, 2024, 2:04:23 AM4/16/24
to Kurtis Rader, Shivli Srivastava, golang-nuts
That my point too.


Am Dienstag, 16. April 2024 schrieb Kurtis Rader <kra...@skepticism.us>:

Shivli Srivastava

unread,
Apr 17, 2024, 2:12:24 AM4/17/24
to Roland Müller, golan...@googlegroups.com
The class should be written as org.Sign instead of org/Sign.

I tried with both , both are valid options

Kurtis Rader

unread,
Apr 17, 2024, 2:40:43 AM4/17/24
to Shivli Srivastava, golan...@googlegroups.com
On Tue, Apr 16, 2024 at 11:09 PM Shivli Srivastava <shiv...@gmail.com> wrote:
On Wed, Apr 17, 2024 at 11:37 AM Shivli Srivastava <shiv...@gmail.com> wrote:
java -cp /Users/shivli.srivastava/Downloads/jars/bcmail-jdk15on-159.jar:/Users/shivli.srivastava/Downloads/jars/bcpkix-jdk15on-155.jar:/Users/shivli.srivastava/Downloads/jars/bcprov-jdk15on-155.jar:/Users/shivli.srivastava/Downloads/jars/xml-apis-1.4.01.jar:/Users/shivli.srivastava/Downloads/jars/xercesImpl-2.12.2.jar:DigitalSign.jar org/Sign 

It ran as expected in my working directory . 
The issue was setting the current working directory .
After setting the wd the command ran as expected.
wd, err := os.Getwd()

if err != nil {
return "", err
}
command.Dir = wd


I am confused by your reply. If the os/exec.Cmd.Dir structure member is the empty string the os/exec package should use the current directory. The code you showed us when you started this discussion thread used exec.Command() which would have defaulted the Cmd.Dir structure member to the empty string. Furthermore, your original code looked like exec.Command().CombinedOutput(); it didn't have an explicit command variable whose Dir structure member could be altered. So it seems like you aren't telling us the whole story.
Reply all
Reply to author
Forward
0 new messages