0

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:

Error not finding the agent class

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

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

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).
Just_Alex
  • 518
  • 1
  • 4
  • 16
  • 1
    You are asking how to call ur agents from a subclass, I'm asking how to execute them remotely with a compiler inside another program. However, passing arguments into agents like I did is simple: u write ur Jade booting class the way I did, and then into the agent class (the one that extends agent, u add the following: Object[] args = getArguments(); and then if (args != null) { parse the arguments that u need into the agent variables and Voila } Hope it helps – ALEXANDRA LutinNoir Jul 04 '20 at 16:57