I can't deploy the agent in JADE implemented in Java, any alternatives ?
package package1;
import jade.core.Agent;
public class JadePFE extends Agent {
#Override
protected void setup() {
System.out.println("Hello agent 007");
}
}
I think you mean to start the JADE platform, this is the contents of the my main method which launches the whole thing. Hope it helps
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] args1 = new String[3];
args1[0] = "-gui";
args1[1] = "-agents";
args1[2] = "agentName:package.agentClassName";
jade.Boot.main(args1);
}
}
If I have understand, you want know how deploy an agent (and maybe start the platform) directly from the code.
I show you how:
import jade.core.Runtime;
import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.wrapper.*;
public class Start {
public static void main(String args[]) throws InterruptedException, StaleProxyException {
// Get a hold on JADE runtime
Runtime runTime = Runtime.instance();
// Exit the JVM when there are no more containers around
runTime.setCloseVM(true);
// Create a profile and the main container and start RMA
Profile mainProfile = new ProfileImpl(true);
AgentContainer mainContainer = runTime.createMainContainer(mainProfile);
AgentController rma = mainContainer.createNewAgent("rma", "jade.tools.rma.rma", null);
rma.start();
Thread.sleep(500);
// Create a Sniffer
AgentController sniffer = mainContainer.createNewAgent(
"mySniffer", "jade.tools.sniffer.Sniffer",
new Object[]{"BuyerAgent1;BuyerAgent2;ShipperAgent1;ShipperAgent2"});
sniffer.start();
Thread.sleep(500);
// Create a Introspector
AgentController introspector = mainContainer.createNewAgent(
"myIntrospector", "jade.tools.introspector.Introspector",
null);
introspector.start();
Thread.sleep(500);
// Prepare for create and fire new agents:
Profile anotherProfile;
AgentContainer anotherContainer;
AgentController agent;
/* Create a new profile and a new non-main container, connecting to the
default main container (i.e. on this host, port 1099)
NB. Two containers CAN'T share the same Profile object: create a new one. */
anotherProfile = new ProfileImpl(false);
anotherContainer = runTime.createAgentContainer(anotherProfile);
System.out.println("Starting up a BuyerAgent...");
agent = anotherContainer.createNewAgent("BuyerAgent1", "transfersimulation.BuyerAgent", new Object[0]);
agent.start();
Thread.sleep(900);
anotherProfile = new ProfileImpl(false);
anotherContainer = runTime.createAgentContainer(anotherProfile);
System.out.println("Starting up a BuyerAgent...");
agent = anotherContainer.createNewAgent("BuyerAgent2", "transfersimulation.BuyerAgent", new Object[0]);
agent.start();
Thread.sleep(900);
anotherProfile = new ProfileImpl(false);
anotherContainer = runTime.createAgentContainer(anotherProfile);
System.out.println("Starting up a ShipperAgent...");
agent = anotherContainer.createNewAgent("ShipperAgent1", "transfersimulation.ShipperAgent", new Object[0]);
agent.start();
Thread.sleep(900);
return;
}
}
This works if no other JADE Platform is already running.
Related
public class TestResourceBundle {
private static final Path frZoo = Paths.get("./src/Zoo_fr.properties");
private static final Path enZoo = Paths.get("./src/Zoo_en.properties");
private static void createFiles() {
try {
Files.createFile(frZoo);
Files.createFile(enZoo);
try (BufferedWriter enWriter = Files.newBufferedWriter(enZoo);
BufferedWriter frWriter = Files.newBufferedWriter(frZoo);) {
enWriter.write("hello=Hello\nopen=The zoo is open");
frWriter.write("hello=Bonjour\nopen=Le zoo est ouvert");
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void createBundle() {
Locale us = new Locale("en", "US");
Locale france = new Locale("fr", "FR");
ResourceBundle usBundle = ResourceBundle.getBundle("Zoo", us);
ResourceBundle frBundle = ResourceBundle.getBundle("Zoo", france);
System.out.println(usBundle.getString("hello"));
System.out.println(frBundle.getString("hello"));
}
}
In the main function, if I run the following, it will throw java.util.MissingResourceException
public static void main(String[] args) {
createFiles();
createBundle();
}
but if I run these two functions separately (in two programs), it works and does not have any problem.
First run
public static void main(String[] args) {
createFiles();
// createBundle();
}
then run the following, in this case, it works
public static void main(String[] args) {
// createFiles();
createBundle();
}
I don't know why, please help
The problem is that you are trying to load a bundle that is not present in the classpath the application knows about.
When you call ResourceBundle.getBundle it will try to load the resource bundle from the application classpath. But the application classpath was already defined at the application startup, so your brand new files are not listed there.
Two options I can think of: Load the bundle from a file input stream or, define your own classloader to load the files.
1. Load the bundle from a File Input Stream
Create a new PropertyResourceBundle from a FileInputStream that loads each file directly.
Warning: Stream closing and exception handling omitted for brevity.
FileInputStream enFileStream = new FileInputStream("./src/Zoo_en.properties");
FileInputStream frFileStream = new FileInputStream("./src/Zoo_fr.properties");
ResourceBundle usBundle = new PropertyResourceBundle(enFileStream);
ResourceBundle frBundle = new PropertyResourceBundle(frFileStream);
2. Create a URL ClassLoader to load the new files
This is a more scalable approach. Create a new URLClassLoader and use that class loader as an argument for getBundle.
Warning: Stream closing and exception handling omitted for brevity.
File bundleRootPath = new File("./src");
URL[] urls = new URL[]{bundleRootPath.toURI().toURL()};
ClassLoader classLoader = new URLClassLoader(urls);
ResourceBundle usBundle = ResourceBundle.getBundle("Zoo", us, classLoader);
ResourceBundle frBundle = ResourceBundle.getBundle("Zoo", france, classLoader);
Hope that helps.
I'm trying to import PAC file from URL and change Wifi proxy settings programmatically. I searched and found that it`s possible with:
ProxyInfo.buildPacProxy(Uri.parse("someurl")
Before asking this question I checked here and also all of this. The problem that I face is when I implement some of these solutions everything compiles well without exceptions, but when I check there are no proxy settings updated.
This is my last code, but once again without success:
public void setWifiProxySettings5()
{
//get the current wifi configuration
WifiManager manager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config = GetCurrentWifiConfiguration(manager);
if(null == config)
return;
try
{
//linkProperties is no longer in WifiConfiguration
Class proxyInfoClass = Class.forName("android.net.ProxyInfo");
Class[] setHttpProxyParams = new Class[1];
setHttpProxyParams[0] = proxyInfoClass;
Class wifiConfigClass = Class.forName("android.net.wifi.WifiConfiguration");
Method setHttpProxy = wifiConfigClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams);
setHttpProxy.setAccessible(true);
Class proxySettingsClass = Class.forName("android.net.IpConfiguration$ProxySettings");
Class[] setProxySettingsParams = new Class[1];
setProxySettingsParams[0] = proxySettingsClass;
Method setProxySettings = wifiConfigClass.getDeclaredMethod("setProxySettings", setProxySettingsParams);
setProxySettings.setAccessible(true);
ProxyInfo pacInfo = ProxyInfo.buildPacProxy(Uri.parse("http://localhost/pac"));
//pass the new object to setHttpProxy
Object[] params_SetHttpProxy = new Object[1];
params_SetHttpProxy[0] = pacInfo;
setHttpProxy.invoke(config, params_SetHttpProxy);
//pass the enum to setProxySettings
Object[] params_setProxySettings = new Object[1];
params_setProxySettings[0] = Enum.valueOf((Class<Enum>) proxySettingsClass, "STATIC");
setProxySettings.invoke(config, params_setProxySettings);
//save the settings
manager.updateNetwork(config);
manager.disconnect();
manager.reconnect();
}
catch(Exception e)
{
Log.v("wifiProxy", e.toString());
}
}
i want to do groovy file call in java
my source:
ex.groovy
def swingBuilder = new SwingBuilder()
swingBuilder.edt {
frame(title: 'ex', size: [200, 150], show: true) {
borderLayout(vgap: 5)
panel(constraints: BorderLayout.CENTER, border: emptyBorder(10)) {
button "a"
button "b"
}
}
}
ex1.java
class ex1{
public static void main(String[] args)throws Exception {
File sourceFile = new File("mypath/ex.groovy");
ClassLoader clo = jview.class.getClassLoader();
GroovyClassLoader classLoader = new GroovyClassLoader(clo);
Class groovy = classLoader.parseClass(sourceFile);
GroovyObject groovyob = (GroovyObject)groovy.newInstance();
groovyob.invokeMethod("run", null);
}
}
how do i?
help please..
You could use GroovyShell (if required with Binding)
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
public class ex1 {
public static void main(String[] args) {
Binding binding = new Binding();
GroovyShell shell = new GroovyShell(binding);
shell.evaluate("mypath/ex.groovy");
}
I need develop a "launcher" for a java application. This launcher need be able to:
check if main application is up-to-date;
if not, download a update;
run this application and self terminate.
Something like this:
public final class Launcher {
public static void main(String[] args) throws Exception {
String jarName = args[0];
if (jarHasUpdate(jarName)
refreshJar(jarName);
executeJar(jarName);
}
}
What better way of develop the step 3?
I'm trying 2 distinct ways:
1- Run another instance of Java
With the code:
Runtime.getRuntime().exec("java -jar mainApp.jar");
Problem: the launcher still running until mainApp have finished.
2- Using ClassLoader to load .jar at runtime
Like this:
public final class Launcher {
#SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("Invalid number of arguments.");
System.exit(1);
}
Refresh.refreshFile(args[0]);
// args[0] .jar name
// args[1] class with main function
File file = new File(args[0]);
try (URLClassLoader cl = new URLClassLoader(new URL[] { file.toURI().toURL() });) {
Class cls = cl.loadClass(args[1]);
Method method = cls.getDeclaredMethod("main", new Class[] { String[].class });
Object params = new String[] {};
method.invoke(cls, params);
}
}
}
Problem: if "mainApp.jar" has dependencies, this isn't loaded.
Does anyone know how to clear the cache on a new start of a test while running SafariDriver? I've tried to use java robot to keypress command + option + e, but that does not seem to work. It does not focus on the browser.
Robot r = new Robot();
try {
Robot robot = new Robot();
r.keyPress(KeyEvent.META_MASK);
r.keyPress(KeyEvent.VK_META);
r.keyPress(KeyEvent.VK_E);
r.keyRelease(KeyEvent.VK_E);
r.keyRelease(KeyEvent.VK_META);
r.keyRelease(KeyEvent.META_MASK);
} catch (AWTException e) {
e.printStackTrace();
}
Ive also tried to do an actions.builder method but that does not seem to work
String clearCache = Keys.chord(Keys.CONTROL, Keys.COMMAND, "E");
Actions builder = new Actions(browser);
builder.sendKeys(clearCache);
Action clearCacheAction = builder.build();
clearCacheAction.perform();
I've also looked into using SafariDriver options but my java is not that good to fully understand how to implement it. Below is the code that Ive been trying to use. I created a SafariOptions Class and tried to instantiate it in my #before class.
package test
import org.openqa.selenium.safari.SafariDriver;
public class SafariOptions extends SafariDriver {
private static SafariOptions ourInstance = new SafariOptions();
public static SafariOptions getInstance() {
return ourInstance;
}
public void setUseCleanSession(boolean useCleanSession){
}
public SafariOptions() {
boolean useCleanSession = true;
}
}
#Before
public void createDriver() {
assumeTrue(isSupportedPlatform());
browser = new SafariDriver();
SafariDriver options = new SafariOptions();
}
Nothing seems to clear the Safari cache on each test run.
Quick and easy solution for all who might want to know.
Add the following code to a .sh file to root.
killall cookied
rm -rf ~/Library/Caches/com.apple.Safari/*
rm -rf ~/Library/Safari/LocalStorage/*
rm -rf ~/Library/Cookies/*
Call on the file in #Before
Runtime runtime = Runtime.getRuntime();
runtime.exec("file.sh");
System.out.println("Cookies Removed");