Java groovy win cmd行类和脚本
我试图在windows上运行一个groovy(2.4.3)脚本,该脚本调用一个goovy类xxxxx.groovy。我使用类路径和各种脚本尝试了许多变体,下面是一些示例,总是得到multipleCompliationErrorException。。。。无法解析类 类文件是firstclass.groovyJava groovy win cmd行类和脚本,java,windows,class,groovy,Java,Windows,Class,Groovy,我试图在windows上运行一个groovy(2.4.3)脚本,该脚本调用一个goovy类xxxxx.groovy。我使用类路径和各种脚本尝试了许多变体,下面是一些示例,总是得到multipleCompliationErrorException。。。。无法解析类 类文件是firstclass.groovy import org.apache.commons.io.FilenameUtils class firstclassstart { def wluid, wlpwd,
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
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
scripttest.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
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中
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
但是,我发现您的代码中存在一些问题,这些问题可能会导致multiplecompliationerrorexception
test.groovy
中添加import firstclass
test.groovy
中使用script.firstclass
?你的班级被简单地称为firstclass
firstclass.groovy
中,您使用的是import org.apache.commons.io.FilenameUtils
和其他文件,但是您没有将其包含在类路径中groovy -cp "mylib/firstclass.groovy;commons-io-2.4.jar;" mylib/testexe.groovy
最后,我认为,您必须将test.groovy
更改为以下内容:
import firstclass
firstclass sample = new firstclass("uid", "pwd", "url", "port")
sample.getstatus()
在命令中,将剩余的包含添加到类路径中
groovy -cp "mylib/firstclass.groovy;commons-io-2.4.jar;" mylib/testexe.groovy
希望这有帮助
根据OP更改进行更新:
在这些变化之后,你有一些错误,我试着列举:
firstclass.groovy
则您的类必须是classfirstclass
而不是classfirstclassstart
test.groovy
中,使用newfirstclass
而不是newfirstclass.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
}
}
import firstclass
def sample = new firstclass("weblogic", "Admin123", "x.com", "7002")
sample.isFile("./firstclass.groovy")
脚本test.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
}
}
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
有了这些更改,您的代码必须正常工作,我尝试了一下,并按预期工作。1)好;2) 我的打字错误来自另一次尝试;3) 好的,我仍然在import语句中得到一个错误。我尝试将../lib/groovy-2.4.34.jar添加到cp中,并且做了同样的事情。我还尝试从这篇文章中评估(新文件(“mylib/firstclass.groovy”)。有了这个,我得到了两个错误,一个指向sample,一个指向new;我稍微修改了testfc中第2行的语法是def sample=newfirstclass(“uid”、“pwd”、“url”、“port”)。我简化了设置并将所有内容放在同一个目录中,然后运行..\groovy-cp“firstclass.groovy;commons-io-1.3.2.jar”testfc.groovy我尝试导入firstclassstart并导入firstclass.firstclassstart。@user3120960我根据您的更改更新了我的答案,查看一下
:)
。谢谢;这确实有效。让我困惑的是,在IntelligJDead的原始代码中,我得到了一个重复的类错误。我现在发现这是因为我在class语句之前放置了Logger Logger=Logger.getLogger(“”)。@user3120960现在它可以正常工作了,很高兴能帮助您:)