简体   繁体   中英

Compile and run Jade agents using JavaCompiler and URLClassLoader

I can't find a way to execute my agent JADE class by calling a JavaCompiler manually. I'm trying to implement a simulator, and I added an import feature that allows people to load their JADE agent classes and I would execute them on the simulator to extract data.

Here is the import code:

            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

            int result = compiler.run(null, null, null, importedClassPath);
            System.out.println("résultat de la compilation Agent JADE : " + result);

            int result2 = compiler.run(null, null, null,
                    "C:\\Users\\Administrateur\\Desktop\\RoboticsWorkspace\\RuntimeProject\\src\\AcompileJadeMain.java");

            System.out.println("résultat de la compilation MAIN : " + result2);

            File classesDir = new File(
                    "C:\\Users\\Administrateur\\Desktop\\RoboticsWorkspace\\RuntimeProject\\bin\\");
            File classesDir2 = new File(
                    "C:\\Users\\Administrateur\\Desktop\\RoboticsWorkspace\\RuntimeProject\\bin\\");

            URLClassLoader classLoader, classloader2;

            try
            {
                // Loading the class
                classLoader = URLClassLoader.newInstance(new URL[] { classesDir.toURI().toURL() });
                classloader2 = URLClassLoader.newInstance(new URL[] { classesDir2.toURI().toURL() });

                Class<?> cls, cls2;

                cls = Class.forName(importedClassname, true, classLoader);
                cls2 = Class.forName("AcompileJadeMain", true, classloader2);

                Object instanceAgent = cls.newInstance();
                Object instanceMainAgent = cls2.newInstance();

                Method call;
                try
                {
                    call = cls2.getMethod("main", String[].class);
                    String[] args = new String[0];
                    call.invoke(null, new Object[] { args });

                } catch (NoSuchMethodException | SecurityException | IllegalArgumentException
                        | InvocationTargetException e1)
                {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            } catch (MalformedURLException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InstantiationException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

and here is the main that is being compiled:

String[] commande = new String[3];
String argument="";
argument = argument+"Robocar:ACompilJade(20,25,7,10,Est,5)";
commande[0]="-cp C:\\Users\\Administrateur\\Documents\\Eclipse_BackupProjects\\jade.jar;"
        +"C:\\Users\\Administrateur\\Desktop\\RoboticsWorkspace\\RuntimeProject\\bin ";
commande[1]="jade.boot -agents ";
commande[2]=argument;
jade.Boot.main(commande);

I tried including both the paths for JADE.rar and the paths of the agent class but it is still enable to find the agent class and I get the following error:

找不到代理类时出错

I'm also importing the jade.rar file into my project's build path.

I usually use the following line to launch agents from the class file, in my Java IDE.

bootOptions[n] = "FA:"+Facilitator.class.getCanonicalName();

I'm not sure you can pass arguments this ACompilJade(20,25,7,10,Est,5)"; . But if you can I would be interested to know how.

I usually:

  1. Create a subclass Robocar1 with the properties (20,25,7,10,Est,5) , or
  2. Send the data to this agent once it is launched. (I know this is a pain, that is why I am interested in an easier method).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM