简体   繁体   English

Groovy脚本运行Java主类

[英]Groovy script to run java main class

I have below code of main class that i am trying to run with gradle . 我在下面尝试使用gradle运行的主类代码。 i have attached the sample main class of java and gradle script below. 我已经在下面附加了Java和gradle脚本示例主类。

public class Hello {
   public static void main(String[] args) throws IOException {

      System.out.println("This is file system watch service"); 
      Path dir = Paths.get("c:\\sid\\");
      WatchService service = FileSystems.getDefault().newWatchService();
      WatchKey key = dir.register(service, ENTRY_CREATE);

      System.out.println("Watching directory: "+dir.toString());
      //file creation logic
      while(true) {
         for (WatchEvent<?> event: key.pollEvents()) {
            WatchEvent.Kind<?> kind = event.kind();
            if (kind == OVERFLOW) {
               continue;
            }

            WatchEvent<Path> ev = (WatchEvent<Path>)event;
            Path filename = ev.context();
         }
      }
   }
}

Also my gradle script is as below 另外我的gradle脚本如下

apply plugin: 'java'
task hello1 << {
   def process = ['java', '-cp', 'sourceSets.main.runtimeClasspath',  'com.test.gradle.Hello'].execute()
   process.in.close()
   process.out.close()
   process.err.close()
}

task hello << {
   ant.java(classpath:'sourceSets.main.runtimeClasspath', classname:'com.test.gradle.Hello', fork:'true')
   ant.echo('Done')
}

When I call gradle hello1 from command prompt it says build successful however it seems my main program never gets executed. 当我从命令提示符下调用gradle hello1 ,它说生成成功,但是似乎我的主程序从未执行过。 To see the execution of main program I have added sample sysout and file creation logic with PrintWriter. 为了查看主程序的执行情况,我使用PrintWriter添加了示例sysout和文件创建逻辑。 I have also tried with ant with gradle hello this is also not working. 我也尝试过用gradle的蚂蚁hello这也不起作用。

Now when i use the gradle task as below removed fork true from previous 现在当我如下使用gradle任务时,从以前的版本中删除了true

task hello << {
    ant.java(classpath:'sourceSets.main.runtimeClasspath.asPath', classname:'com.test.gradle.Hello.class')
    ant.echo('Done')

}

it is generating below exception

[ant:java] Could not find com.test.gradle.Hello.class. Make sure you have it in
your classpath
        at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:13
8)
        at org.apache.tools.ant.taskdefs.Java.run(Java.java:771)
        at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:221)
        at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:135)
        at org.apache.tools.ant.taskdefs.Java.execute(Java.java:108)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.jav
a:106)
        at groovy.util.AntBuilder.performTask(AntBuilder.java:319)
        at groovy.util.AntBuilder.nodeCompleted(AntBuilder.java:264)
        at org.gradle.api.internal.project.ant.BasicAntBuilder.nodeCompleted(Bas
icAntBuilder.java:72)
        at groovy.util.BuilderSupport.doInvokeMethod(BuilderSupport.java:147)
        at groovy.util.AntBuilder.doInvokeMethod(AntBuilder.java:203)
        at org.gradle.api.internal.project.ant.BasicAntBuilder.doInvokeMethod(Ba
sicAntBuilder.java:87)
        at groovy.util.BuilderSupport.invokeMethod(BuilderSupport.java:64)
        at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaC
lassSite.java:45)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSi
teArray.java:45)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCa
llSite.java:108)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCa
llSite.java:116)
        at build_1dcr9mg141bsc4tjjgplovccq0$_run_closure2.doCall(C:\Users\sumit\
workspace1\gradleTest\build.gradle:13)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:
90)

Try this 尝试这个

javaexec {
    main = 'com.pack.YourClass'
    classpath(sourceSets.src.output.classesDir, sourceSets.src.compileClasspath)
    args(arg1, arg2)
}

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

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