2014-09-28 107 views
1

我有下面的代碼主要類,我試圖用gradle運行。我在下面附加了java和gradle腳本的示例主類。Groovy腳本運行java主類

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

而且我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') 
} 

當我打電話gradle這個hello1從命令提示符它說,成功打造但是似乎我的主要程序永遠不會被執行。要查看主程序的執行情況,我已經使用PrintWriter添加了示例sysout和文件創建邏輯。 我也試過用螞蟻與gradle hello這也是行不通的。

現在,當我使用gradle這個任務,因爲下方拆下叉真正從以前的

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) 
+0

有什麼特別的原因,您需要使用來執行代碼'Hello'類?您可以將'Hello'類的內容直接添加到'build.gradle'(或者甚至單個方法 - main的內容)並直接從腳本運行。 – Opal 2014-09-28 18:33:59

+0

實際上,我正在使用具有連接相關邏輯的java工具之一。我將這段代碼放在單獨的gradle項目中。我正在做一些自定義的任務來運行竹集成和運行這個類多數民衆贊成爲什麼避免投入gradle – Sam 2014-09-28 19:08:03

回答

1

試試這個

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

實際上我想運行java作爲後臺進程,所以不使用javaExec調用。並試圖用螞蟻或時髦的過程來實現 – Sam 2014-09-29 13:27:05