Groovy cli script doesn't set classpath

29 Views Asked by At

I get the following when executing my Groovy script:

satchwinston@MacBook-Air groovy-cli % ./encryptor.groovy -e hgjjgjgkgk
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/Users/satchwinston/Development/test/groovy-cli/encryptor.groovy: 11: unable to resolve class bnc.Encryption
 @ line 11, column 1.
   import bnc.Encryption
   ^

1 error

Here is the script:

#!/bin/sh
'''':
exec groovy -cp  "/Users/satchwinston/Development/test/picocli-groovy-4.7.5.jar:bnc.jar" "$0"
'''

import java.nio.file.Files
import java.nio.file.Path

import groovy.cli.picocli.CliBuilder

import bnc.Encryption

def encrypt(string) {
  (new Encryption()).encrypt(string)
}

def decrypt(string) {
  (new Encryption()).decrypt(string)
}

// Main
def cli = new CliBuilder(usage:'encryptor ([-e] | [-d]) ([-in filename] | <arg>) > <filename>', header:'Options')
cli.e('encrypt')
cli.d('decrypt')
cli.in('input filename')
def options = cli.parse(args)

if (args.length = 0 || (args.length != 2 && args.length != 3)) {
  println(cli.usage())
  System.exit(1) 
}

def e = options.e ?: false
def d = options.d ?: false
def in = options.in ?: ""
def str = in.length == 0 ? args[1] : ""

if (e && str.length() > 0)
  println(encrypt(str))
else if (e)
  println(encrypt(Files.readString(Path.of(in))))
else if (d && str.length > 0)
  println(decrypt(str))
else if (d)
  println(decrypt(Files.readString(Path.of(in))))
else {
  println(cli.usage())
  System.exit(1)
}

System.exit(0)


It looks like it can use the picocli jar but not my bnc.jar.

satchwinston@MacBook-Air test % jar -tf bnc.jar
META-INF/
META-INF/MANIFEST.MF
bnc/Encryption.class
satchwinston@MacBook-Air test % 

I had this problem with Scala too. I had to resort to using a Scala directive: //> using jars /Users/satchwinston/Development/test/bnc.jar

Using --classpath did not work.

0

There are 0 best solutions below