0

I created a Java program, which reads a file, figures out which operation to perform ( create an object, call a class' method) using the Reflection API in Java. for example:

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

if I find the new keyword, then I try to create a new object of that class. Otherwise, I will call the method (in the example, withValue()) with the argument -0.5. The source code is shown below:

//
// 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 );

    }

}

However, I'm getting the following error:

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)

I loaded the .jar file which contained the class binaries. It's my first time doing this kind of task in Java, so I'm not very familiar.... could someone please help me? Is it a missing library or something like? THanks

1
  • 1
    Loading a jar to your IDE project does not always load or set it to the classpath. In this case it seems that one of your objects have a different dependency of some AspectJ class. Try to find the jar file missed using this site: findjar.com/index.x Commented Aug 27, 2012 at 23:46

2 Answers 2

2

You need to set in your classpath all the libraries for the classes you are going to call and for all the dependencies of those classes. So, in this case, it seems that org.powertac.common.Competition has a dependency on org.aspectj.lang.Signature.

Sign up to request clarification or add additional context in comments.

Comments

1

This class org.aspectj.lang.Signature is part of the Aspectj Runtime Library . If you're using Maven you can add:

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

to your POM.xml to retrieve the missing JAR files.

or directly:

aspectjrt-1.5.4.jar and also aspectjweaver-1.5.4.jar and log4j

3 Comments

But I don't have a POM.xml file ... how could I solve those dependencies?
I've added 2 direct links. Download & add to your classpath.
one more to go: org/apache/log4j/Logger

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.