1

I'm trying to run a groovy(2.4.3) script on windows that calls a goovy class xxxxx.groovy. I've tried a number of variations using classpath and various scripts, some examples below, always getting MultipleCompliationErrorsException.... unable to resolve class

classfile is 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 line script 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

script 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 put firstclass.groovy in directory test below script

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

c:>groovy test.groovy

just looking for a bullet proof, portable way to oranize my java classes, .groovy classess, etc. and scripts.

Thanks

user3120960
  • 179
  • 1
  • 3
  • 11

1 Answers1

0

I think that you can do using for example your first approach:

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

However I see some problems in your code which are probably causing MultipleCompliationErrorsException.

  1. Since you're including firstclass.groovy in your classpath, you've to add the import firstclass in the test.groovy.

  2. Why are you using script.firstclass in test.groovy? you're class is called simply firstclass.

  3. In your firstclass.groovy you're using import org.apache.commons.io.FilenameUtils and probably other, however you're not including it in the classpath.

So finally I think that, you've to change your test.groovy for something like:

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

And in your command add the remaining includes for apache Commons IO to the classpath.

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

Hope this helps,

UPDATE BASED ON OP CHANGES:

After the changes you've some things wrong, I try to enumerate it:

  1. If your file is called firstclass.groovy your class must be class firstclass not class firstclassstart.

  2. In your test.groovy use new firstclass not new firstclass.firstclassstart.

So the thing is, your code must be:

class file 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
   }
}

script test.groovy:

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

Finally the command to execute it:

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

With this changes your code must works, I try it and works as expected.

albciff
  • 18,112
  • 4
  • 64
  • 89
  • 1) ok; 2) my typo from another try; 3) ok I'm still getting an error on the import statement. I tried adding ../lib/groovy-2.4.34.jar to the cp and same thing. I also tried evaluate(new File("mylib/firstclass.groovy")) from this post http://stackoverflow.com/questions/9136328/including-a-groovy-script-in-another-groovy. With this I get 2 errors 1 pointing to sample and one pointing to new. – user3120960 May 12 '15 at 13:21
  • I get 5: unable to resolve firstclass.firstclassstart with the ^ under new; I changed the syntax slightly line2 in testfc is def sample = new firstclass("uid", "pwd", "url", "port"). I simplifed the setup and put everything in the same directory and ran ..\groovy -cp "firstclass.groovy;commons-io-1.3.2.jar" testfc.groovy I tried import fisrtclassstart and import firstclass.firstclassstart. – user3120960 May 12 '15 at 23:35
  • @user3120960 I update my answer based on your changes, take a look on it `:)`. – albciff May 13 '15 at 06:55
  • Thanks a bunch; that does work. The thing that was confusing me is in my original code in IntelligjIDEA, I was getting a duplicate class error. I now figured out that this was because of my placement of Logger logger = Logger.getLogger("") before the class statement. – user3120960 May 13 '15 at 12:14
  • @user3120960 perfect that now it works, nice to help you `:)` – albciff May 13 '15 at 12:37