1

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

3
  • Object cls_object = cls.newInstance(); --> How do Object class knows methods of CSVAPI, Just cast to that specific class... Commented Aug 21, 2014 at 13:38
  • 1
    Whoa, why are you doing all this reflection-stuff instead of just using new CSVAPI() and including vendorcatalogapi.jar in the classpath?? I don't think fiddling around with classloading is fit for a first java-program... Commented Aug 21, 2014 at 13:44
  • @piet.t : didn't know that a simpler way exists! Commented Aug 21, 2014 at 13:56

3 Answers 3

2

If this is your first java program, then loading the class dynamically is probably overkill. Just use it normally and let the default class loader load it:

import java.io.File; import java.net.URL; import java.net.URLClassLoader; import java.io.IOException; import rediff.inecom.catalog.product.CSVAPI; class MyFirstClass{ public void myFunction() { CSVAPI cvsapi = new CSVAPI(); System.out.println(cvsapi); String output = cvsapi.UpdateCSVAPI(12345,"myfile.csv"); System.out.println(output); System.out.println("Success!"); } public static void main(String args[]){ new MyFirstClass().myFunction(); } } 

Compile (note that the source code file name must match the class name):

javac -cp vendorcatalogapi.jar MyFirstClass.java 

Run:

java -cp .:vendorcatalogapi.jar MyFirstClass (on Unix based) java -cp .;vendorcatalogapi.jar MyFirstClass (on Windows) 
Sign up to request clarification or add additional context in comments.

3 Comments

+1: that's the way to do it in a first java-program!
I am getting error: error: cannot find symbol symbol: class CSVAPI
Ok, solved by import rediff.inecom.catalog.product.CSVAPI;. Thanks for your answer
2

You have to let the compiler know that cls_object is an instance of CSVAPI. If you don't, you can only use the object methods (toString, equals, etc.).

To do this, you can do the following:

 rediff.inecom.catalog.product.CSVAPI cls_object = (rediff.inecom.catalog.product.CSVAPI) cls.newInstance(); 

Please, note that you need to have CSVAPI in your classpath!

Comments

1

Object class doesnt know the methods of rediff.inecom.catalog.product.CSVAPI class.

 Class cls = cl.loadClass("rediff.inecom.catalog.product.CSVAPI"); Object cls_object = cls.newInstance(); 

So, explicit casting is required

rediff.inecom.catalog.product.CSVAPI object = (rediff.inecom.catalog.product.CSVAPI) cls.newInstance(); 

will do the job.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.