JAVA Hello World ? GUI
Step 3) Save the file for Java Hello World program as FirstProgram.java make sure to select file type as all files while saving the file in our working folder C:\workspace
JAVA Hello World – GUI
The tool jdeps indicates that this class only depends on types in the java.lang and java.io packages, therefore it only needs the module java.base. A similar output would be produced for a JAR file as input. With a list of the required modules, you can now create a Java runtime.
You can now use the Java runtime located at /javaruntime to execute the application code dependent on java.base module. The structure of the javaruntime folder produced by jlink is similar to the JDK directory structure, and the java command line tool to start the JVM, is located in the ./bin/ folder as usual. Given a custom Java runtime contains all the required modules of an existing application, it can be referenced by JAVA_HOME.
In this step we will introduce the usage of the JUnit [3] test framework in combination with Ant. Because Ant has abuilt-in JUnit 4.13.1 you could start directly using it. Write a test classin src\oata\HelloWorldTest.java:
We reuse the path to our own jar file as defined in run-target by giving it an id and making itglobally available. The printsummary=yes lets us see more detailed information than just a "FAILED"or "PASSED" message. How much tests failed? Some errors? printsummary lets us know. The classpath is set upto find our classes. To run tests the batchtest here is used, so you could easily add more test classes inthe future just by naming them *Test.java. This is a common naming scheme.
Let's start with a classic: the simplest JADE agent that can print Hello World . JADE Agents are defined as subclasses of the predefined class Agent and their initial code must be placed in a method called setup. The code for Hello World is shown below. To spice things up, we also have the Agent prints its name - with getLocalName. For such a simple example, we only need to import one class, Agent. import jade.core.Agent; public class HelloAgent extends Agent protected void setup() System.out.println("Hello World. "); System.out.println("My name is "+ getLocalName()); Note: The programs for all the examples in this chapter can be found in the code/ch_2 directory.Here are the commands required to compile and run our agent (in UNIX) . % javac HelloAgent.java % java jade.Boot fred:HelloAgentThe first line is obvious, but the second line requires some explanation. Basically, Agents are a bit like Java Applets in that they can't be executed directly; they must execute within a larger program which provides the necessary services. In the case of Applets, a browser or an Applet viewer is needed; for Jade agents the environment is provided by the class jade.Boot which finds which Agents to run from command line parameters. The parameter, here "fred:HelloAgent", indicates the class of the agent, HelloAgent, and provides it with a unique name (fred). Jade environments are called containers. Typically, in a multi-agent application, there will be several containers (with agents) running on different machines. The first container started must be a main container which maintains a central registry of all the others so that agents can discover and interact with each other.Below we show the output of execution. Jade first prints out a version message and the IOR (CORBA address) of the container before starting the HelloAgent. % javac HelloAgent.java % java jade.Boot fred:HelloAgent This is JADE 3.0b1 - 2003/03/19 17:08:01 downloaded in Open Source, under LGPL restrictions, at IOR:000000000000001149444C3A464950412F4D54533A312E300000.... .... 0020501000100010020000101090000000100010100 Agent container Main-Container@JADE-IMTP://Jeans-Computer.local. is ready. Hello World. My name is fredYou will notice that the system prompt "%" is missing. This is because the Jade container stays active after our agent has finished execution. It is waiting for messages from other containers or the possible arrival of mobile agents. To terminate the program and get back to the system prompt you need to type CTL-C. Note: the Czech tutorial also starts with the Hello World example.More realistic AgentsBy putting our code in the setup method of the HelloAgent, we cheated. Agent actions are normally specified through Behaviour classes. More exactly, the actions are described in the "action" method of these Behaviours. The setup method only serves to create instances of these behaviours and linking them to the Agent object. The skeleton of an typical agent class is shown below: import jade.core.Agent; import jade.core.behaviours.*; public class myAgent extends Agent protected void setup() addBehaviour( new myBehaviour( this ) ); class myBehaviour extends SimpleBehaviour public myBehaviour(Agent a) super(a); public void action() //...this is where the real programming goes !! private boolean finished = false; public boolean done() return finished; // ----------- End myBehaviour //end class myAgentHere, the Behaviour is defined to be a subclass of SimpleBehaviour, the most flexible of jade's predefined Behaviours. It is alsopositioned to be an internal class to the Agent. It could also have been implemented as an anonymous class or defined in a separate file. In all cases, it is usual to pass a reference to the owner agent (this) when creating a Behaviour.A last point is the provision of a mechanism to terminate the behaviour. In Jade, as long as a behaviour is not "done", its action method will be called repeatedly after every event - such as receipt of a message or expiry of a timer delay. The example shows a common technique used to terminate the behaviour: a FINISHED flag which is tested in the "done" method and can be set in "action". Later we will also show how to use the "block" method to indicate that a behaviour has finished handling the current event and make it wait for the next event.Simple0.javaHere is a more typical implementation of the HelloAgent. import jade.core.Agent; import jade.core.behaviours.*; public class Simple0 extends Agent protected void setup() addBehaviour( new B1( this ) ); class B1 extends SimpleBehaviour public B1(Agent a) super(a); public void action() System.out.println( "Hello World! My name is " + myAgent.getLocalName() ); private boolean finished = false; public boolean done() return finished; //End class B1 The example also shows two new things:myAgent: a local variable of all Behaviour objects which stores the agent reference passed as a parameter when the behaviour was created.
The class B1 is specified outside the context of the Agent, but it can use myAgent to access its owner's attributes and methods.
Here are the commands required (in UNIX) to compile and run our agent.% javac Simple0.java% java jade.Boot fred:Simple0Below we show the output of execution. Jade first prints out a version message and the IOR (CORBA address) of the container before starting the Simple0 agent. The behaviour isn't quite what we had hoped for, in that the agent loops and keeps on printing its message until we abort execution. jean% java jade.Boot fred:Simple0 This is JADE 3.0b1 - 2003/03/19 17:08:01 downloaded in Open Source, under LGPL restrictions, at IOR:000000000000001149444C3A464950412F4D54533A312E300000.... .... 0020501000100010020000101090000000100010100 Hello World! My name is fred Hello World! My name is fred Hello World! My name is fred Hello World! My name is fred ..... loops until stopped with CTL-C.... !The problem here is that we haven't explicitely indicated that the action was "done".Simple1.javaHere is our third try at the "Hello World" example. We add a local variable, "n", to the behaviour to terminate its operation after "action" has been called 3 times. /***************************************************************** Simple1.java: Minimal agent with anonymous behaviour *****************************************************************/ import jade.core.Agent; import jade.core.behaviours.*; public class Simple1 extends Agent protected void setup() addBehaviour( // -------- Anonymous SimpleBehaviour new SimpleBehaviour( this ) int n=0; public void action() System.out.println( "Hello World! My name is " + myAgent.getLocalName() ); n++; public boolean done() return n>=3; ); // --- setup --- // --- class Simple1This time the behaviour is implemented as an anonymous sub-class of SimpleBehaviour. The output is shown below: jean% java jade.Boot x:Simple1 This is JADE 3.0b1 - 2003/03/19 17:08:01 downloaded in Open Source, under LGPL restrictions, at IOR:000000000000001149444C3A46...0010100 Hello World! My name is x Agent container Main-Container@JADE-IMTP://Jeans-Computer.local. is ready. Hello World! My name is x Hello World! My name is xIn this case, the behaviour end properly but the program doesn't TERMINATE even though there is no more activity after the three lines are printed. This is because 1) the agent still exists and 2) the JADE environment which we started stays around to interact with other agents which could be started later in the network. This is more obvious if we use the GUI option.JADE OptionsWhen starting a Jade container, we can specify many options. In particular, the "-gui" option causes Jade to create a special "Remote Management Agent" with a graphical interface which shows all participating agents and containers. It can also be used to kill agents, start new ones or to trace messages sent between agents (with the "sniffer" tool). Here is the command to invoke the interface with our Simple1 example:jean% java jade.Boot -gui x:Simple1and here is the resulting window [ Note: it takes some time to appear and may show up after our simple agent has finished ].