I have a java class and I want to Load a class inside a external jar.
The jar is xstream-1.4.2.jar the class is com.thoughtworks.xstream.XStream
I search in google but I cant insert an external reference to my project (jar)
this is my code:
File file = new File("c:\\cubrid\\bin\\xstream-1.4.2.jar");
URL url = file.toURL();
URL[] urls = new URL[]{url};
ClassLoader cl = new URLClassLoader(urls);
Class cls = cl.loadClass("com.thoughtworks.xstream.XStream");
cadena = cls.toXML(objeto);
I dont know how to call a method, I read some documents in google but sorry is my second day programming on Java.
I want to generate a .class library. This is loaded for another application. for this I need to use some method to load the jar externally.
this class file will be loaded as java stored procedure in a CUBRID Database server.
my problem is error: java.lang.reflect.InvocationTargetException
my code
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.xml.parsers.DocumentBuilderFactory;
public class jp2 {
static ResultSet resultado = null;
static String cadena = null;
static Statement statement = null;
static DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
public static String Validar(String tblUsuariosLogin,String tblUsuariosPassword)
{
try {
Class.forName("cubrid.jdbc.driver.CUBRIDDriver");
Connection con = DriverManager.getConnection("jdbc:default:connection:");
File file = new File("c:\\cubrid\\bin\\xstream-1.4.2.jar");
URL url = file.toURL();
URL[] urls = new URL[]{url};
ClassLoader cl = new URLClassLoader(urls);
Class cls = cl.loadClass("com.thoughtworks.xstream.XStream");
// XStream xstream = new XStream();
cadena = cls.toXML(objeto);
return cadena;
} catch (Exception e) {
// TODO: handle exception
}
return cadena;
}
}
CUBRID database allow you load .class files and use it at stored procedures.
Take a look at
URLClassLoader, it allows you to load classes from an external JAR file./edit
Your problem is that you are trying to call a method on the
Classof an object, not the object itself. If you want to call the method, you need to create a new instance (see the second method provided) of thatClass, and call the method on the result.Are you sure you don’t just want to have that JAR file on your classpath and deal with the classes directly instead of using reflection to load and instantiate the classes as you need them?