This is my first java program, so please excuse me if its too naive.
I have a 3rd party jar. I want to instantiate a class in the jar and be able to use its methods. Some details about the class in the jar:
Class File: rediff.inecom.catalog.product.CSVAPI Constructor: CSVAPI() Method: UpdateCSVAPI(key, csvpath) Return: String I have written the following program:
import java.io.File; import java.net.URL; import java.net.URLClassLoader; import java.io.IOException; class MyLoaderClass{ public void myLoaderFunction(){ File file = new File("vendorcatalogapi.jar"); try { URL url = file.toURI().toURL(); URL[] urls = new URL[]{url}; ClassLoader cl = new URLClassLoader(urls); Class cls = cl.loadClass("rediff.inecom.catalog.product.CSVAPI"); Object cls_object = cls.newInstance(); System.out.println(cls_object); String output = cls_object.UpdateCSVAPI(12345,"myfile.csv"); System.out.println(output); System.out.println("try"); } catch (Exception e) { System.out.println("catch"); e.printStackTrace(); } } public static void main(String args[]){ new MyLoaderClass().myLoaderFunction(); } } I am trying to compile it using:
javac -cp vendorcatalogapi.jar temp.java But I am getting the following error:
temp.java:17: error: cannot find symbol String output = cls_object.UpdateCSVAPI(12345,"myfile.csv"); ^ symbol: method UpdateCSVAPI(int,String) location: variable cls_object of type Object 1 error Looks like the object is not correctly initialized. Please can someone help me with the correct way of doing it
new CSVAPI()and includingvendorcatalogapi.jarin the classpath?? I don't think fiddling around with classloading is fit for a first java-program...