Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Classloader not work on RCP
Classloader not work on RCP [message #520753] Mon, 15 March 2010 08:09 Go to next message
Terris  is currently offline Terris Friend
Messages: 4
Registered: March 2010
Junior Member
The following code work on standalone java but not work on RCP
ClassLoader loader = new ClassLoader() {
	@Override
	protected Class<?> findClass(String name) throws ClassNotFoundException {
		InputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(filename));
			ByteArrayOutputStream byteStr = new ByteArrayOutputStream();
			int byt = -1;
			while ((byt = in.read()) != -1) {
				byteStr.write(byt);
			}
			byte byteArr[] = byteStr.toByteArray();
			return defineClass(null, byteArr, 0, byteArr.length);
		} catch (final RuntimeException rex) {
			throw rex;
		} catch (final Exception ex) {
			ex.printStackTrace();
			throw new ClassNotFoundException(name);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (final IOException ioe) {
					ioe.printStackTrace();
				}
			}
		}
	}
};
loader.loadClass("garbage").newInstance();

Is there anyone find the problems?

Thanks
Terris
Re: Classloader not work on RCP [message #520920 is a reply to message #520753] Mon, 15 March 2010 14:18 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Terris wrote:
> The following code work on standalone java but not work on RCP

Classloading in RCP (based on OSGi) is simple or complicated, depending
on your point of view.

Simple: Bundles can only see other bundles. If you want to load a
class, get it from a bundle.

Complicated: What you are trying to do works against the modularity
imposed by OSGi.

But OSGi and plugins come with a number of methods to work in a modular
environment.

What are you trying to do? i.e. what usecase are you trying to implement?

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
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: Classloader not work on RCP [message #520977 is a reply to message #520920] Tue, 16 March 2010 02:11 Go to previous messageGo to next message
Terris  is currently offline Terris Friend
Messages: 4
Registered: March 2010
Junior Member
Sorry, I am not familiar with java. Do you mean bundle is something like:
Bundle bundle = Activator.getDefault().getBundle();
bundle.loadClass(filename).newInstance();

It works but I can't reload it after the class is edited and recompiled.

I am trying to write a program that can run a customized java program developed by user.

Thanks
Terris
Re: Classloader not work on RCP [message #521090 is a reply to message #520977] Tue, 16 March 2010 12:32 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

For writing and compiling java JDT has a number of extensible tools, but
to load the classes they compile they usually launch a new JVM.

Is it that you want to compile some java and then load it into your
running RCP app? Are you trying to reload a library? Do you have a lot
of user defined classpath dependencies for their file?

Or you simply want to use Java as a scripting language, to manipulate a
small set of APIs that you provide?

You can install and uninstall bundles on the fly using the OSGi APIs,
but depending on what you are doing that might be too heavy weight.

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
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: Classloader not work on RCP [message #521278 is a reply to message #521090] Wed, 17 March 2010 02:52 Go to previous messageGo to next message
Terris  is currently offline Terris Friend
Messages: 4
Registered: March 2010
Junior Member
Yes, I want to use java as a scripting language. It may be good if I can pass an object to the customized program and it return a value to my main program.

Thanks.
Terris

[Updated on: Wed, 17 March 2010 02:52]

Report message to a moderator

Re: Classloader not work on RCP [message #521388 is a reply to message #521278] Wed, 17 March 2010 08:42 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Presuming you can compile the Java script correctly, you could do one of
2 things:

1) you can simply compile it in a bundle and load/unload the bundle
through OSGi. A bundle can be a directory on the disk, and can be as
simple as a META-INF/MANIFEST.MF and bin/com/example/MyScript.class.
We've used this trick before to be able to create a normal plugin
project but treat it like a script project (it is continually unloaded
and re-loaded into the current eclipse).

2) If you have almost no variables in your script classpath (i.e. it
depends on classes your plugin exports) you can still try the
URLClassLoader ... just make its parent a class from your bundle. Then
it will be loaded as if it was part of your bundle.

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
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:Limiting dimensions of view folders / views
Next Topic:Question about WorkbenchWindows and Perspectives
Goto Forum:
  


Current Time: Tue Apr 23 16:46:00 GMT 2024

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

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

Back to the top