Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Dynamically load class at runtime(Class.forName("xy") not possible?)
Dynamically load class at runtime [message #659133] Fri, 11 March 2011 08:12 Go to next message
Alexander Mack is currently offline Alexander Mack
Messages: 100
Registered: July 2009
Senior Member
Hello,
I am working on an eclipse plugin and I need to load a directory into the classpath at runtime. It's only possible at runtime!
I tried to do the following:
public static void addDirectoryToClassPath(String pDirectory) throws Exception {
		File lDirectoryToAdd = new File(pDirectory);
		if(!lDirectoryToAdd.exists() || !lDirectoryToAdd.isDirectory()) {
			throw new Exception(pDirectory + " doesn't exist or isn't a directory!");
		} else {
			Thread lCurrentThread = Thread.currentThread();
			ClassLoader lOldClassLoader = lCurrentThread.getContextClassLoader();
			URL[] lDirectoryURL = new URL[] {lDirectoryToAdd.toURI().toURL()};
			URLClassLoader lNewClassLoader = new URLClassLoader(lDirectoryURL, lOldClassLoader);
			lCurrentThread.setContextClassLoader(lNewClassLoader);
		}
	}


But if I try to load the class later via
Class.forName("classname);

I get a CLassNotFoundException.

For Information: I will load the class in another eclipse plugin, but that's not the problem at the moment because even in the same plugin Class.forName("") doesn't work...

I would appreciate any help

Thanks in advance
Alex
Re: Dynamically load class at runtime [message #659156 is a reply to message #659133] Fri, 11 March 2011 10:11 Go to previous messageGo to next message
Mickael Istria is currently offline Mickael Istria
Messages: 755
Registered: July 2009
Location: Grenoble, France
Senior Member
Hi,

Dealing with contextClassLoader in OSGi is quite difficult!
You'd rather keep a reference to your classloader and use lNewClassLoader.loadClass("") rather than Class.forName("");

HTH

--
Mickael Istria -- BonitaSoft S.A.
http://www.bonitasoft.com/products/BPM_download.php
Re: Dynamically load class at runtime [message #659157 is a reply to message #659133] Fri, 11 March 2011 10:20 Go to previous messageGo to next message
Alexander Mack is currently offline Alexander Mack
Messages: 100
Registered: July 2009
Senior Member
Hello and first of all thanks for your answer!

That's not as easy as it seems because the Class.forName statment is not within the same plugin.

Plugin A should add some directories to the classpath whilst plugin B is loading those classes.
Changing the functionality of Plugin B from Class.forName() to your suggestion isn't programmed properly, is it?

No other way than changing plugin B?
Re: Dynamically load class at runtime [message #659198 is a reply to message #659133] Fri, 11 March 2011 13:04 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

You cannot do it the way you describe. Eclipse plugins are OSGi
bundles. That means that OSGi provides a framework classpath with
almost nothing, and the framework wires up the classes a bundles can
see, based on the MANIFEST.MF Require-Bundle or Import-Package

To dynamically load classes, you'll have to do some hand waving.

Are these JDBC drivers? There are probably examples out there of install
JDBC drivers into an RCP app, maybe http://www.eclipse.org/datatools has
some tips.

But in general, you need to use a bundle to load your classes into OSGi.
example:

1) if you are creating a directory with classes/jars at runtime, make it
a bundle by including a META-INF/MANIFEST.MF. Then use your
BundleContext to install it.

2) if it's a known library that will be installed on the client system
(you just can't know where) you can ship a bundle with a MANIFEST.MF
that uses the external: $INTERESTING_DIR$/lib/whatever.jar directive in
its Bundle-ClassPath. When installing on the client system, you can
simple add -DINTERESTING_DIR to the .ini file and it will pick up the
installed lib.

There are also runtime options that would allow you to add to the system
classpath or boot classpath, but they require restarting to update.

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: Dynamically load class at runtime [message #659464 is a reply to message #659198] Mon, 14 March 2011 07:34 Go to previous messageGo to next message
Alexander Mack is currently offline Alexander Mack
Messages: 100
Registered: July 2009
Senior Member
Hello Paul and thanks for your answer first!

No, its not for JDBC support. I programmed a debugger based on a 4th Generation Language and java.

Plugin A is the builder which should set up the classpath and call the compiler if necessary.
Plugin B is the compiler and should load classes dynamically via the classpath set up by Plugin A.

Your suggestion to solve the problem via the .ini file is quite difficult, because the classpath is dynamic. I can add libraries during runtime and some other things.

I'm just able to get the whole classpath right before Plugin A calls Plugin B. Not earlier,

Kind regards
Alex
Re: Dynamically load class at runtime [message #659519 is a reply to message #659464] Mon, 14 March 2011 11:56 Go to previous message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

On 03/14/2011 03:34 AM, Alexander Mack wrote:
> Plugin A is the builder which should set up the classpath and call the
> compiler if necessary.
> Plugin B is the compiler and should load classes dynamically via the
> classpath set up by Plugin A.

JDT doesn't actually load the java source that it's working with or
compiling into its own JVM. It creates an AST and works with that.

Since this isn't really applicable to your running environment, why
wouldn't you just create a URLClassLoader and pass that to your compiler
plugin?

> Your suggestion to solve the problem via the .ini file is quite
> difficult, because the classpath is dynamic. I can add libraries during
> runtime and some other things.

Yes, restarting is useful for a install time problem, not dynamically
changing classpaths ... OSGi is useful for that, but I'm not sure you're
playing in the right space.

>
> I'm just able to get the whole classpath right before Plugin A calls
> Plugin B. Not earlier,

Why does your plugin need something in its classpath? I'm pretty sure
that other tools work without loading their stuff into their own JVM.

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Previous Topic:Re: Main menu drop down event and mouse hover on Main menus.
Next Topic:building eclipse with javac
Goto Forum:
  


Current Time: Thu Sep 18 07:46:59 GMT 2014

Powered by FUDForum. Page generated in 0.01991 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software