简体   繁体   English

导入Java库groovy

[英]Import Java library groovy

i want to use a method defined in the apache.common.net library in my groovy-Script. 我想在我的groovy-Script中使用apache.common.net库中定义的方法。

I first downloaded and included it in my config: 我首先下载并将其包含在我的配置中:

            this.class.classLoader.rootLoader.addURL(new URL("file:///${currentDir}/lib/commons-net-3.3.jar"))

Afterwards i try to use it in my groovy-script like this (to make it clear: the import pimpim.* imports also the classLoader above): 之后我尝试在我的groovy脚本中使用它(为了说清楚:导入pimpim。*导入上面的classLoader):

    import pimpim.*

import org.apache.commons.net.ftp.*

def pm = PM.getInstance("test")


public class FileUploadDemo {
  public static void main(String[] args) {
    FTPClient client = new FTPClient();

I also tried several annotations for the "import" like 我也试过几个注释为“导入”之类的

import org.apache.commons.net.ftp.FTPClient

But i keep getting this error: 但我一直收到这个错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Y:\pimconsole\scripts\ftp.gy: 11: unable to resolve class FTPClient
 @ line 11, column 15.
       FTPClient client = new FTPClient();

What did i miss? 我错过了什么? Sorry, i am still new to groovy :/ 对不起,我还不熟悉groovy:/

So, you can add it to the classpath when you start up your script; 因此,您可以在启动脚本时将其添加到类路径中;

groovy -cp .;lib/commons-net-3.3.jar ftp.gy

Or, you can add a @Grab annotation to your script, and Groovy will download the dependency and add it to the classpath before running (but this can not work if your scripts are executed on a box with no access to maven); 或者,您可以在脚本中添加@Grab注释,Groovy将在运行之前下载依赖项并将其添加到类路径中(但如果您的脚本在无法访问maven的框中执行,则无法执行此操作);

@Grab('commons-net:commons-net:3.3')
import org.apache.commons.net.ftp.*

...rest of your script...

Or the classpath hacking route you have above should work if you try: 或者,如果您尝试:上面的类路径黑客路由应该有效:

this.getClass().classLoader.rootLoader.addURL(new File("lib/commons-net-3.3.jar").toURL())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM