Load java class
First create a package test.loading. Create an interface Loaded and put it in the created package:
package test.loading;
public interface Loaded {
public void print();
}
Second step is to create class which implement the Loaded interface
package test.loading;
public class LoadingClass implements Loaded{
@Override
public void print() {
System.out.println("Implemented");
}
}
Create a strategy which loads the created class LoadingClass from the test.loading package. The test.loading must be located in the C:\temp directory.
package dukascopy.strategies.clients;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Period;
import com.dukascopy.api.RequiresFullAccess;
@RequiresFullAccess
public class LoadClassExample implements IStrategy {
private IConsole console;
public void onAccount(IAccount arg0) throws JFException {
}
public void onBar(Instrument arg0, Period arg1, IBar arg2, IBar arg3) throws JFException {
}
public void onMessage(IMessage arg0) throws JFException {
}
The method onStart load the class Loaded using the URLClassLoader class from the directory specified in the file variable:
public void onStart(IContext context) throws JFException {
this.console = context.getConsole();
try{
File file = new File("c:\\temp\\");
// Convert File to a URL
URL url = file.toURL();
URL[] urls = new URL[]{url};
// Create a new class loader with the directory
ClassLoader cl = new URLClassLoader(urls);
// The class Loaded.class should be located in the directory file:/c:/temp/test/loading
console.getOut().println("Before");
Class cls = cl.loadClass("test.loading.Loaded");
console.getOut().println("Loaded: " + cls);
} catch (Exception e) {
e.printStackTrace(context.getConsole().getErr());
}
}
public void onStop() throws JFException {
}
public void onTick(Instrument arg0, ITick arg1) throws JFException {
}
}
The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.