得到一些愚蠢的语法错误

Jon*_*ony 0 java ant

在行获取语法错误:

`List<Class> findClasses(File directory)` throws `ClassNotFoundException`...
Run Code Online (Sandbox Code Playgroud)

无法弄清楚原因.这是我的代码.

import org.apache.tools.ant.Task;
import java.util.*;
import java.io.*;

public class CreateTestPackage extends Task
{
 String allTestsFile = getProject().getProperty("alltestfiles")+ getProject().getProperty("testfile");
 public void execute()
 {
  List<Class> findClasses(File directory) throws ClassNotFoundException
  {

   List<Class> classes = new ArrayList<Class>();
   if (!directory.exists())
   {
    return classes;
   }
   File[] files = directory.listFiles(new FilenameFilter()
   {
    public boolean accept( File dir, String name )
    {
     return name.matches("test.*\\.class");
    }
   });
   for (File file : files)
   {
    if (file.isDirectory())
    {
     assert !file.getName().contains(".");
     classes.addAll(findClasses(file));
    }
    else if (file.getName().endsWith(".class"))
    {
     classes.add(Class.forName(file.getName().substring(0, file.getName().length() - 6)));
    }
   }
   return classes;
  }
  for(Class c : classes)
  {
     string packageName=c.getPackage().getName();
        BufferedWriter out =null;
        try
        {
           out = new BufferedWriter(new FileWriter(testfile));
           out.write(packageName+"\n");         
        }
        catch (IOException e)
        {
          e.printStackTrace();
       }
        finally
        {
          if (out!=null)
           try
           {
              out.close();
           }
           catch (IOException e)
           {
              e.printStackTrace();
           }
       }
  }

 }
}
Run Code Online (Sandbox Code Playgroud)

jjn*_*guy 6

问题是你定义了execute()方法,然后尝试在其中定义findClasses()方法.

这不是合法的Java语法.

execute在定义另一个方法之前,需要关闭方法体.