簡體   English   中英

groovy win cmd行類和腳本

[英]groovy win cmd line class and script

我正在嘗試在調用goovy類xxxxx.groovy的Windows上運行groovy(2.4.3)腳本。 我已經嘗試過使用類路徑和各種腳本進行多種變體,下面是一些示例,但始終會出現MultipleCompliationErrorsException...。無法解析類

classfile是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
       }
   }

cmd行腳本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

腳本test.groovy v2將firstclass.groovy放在腳本下面的目錄測試中

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

c:>groovy test.groovy

只是尋找一種防彈,可移植的方式來整理我的java類,.groovy類等和腳本。

謝謝

我認為您可以使用例如第一種方法:

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

但是,我在您的代碼中看到一些問題,可能會導致MultipleCompliationErrorsException

  1. 由於要在類路徑中包含firstclass.groovy,因此必須在test.groovy添加import firstclass

  2. 你為什么要使用script.firstclasstest.groovy 你被稱為firstclass

  3. 在您的firstclass.groovy您正在使用import org.apache.commons.io.FilenameUtils以及其他,但是您並未將其包括在類路徑中。

所以最后我認為,您必須將test.groovy更改為以下內容:

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

然后在命令中將apache Commons IO的其余內容添加到類路徑中。

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

希望這可以幫助,

根據OP更改進行更新:

更改后,您有些錯誤,我嘗試列舉一下:

  1. 如果您的文件名為firstclass.groovy您的類必須是class firstclass而不是class firstclassstart

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

因此,您的代碼必須是:

類文件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

進行此更改后,您的代碼必須可以正常工作,我可以嘗試並按預期工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM