groovy win 命令行 class 和脚本

groovy win cmd line class and script

我正在尝试 运行 windows 上的一个 groovy(2.4.3) 脚本,该脚本调用一个 goovy class xxxxx.groovy。我已经使用 classpath 和各种脚本尝试了多种变体,下面是一些示例,总是出现 MultipleCompliationErrorsException.... unable to resolve class

class文件是firstclass.groovy

import org.apache.commons.io.FilenameUtils

class firstclassstart {

       def  wluid,  wlpwd,  wlserver, port

       private wlconnection, connectString, jmxConnector, Filpath, Filpass, Filname, OSRPDpath, Passphrase

       // object constructor
       firstclassstart(wluid, wlpwd, wlserver, port) {            
           this.wluid = wluid
           this.wlpwd = wlpwd
           this.wlserver = wlserver
           this.port = port

            }

       def isFile(Filpath) {
           // Create a File object representing the folder 'A/B'
           def folder = new File(Filpath)

           if (!org.apache.commons.io.FilenameUtils.isExtension(Filpath, "txt")) {
               println "bad extension"
               return false
           } else if (!folder.exists()) {
               // Create all folders up-to and including B
               println " path is wrong"
               return false
           } else
               println "file found"
           return true
       }
   }

命令行脚本test.groovy

import firstclass
def sample = new firstclass.firstclassstart("weblogic", "Admin123", "x.com", "7002")
//def sample = new firstclassstart("weblogic", "Admin123", "x.com", "7002")
sample.isFile("./firstclass.groovy")

..\groovy -cp "firstclass.groovy;commons-io-1.3.2.jar" testfc.groovy

脚本test.groovy

GroovyShell shell = new GroovyShell()
def script = shell.parse(new File('mylib/firstclass.groovy'))
firstclass sample = new script.firstclass("uid", "pwd", "url", "port")
sample.getstatus()

c:>groovy test.groovy

script test.groovy v2 将 firstclass.groovy 放在目录 test 下面的 script

import test.firstclass
firstclass sample = new script.firstclass("uid", "pwd", "url", "port")
sample.getstatus()

c:>groovy test.groovy

只是在寻找一种防弹、便携的方式来整理我的 java classes、.groovy classess 等和脚本。

谢谢

我认为您可以使用您的第一种方法:

groovy -cp mylib/firstclass.groovy mylib/test.groovy

但是我发现您的代码中存在一些问题,这些问题可能导致了 MultipleCompliationErrorsException

  1. 由于您在 class 路径中包含 firstclass.groovy,因此您必须在 test.groovy 中添加 import firstclass

  2. 为什么要在 test.groovy 中使用 script.firstclass?你 class 被简单地称为 firstclass.

  3. 在您的 firstclass.groovy 中,您使用的是 import org.apache.commons.io.FilenameUtils,可能还有其他,但是您没有将其包含在 class 路径中。

最后我认为,您必须将 test.groovy 更改为:

import firstclass
firstclass sample = new firstclass("uid", "pwd", "url", "port")
sample.getstatus()

然后在您的命令中将 apache Commons IO 的剩余包含添加到 class 路径。

groovy -cp "mylib/firstclass.groovy;commons-io-2.4.jar;" mylib/testexe.groovy

希望这对您有所帮助,

基于操作变化的更新:

改完后你有些地方不对,我试着列举一下:

  1. 如果你的文件名为 firstclass.groovy 你的 class 必须是 class firstclass 而不是 class firstclassstart.

  2. 在您的 test.groovy 中使用 new firstclass 而不是 new firstclass.firstclassstart

所以问题是,您的代码必须是:

class 文件 firstclass.groovy:

import org.apache.commons.io.FilenameUtils

class firstclass {

   def  wluid,  wlpwd,  wlserver, port

   private wlconnection, connectString, jmxConnector, Filpath, Filpass, Filname, OSRPDpath, Passphrase

   // object constructor
   firstclass(wluid, wlpwd, wlserver, port) {            
       this.wluid = wluid
       this.wlpwd = wlpwd
       this.wlserver = wlserver
       this.port = port

        }

   def isFile(Filpath) {
       // Create a File object representing the folder 'A/B'
       def folder = new File(Filpath)

       if (!org.apache.commons.io.FilenameUtils.isExtension(Filpath, "txt")) {
           println "bad extension"
           return false
       } else if (!folder.exists()) {
           // Create all folders up-to and including B
           println " path is wrong"
           return false
       } else
           println "file found"
       return true
   }
}

脚本test.groovy:

import firstclass
def sample = new firstclass("weblogic", "Admin123", "x.com", "7002")
sample.isFile("./firstclass.groovy")

最后执行它的命令:

groovy -cp "firstclass.groovy;commons-io-1.3.2.jar" test.groovy

进行此更改后,您的代码必须能够正常工作,我尝试了它并按预期工作。