-2

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.

NewCastle79
  • 73
  • 10
  • Is your issue building/compiling or running your code? – Mark Elliot May 15 '12 at 00:33
  • my problem is error: java.lang.reflect.InvocationTargetException – NewCastle79 May 15 '12 at 00:49
  • If this is your second day programming, then there's a good chance that you shouldn't be doing any of this. Just add the xstream jar to your classpath when compiling and running. – bmargulies May 15 '12 at 00:53
  • I can add a xstream jar in eclipse project, the class file is loaded in the server and this file can find the xtream reference, due an this class file is loaded by another application. for this, I cannot add the jar file in the eclipse project using "add external jar" I repeat my file is loaded by other program and run this. – NewCastle79 May 15 '12 at 01:16

1 Answers1

4

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 Class of 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 that Class, 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?

Community
  • 1
  • 1
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • I am trying to use its. but I have some problems this is my second day programming in java please done downvote! – NewCastle79 May 15 '12 at 00:43
  • @NewCastle79 What problems are you having? – Jeffrey May 15 '12 at 00:46
  • my problem is error: java.lang.reflect.InvocationTargetException – NewCastle79 May 15 '12 at 00:47
  • Jeffrey excuse me I dont understand the concept "on you classpath", this mean classpath on windows path? how I can work with this classpath? – NewCastle79 May 15 '12 at 01:20
  • When you execute the java (and javac) command, you have the option to specify more locations to find class files, ie `java -classpath C:\cubrid\bin\xstraem-1.4.2.jar foo.bar.YourMainClass` would allow you to import classes directly from this jar file. Most IDEs have an "include external library" option which handles this for you. – Jeffrey May 15 '12 at 01:25
  • the problem is my class dont have a main class – NewCastle79 May 15 '12 at 03:31