我有以下代码的主类,我试图用gradle运行。我已经在下面附加了示例主类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();
}
}
}
}
我的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任务时,从前面的
中删除了fork truetask 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 :(得分:1)
试试这个
javaexec {
main = 'com.pack.YourClass'
classpath(sourceSets.src.output.classesDir, sourceSets.src.compileClasspath)
args(arg1, arg2)
}