java - groovy win cmd line class and script -
i'm trying run groovy(2.4.3) script on windows calls goovy class xxxxx.groovy. i've tried number of variations using classpath , various scripts, examples below, getting multiplecompliationerrorsexception.... unable resolve class
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 file object representing 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 folders up-to , including b println " path 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 bullet proof, portable way oranize java classes, .groovy classess, etc. , scripts.
thanks
i think can using example first approach:
groovy -cp mylib/firstclass.groovy mylib/test.groovy
however see problems in code causing multiplecompliationerrorsexception
.
since you're including firstclass.groovy in classpath, you've add
import firstclass
intest.groovy
.why using
script.firstclass
intest.groovy
? you're class calledfirstclass
.in
firstclass.groovy
you're usingimport org.apache.commons.io.filenameutils
, other, you're not including in classpath.
so think that, you've change test.groovy
like:
import firstclass firstclass sample = new firstclass("uid", "pwd", "url", "port") sample.getstatus()
and in command add remaining includes apache commons io classpath.
groovy -cp "mylib/firstclass.groovy;commons-io-2.4.jar;" mylib/testexe.groovy
hope helps,
update based on op changes:
after changes you've things wrong, try enumerate it:
if file called
firstclass.groovy
class mustclass firstclass
notclass firstclassstart
.in
test.groovy
usenew firstclass
notnew firstclass.firstclassstart
.
so thing is, 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 file object representing 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 folders up-to , including b println " path 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 command execute it:
groovy -cp "firstclass.groovy;commons-io-1.3.2.jar" test.groovy
with changes code must works, try , works expected.
Comments
Post a Comment