简体   繁体   中英

Groovy script to run java main class

I have below code of main class that i am trying to run with gradle . i have attached the sample main class of java and gradle script below.

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

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. To see the execution of main program I have added sample sysout and file creation logic with PrintWriter. I have also tried with ant with gradle hello this is also not working.

Now when i use the gradle task as below removed fork true from previous

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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