0

我创建了一个Java程序,它读取文件,使用 Java 中的反射 API 确定要执行的操作(创建对象,调用类的方法)。例如:

2234:org.powertac.common.Rate::9::new
2234:org.powertac.common.Rate::9::withValue::-0.5

如果我找到new关键字,那么我会尝试创建该类的新对象。否则,我将使用参数调用方法(在示例中为withValue()-0.5。源代码如下图所示:

//
// program created a map between int and string
//
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
import java.lang.reflect.*;
import org.powertac.common.*;
import org.powertac.util.*;

public class Parser {

    private String filename;
    private Map< Integer, Object > map;
    //
    // just the default constructor is needed for now
    //
    public Parser(){
        this.map = new HashMap< Integer, Object >();
        this.filename = "/home/phillipe/powertac/powertac-server/" +
                        "server-distribution/log/powertac-boot.state";
    }
    //
    // override the default constructor
    //
    public Parser( String filename ){
        this.filename = filename;
    }

    public void parse() throws ClassNotFoundException{
        try{
            //
            // assure file exists before parsing
            //
            FileReader fr = new FileReader( this.filename );
            BufferedReader textReader = new BufferedReader( fr );
            String line;
            Integer id = 1;
            while(( line = textReader.readLine()) != null ){
                Pattern p = Pattern.compile("([^:]+):([^:]+)::([\\d]+)::([^:]+)::(.+)");
                Matcher m = p.matcher( line );
                if (m.find()) {
                  //String id = m.group(1);
                  String className = m.group(2);
                  int orderOfExecution = Integer.valueOf( m.group( 3 ));
                  String methodNameOrNew = m.group(4);
                  Object[] arguments = m.group(5).split("::");

                  if( methodNameOrNew.compareTo( "new" ) == 0 ){
                      System.out.println("Loading class: " + className);
                      Competition cc = null;


                      if( className.contains("Competition")){
                          this.map.put(id, cc);
                      }
                      else if( className.contains( "LocalBroker" )){
                          continue;
                      }
                      else {
                          Class<?> cl = Class.forName( className );
                          Constructor<?> cons = cl.getConstructor(String.class);
                          Object obj = cons.newInstance( arguments );
                          this.map.put( id , obj );
                      }

                  }
                  else{
                      Class<?> cl = Class.forName( className );
                      Method method = cl.getMethod( methodNameOrNew, String.class );
                      this.map.put(id, method);
                  }
                  id++;
                  System.out.printf("%s %s %d %s %d\n", id, className, orderOfExecution, methodNameOrNew, arguments.length);
                }
            }
            textReader.close();
        }
        catch( IOException ex ){
            ex.printStackTrace();
        }
        catch( ArrayIndexOutOfBoundsException ex){
            ex.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void print(){
        Iterator<?> iterator = this.map.keySet().iterator();  

        while (iterator.hasNext()) {  
           String key = iterator.next().toString();  
           String value = this.map.get(key).toString();  

           System.out.println(key + " " + value);  
        }  
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Parser parse = new Parser();
        try {
            parse.parse();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.exit( 0 );

    }

}

但是,我收到以下错误:

Loading class: org.powertac.common.Competition
2 org.powertac.common.Competition 0 new 1
Exception in thread "main" java.lang.NoClassDefFoundError: org/aspectj/lang/Signature
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at Parser.parse(Parser.java:71)
    at Parser.main(Parser.java:123)
Caused by: java.lang.ClassNotFoundException: org.aspectj.lang.Signature
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)

我加载了.jar包含类二进制文件的文件。这是我第一次做这种任务Java,所以我不是很熟悉....有人可以帮帮我吗?它是缺少图书馆还是类似的东西?谢谢

4

2 回答 2

2

您需要在类路径中为要调用的类以及这些类的所有依赖项设置所有库。因此,在这种情况下,似乎 org.powertac.common.Competition 依赖于 org.aspectj.lang.Signature。

于 2012-08-27T23:53:33.577 回答
1

此类org.aspectj.lang.Signature是 Aspectj 运行时库的一部分。如果您使用的是 Maven,您可以添加:

<dependency>
    <groupId>aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.5.4</version>
</dependency>

到您的 POM.xml 以检索丢失的 JAR 文件。

或直接:

aspectjrt- 1.5.4.jar以及 aspectjweaver-1.5.4.jarlog4j

于 2012-08-28T00:05:20.770 回答