1

I'll try to phrase this as best I can. I have a program which has an API-like functionality - it uses reflection to dynamically call methods from within a class. In this instance:

Server.java

public static void main(String[] args) { Class<?> clazz = Class.forName("DiHandler"); StHandler out = (StHandler) clazz.newInstance(); out.read(); } 

DiHandler.java

// implements StHandler import edu.ds.*; public void read() { Ds aType = new Ds(); aType = "134"; } 

So DiHandler has a method read() which can contain anything, it doesn't matter to Server.java after compile time.

My problem is: DiHandler.java uses the class Ds from a JAR file. When I'm working on DiHandler.java in Eclipse (in a separate project from the project Server.java is in) I can add this JAR without a problem. But when I move DiHandler.class, after it's compiled, to be used by Server.class, how can it still use the JAR file?

I hope this makes some sense, I suppose another way to phrase it would be how can I allow DiHandler to call on a class from the JAR without editing the classpath?

Thanks very much in advance and sorry for any confusion or poor phrasing, I can only offer thanks and the customary offer of a pint for any assistance.

M

3
  • 1
    STHandler out = (StHandler) clazz; - I hope that's not actually how you're trying to instantiate an StHandler. Commented Jan 10, 2011 at 14:58
  • you could specify the additional class path in either the command line or the jar manifest file Commented Jan 10, 2011 at 15:02
  • Apologies, I missed out a part, that should be corrected now. :) Commented Jan 10, 2011 at 15:04

2 Answers 2

2

One way or another, you need to let the ClassLoader that loads DiHandler know how to load your Ds class. The easiest way is through the classpath.

The other way is to provide your own ClassLoader which you use to load DiHandler, with that ClassLoader knowing how to load Ds. This could be a subclass of URLClassLoader that allows you add URLs to JAR files on the fly. What it will look like in practice I can't say since I don't know the details of your context.

Sign up to request clarification or add additional context in comments.

2 Comments

my answer wasn't answering the entire question, but was fixing something that obviously didn't work ;) perhaps it was better to be a comment though
Ah...that's interesting... Adding a JAR on the fly is pretty much what I need to do here I beleive. What I have done is allow other engineers to write their own DiHandler implementing StHandler. What I am trying to do is write my own DiHandler, i.e. as if it were written by someone else, but my DiHandler in this case calls upon a class within a JAR. I need to allow users to do this without access to the classpath of the main application. I hope this helps put this into more context. :) Thanks very much for the advice, I will have a look at URLClassLoader and see what I come up with.
0

You can only use a class if the JVM has access to it. This means that class has to be in JVM's classpath.

There are a number of ways to place a jar in the classpath of a program. Perhaps, if you describe the environment your working in and the limitations your facing, someone will be able to help you find the right mechanism for placing the dependent jar on the classpath.

1 Comment

It doesn't actually mean the class has to be in the JVM's classpath at all. There are alternatives.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.