动态创建对象时找不到类 - Java

时间:2012-08-27 23:44:40

标签: java

我创建了一个Java程序,它读取一个文件,使用Java中的Reflection 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做这样的任务,所以我不是很熟悉......有人可以帮帮我吗?它是一个缺少的库或类似的东西? 感谢

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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