Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Adding jars from file system to runtime classpath
Adding jars from file system to runtime classpath [message #334112] Wed, 21 January 2009 11:08 Go to next message
Sheela is currently offline SheelaFriend
Messages: 52
Registered: July 2009
Member
Hi,
we have developed an IDE using eclipse 3.4 and dltkS1.0M3.
we are using a jar containing xml files at runtime.
For that we include jars in Manifest.MF/runtime classpath of the plugin,so
that at runtime the xml files will be loaded from the classpath.

Now our requirement is, instead of keeping the jar inside the plugin ,we
want
to keep the jars in a file system and add them to the classpath at
runtime.So that user can place jars into that directory.

How do we achieve it?

Regards,
Sheela
Re: Adding jars from file system to runtime classpath [message #334123 is a reply to message #334112] Wed, 21 January 2009 15:18 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

I know that some products do this by including the plugin, but using
external: in the bundle classpath.

Bundle-ClassPath: .,
external:$flex.sdk$/lib/fdb.jar,
external:$flex.sdk$/lib/swfutils.jar

Then you run your eclipse with a -Dflex.sdk=/path/to/flex/sdk and pick
up the locally installed libs.

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/ganymede/index.jsp?topic=/org.eclips e.platform.doc.isv/guide/workbench.htm


Re: Adding jars from file system to runtime classpath [message #334146 is a reply to message #334112] Thu, 22 January 2009 05:49 Go to previous messageGo to next message
Sheela is currently offline SheelaFriend
Messages: 52
Registered: July 2009
Member
Hi Paul,

We tried your suggestion,its working fine.Thanks for your suggestion.

Now we have a slightly modified requirement ,we dont know the name of our
jars (as the user will provide the jars) and want all the jars in the
path(known external directory)to be picked at runtime.
Is there any way to do this?

We tried using something like "external:$flex.sdk$/*" , but its not working
Is there any other mechanism ?

Regards,
Sheela
Re: Adding jars from file system to runtime classpath [message #334164 is a reply to message #334146] Thu, 22 January 2009 19:07 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

There's no way (that I know of) to do that, outside of adding them to
the bootclasspath or application classpath.

What's more interesting, why are there are number of jars? A bundle
has to export the packages to be useful.

A bundle is a versioned component that works in the system ... how can
you know bundle 3.4.1 works in your system if it is exporting a random
version of the packages (which have to be predefined anyway)?

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/ganymede/index.jsp?topic=/org.eclips e.platform.doc.isv/guide/workbench.htm


Re: Adding jars from file system to runtime classpath [message #334165 is a reply to message #334164] Thu, 22 January 2009 20:57 Go to previous message
Jan Kohnert is currently offline Jan KohnertFriend
Messages: 196
Registered: July 2009
Senior Member
I've this nasty part of code. Two classed that combined allow you to load
every jar includes in one directory. You have to put your application code
inside the execute() methos of thr AbstractSystemClassLoader. In my case
every jar inside a plugin is getting added. but this can changed easily ...
The abstract class you may used nested:

AbstractSystemClassLoaderExecutable executable = new
AbstractSystemClassLoaderExecutable(){
@Override
protected boolean execute() {
// do thomething (best done using Display.asyncExec())..
}
});

The classes:
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.FileLocator;

import swl.lib.util.ClassPathHacker;

/**
* This class enables executing code in eclipse using the java standard
class loader.
* As default the "lib" directory within the plugin is added to the
classpath.
* @author jko
*
*/
public abstract class AbstractSystemClassLoaderExecutable {

private static boolean CLASSPATH_LOADED = false;
private boolean executionSuccessfull = true;

public AbstractSystemClassLoaderExecutable() {
Thread current = Thread.currentThread();
ClassLoader oldLoader = current.getContextClassLoader();
try {
// Standard classloader setzten.
current.setContextClassLoader(ClassLoader.getSystemClassLoad er());

// Der Klassenpfad wird nur einmal geladen
if (!CLASSPATH_LOADED){
loadClasspath();
}

// Ausführen des Codes
execute();

} catch (IOException e) {
e.printStackTrace();
}finally {
// Eclipse classloader wieder aktivieren.
current.setContextClassLoader(oldLoader);
}
}


private void loadClasspath() throws IOException{
// Pluginlocation.
URL pluginURL =
FileLocator.toFileURL(XslProcessorPlugin.getDefault().getBun dle().getEntry( "/"));
// Alle Datein im lib Verzeichnis auflisten
Enumeration<String> jars =
XslProcessorPlugin.getDefault().getBundle().getEntryPaths("lib ");
// Pattern für "jar" Datein.
Pattern p = Pattern.compile( "(.*\\.jar$)", Pattern.CASE_INSENSITIVE );
while (jars.hasMoreElements()){
String jar = jars.nextElement();
if (p.matcher(jar).matches()){
// Die tatsächliche Position nocheinmal auflösen
String fileString = FileLocator.resolve(new
URL(pluginURL.toExternalForm() + jar)).getFile();
File file = new File(fileString);
// und hinzufügen.
ClassPathHacker.addFile(file);
}
}
CLASSPATH_LOADED = true;
}

abstract protected boolean execute();

public boolean isExecutionSuccessfull() {
return executionSuccessfull;
}
}

You'll need this class as well (its from somewehere in the internet and
overrides the system classloader using java reflection):

import java.lang.reflect.*;
import java.io.*;
import java.net.*;

public class ClassPathHacker {

private static final Class[] parameters = new Class[] { URL.class };

public static void addFile(String s) throws IOException {
File f = new File(s);
addFile(f);
}// end method

public static void addFile(File f) throws IOException {
addURL(f.toURL());
}// end method

public static void addURL(URL u) throws IOException {

URLClassLoader sysloader = (URLClassLoader) ClassLoader
.getSystemClassLoader();
Class sysclass = URLClassLoader.class;

try {
Method method = sysclass.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke(sysloader, new Object[] { u });
} catch (Throwable t) {
t.printStackTrace();
throw new IOException(
"Error, could not add URL to system classloader");
}// end try catch

}// end method

}// end class

take in minde, that this solution may not work with future java versions
since the API used in ClassPathHacker is private and may change.

Have fun.

Jan
Previous Topic:Add control to status bar
Next Topic:pde build dependencies
Goto Forum:
  


Current Time: Sun Dec 22 06:13:05 GMT 2024

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

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

Back to the top