Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » loading a class or a .jar during runtime
loading a class or a .jar during runtime [message #212916] Wed, 30 May 2007 18:54 Go to next message
Eclipse UserFriend
Originally posted by: mire.darkwind-guild.org

I'm trying to load a .jar containing a class file during runtime of a
program. So that, for example, I can use different versions of given
file (all containing the same method(s)). I understand so far that I
have to write my own ClassLoader .. and in order not having to set a new
ClassPath, cache it in form of a byte array for example, no? I think I
have already managed to load a .class using my own FileClassLoader.
However..

What I can't find any information about is:
1.) How to go from there, meaning how to use a loadClassFromJar Method
with a classname String as parameter .. besides, I'm not 100% sure how
to make sure I really succeeded at loading a .class file and
2.) How to use my Method of the class file, once I succeed at loading
it. I have seen solutions on the web using the runnable interface. I do
not wish to do this tho. All I need is something to call a
println("hello world") kinda Method from a test class that I seemed to
be able to load.. so that I can later implement the loader it into my
project.

TIA for any hints,
-M.
Re: loading a class or a .jar during runtime [message #212934 is a reply to message #212916] Wed, 30 May 2007 19:17 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Mire,

Here's a big snip of code from EMF's JETEmitter, which plays class
loader games to be able to load an execute dynamic JET templates (which
involves translating the JET template to Java, compiling that Java code,
and then loading the classes dynamically). Basically we are using
URLClassLoader to most of this, so it's actually quite simple. Include
the URL for where your jar is in the list, e.g., file:/c:/tmp/my.jar,
and then load classes via that class loader. After you have the Class,
you can look up Methods and invoke them.


public static void initialize(Monitor monitor, final JETEmitter
jetEmitter) throws JETException
{
IProgressMonitor progressMonitor =
BasicMonitor.toIProgressMonitor(monitor);
progressMonitor.beginTask("", 10);

progressMonitor.subTask(CodeGenPlugin.getPlugin().getString( "_UI_GeneratingJETEmitterFor_message",
new Object [] { jetEmitter.templateURI }));

final IWorkspace workspace = ResourcesPlugin.getWorkspace();
IJavaModel javaModel =
JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());

try
{
final JETCompiler jetCompiler =
jetEmitter.templateURIPath == null ?
new MyBaseJETCompiler(jetEmitter.templateURI,
jetEmitter.encoding, jetEmitter.classLoader) :
new MyBaseJETCompiler(jetEmitter.templateURIPath,
jetEmitter.templateURI, jetEmitter.encoding, jetEmitter.classLoader);

progressMonitor.subTask
(CodeGenPlugin.getPlugin().getString("_UI_JETParsing_message ",
new Object [] { jetCompiler.getResolvedTemplateURI() }));
jetCompiler.parse();
progressMonitor.worked(1);

String packageName = jetCompiler.getSkeleton().getPackageName();

if (jetEmitter.templateURIPath != null)
{
URI templateURI = URI.createURI(jetEmitter.templateURIPath[0]);
URLClassLoader theClassLoader = null;
if (templateURI.isPlatformResource())
{
// If the template path points at a project with a JET Nature,
// then we will assume that the templates we want to use are
already compiled in this plugin Java project.
//
IProject project =
workspace.getRoot().getProject(templateURI.segment(1));
if (JETNature.getRuntime(project) != null)
{
List<URL> urls = new ArrayList<URL>();

// Compute the URL for where the classes for this
project will be located.
//
IJavaProject javaProject = JavaCore.create(project);
urls.add(new File(project.getLocation() + "/" +
javaProject.getOutputLocation().removeFirstSegments(1) + "/").toURL());

// Compute the URLs for all the output folder of all the
project dependencies.
//
for (IClasspathEntry classpathEntry :
javaProject.getResolvedClasspath(true))
{
if (classpathEntry.getEntryKind() ==
IClasspathEntry.CPE_PROJECT)
{
IPath projectPath = classpathEntry.getPath();
IProject otherProject =
workspace.getRoot().getProject(projectPath.segment(0));
IJavaProject otherJavaProject =
JavaCore.create(otherProject);
urls.add(new File(otherProject.getLocation() + "/" +
otherJavaProject.getOutputLocation().removeFirstSegments(1) + "/").toURL());
}
}

// Define a class loader that will look in the URLs first,
// and if it doesn't find the class there, uses the
emitter's loader.
//
theClassLoader =
new URLClassLoader(urls.toArray(new URL [0]))
{
@Override
public Class<?> loadClass(String className) throws
ClassNotFoundException
{
try
{
return super.loadClass(className);
}
catch (ClassNotFoundException classNotFoundException)
{
return jetEmitter.classLoader.loadClass(className);
}
}
};
}
}
else if (templateURI.isPlatformPlugin())
{
final Bundle bundle =
Platform.getBundle(templateURI.segment(1));
if (bundle != null)
{
// Define a class loader that will look up the class in
the bundle,
// and if it doesn't find it there, will look in the parent.
//
theClassLoader =
new URLClassLoader(new URL [0], jetEmitter.classLoader)
{
@Override
public Class<?> loadClass(String className) throws
ClassNotFoundException
{
try
{
return bundle.loadClass(className);
}
catch (ClassNotFoundException classNotFoundException)
{
return super.loadClass(className);
}
}
};
}
}

if (theClassLoader != null)
{
// Strip off the trailing "_" and load that class.
//
String className = (packageName.length() == 0 ? "" :
packageName + ".") + jetCompiler.getSkeleton().getClassName();
if (className.endsWith("_"))
{
className = className.substring(0, className.length() - 1);
}

try
{
Class<?> theClass = theClassLoader.loadClass(className);
String methodName = jetCompiler.getSkeleton().getMethodName();
Method [] methods = theClass.getDeclaredMethods();
for (int i = 0; i < methods.length; ++i)
{
if (methods[i].getName().equals(methodName))
{
jetEmitter.setMethod(methods[i]);
break;
}
}

// Don't do any of the other normally dynamic JETEmitter
project processing.
//
return;
}
catch (ClassNotFoundException exception)
{
// Continue processing dynamically as normal.
}
}
}

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
jetCompiler.generate(outputStream);
final InputStream contents = new
ByteArrayInputStream(outputStream.toByteArray());

if (!javaModel.isOpen())
{
javaModel.open(new SubProgressMonitor(progressMonitor, 1));
}
else
{
progressMonitor.worked(1);
}

final IProject project =
workspace.getRoot().getProject(jetEmitter.getProjectName());
progressMonitor.subTask

(CodeGenPlugin.getPlugin().getString("_UI_JETPreparingProject_message ",
new Object [] { project.getName() }));

IJavaProject javaProject;
if (!project.exists())
{
progressMonitor.subTask("JET creating project " +
project.getName());
project.create(new SubProgressMonitor(progressMonitor, 1));
progressMonitor.subTask

(CodeGenPlugin.getPlugin().getString("_UI_JETCreatingProject_message ",
new Object [] { project.getName() }));
IProjectDescription description =
workspace.newProjectDescription(project.getName());
description.setNatureIds(new String [] { JavaCore.NATURE_ID });
description.setLocation(null);
project.open(new SubProgressMonitor(progressMonitor, 1));
project.setDescription(description, new
SubProgressMonitor(progressMonitor, 1));
}
else
{
project.open(new SubProgressMonitor(progressMonitor, 5));
IProjectDescription description = project.getDescription();
description.setNatureIds(new String [] { JavaCore.NATURE_ID });
project.setDescription(description, new
SubProgressMonitor(progressMonitor, 1));
}

javaProject = JavaCore.create(project);

// Get the existing classpath and remove the project root if
necessary.
// Any new non-duplicate entries will be added to this.
//
List<IClasspathEntry> classpath = new
UniqueEList<IClasspathEntry>(Arrays.asList(javaProject.getRawClasspath()));
for (int i = 0, len = classpath.size(); i < len; i++)
{
IClasspathEntry entry = classpath.get(i);
if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && ("/"
+ project.getName()).equals(entry.getPath().toString()))
{
classpath.remove(i);
}
}

// Add the new entries, including source, JRE container, and
added variables and classpath containers.
//
progressMonitor.subTask

(CodeGenPlugin.getPlugin().getString("_UI_JETInitializingProject_message ",
new Object [] { project.getName() }));
IClasspathEntry classpathEntry =
JavaCore.newSourceEntry(new Path("/" + project.getName() +
"/src"));

IClasspathEntry jreClasspathEntry =
JavaCore.newContainerEntry(new
Path("org.eclipse.jdt.launching.JRE_CONTAINER"));

classpath.add(classpathEntry);
classpath.add(jreClasspathEntry);
classpath.addAll(jetEmitter.classpathEntries);

IFolder sourceFolder = project.getFolder(new Path("src"));
if (!sourceFolder.exists())
{
sourceFolder.create(false, true, new
SubProgressMonitor(progressMonitor, 1));
}
IFolder runtimeFolder = project.getFolder(new Path("bin"));
if (!runtimeFolder.exists())
{
runtimeFolder.create(false, true, new
SubProgressMonitor(progressMonitor, 1));
}

javaProject.setRawClasspath(classpath.toArray(new
IClasspathEntry[classpath.size()]), new
SubProgressMonitor(progressMonitor, 1));

javaProject.setOutputLocation(new Path("/" + project.getName() +
"/bin"), new SubProgressMonitor(progressMonitor, 1));

javaProject.close();

progressMonitor.subTask

(CodeGenPlugin.getPlugin().getString("_UI_JETOpeningJavaProject_message ",
new Object [] { project.getName() }));
javaProject.open(new SubProgressMonitor(progressMonitor, 1));

IPackageFragmentRoot [] packageFragmentRoots =
javaProject.getPackageFragmentRoots();
IPackageFragmentRoot sourcePackageFragmentRoot = null;
for (int j = 0; j < packageFragmentRoots.length; ++j)
{
IPackageFragmentRoot packageFragmentRoot =
packageFragmentRoots[j];
if (packageFragmentRoot.getKind() ==
IPackageFragmentRoot.K_SOURCE)
{
sourcePackageFragmentRoot = packageFragmentRoot;
break;
}
}

StringTokenizer stringTokenizer = new
StringTokenizer(packageName, ".");
IProgressMonitor subProgressMonitor = new
SubProgressMonitor(progressMonitor, 1);
subProgressMonitor.beginTask("", stringTokenizer.countTokens() + 4);

subProgressMonitor.subTask(CodeGenPlugin.getPlugin().getStri ng( "_UI_CreateTargetFile_message"));
IContainer sourceContainer = sourcePackageFragmentRoot == null ?
project : (IContainer)sourcePackageFragmentRoot.getCorrespondingResour ce();
while (stringTokenizer.hasMoreElements())
{
String folderName = stringTokenizer.nextToken();
sourceContainer = sourceContainer.getFolder(new Path(folderName));
if (!sourceContainer.exists())
{
((IFolder)sourceContainer).create(false, true, new
SubProgressMonitor(subProgressMonitor, 1));
}
}
IFile targetFile = sourceContainer.getFile(new
Path(jetCompiler.getSkeleton().getClassName() + ".java"));
if (!targetFile.exists())
{
subProgressMonitor.subTask

(CodeGenPlugin.getPlugin().getString("_UI_JETCreating_message ", new
Object [] { targetFile.getFullPath() }));
targetFile.create(contents, true, new
SubProgressMonitor(subProgressMonitor, 1));
}
else
{
subProgressMonitor.subTask

(CodeGenPlugin.getPlugin().getString("_UI_JETUpdating_message ", new
Object [] { targetFile.getFullPath() }));
targetFile.setContents(contents, true, true, new
SubProgressMonitor(subProgressMonitor, 1));
}

subProgressMonitor.subTask

(CodeGenPlugin.getPlugin().getString("_UI_JETBuilding_message ", new
Object [] { project.getName() }));
project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, new
SubProgressMonitor(subProgressMonitor, 1));

IMarker [] markers = targetFile.findMarkers(IMarker.PROBLEM,
true, IResource.DEPTH_INFINITE);
boolean errors = false;
for (int i = 0; i < markers.length; ++i)
{
IMarker marker = markers[i];
if (marker.getAttribute(IMarker.SEVERITY,
IMarker.SEVERITY_INFO) == IMarker.SEVERITY_ERROR)
{
errors = true;
subProgressMonitor.subTask
(marker.getAttribute(IMarker.MESSAGE) + " : " +
(CodeGenPlugin.getPlugin().getString
("jet.mark.file.line",
new Object []
{
targetFile.getLocation(),
marker.getAttribute(IMarker.LINE_NUMBER)
})));
}
}

if (!errors)
{
subProgressMonitor.subTask
(CodeGenPlugin.getPlugin().getString
("_UI_JETLoadingClass_message", new Object [] {
jetCompiler.getSkeleton().getClassName() + ".class" }));

// Construct a proper URL for relative lookup.
//
List<URL> urls = new ArrayList<URL>();
urls.add(new File(project.getLocation() + "/" +
javaProject.getOutputLocation().removeFirstSegments(1) + "/").toURL());

// Determine all the bundles that this project depends on.
//
final Set<Bundle> bundles = new HashSet<Bundle>();
LOOP:
for (IClasspathEntry jetEmitterClasspathEntry :
jetEmitter.getClasspathEntries())
{
IClasspathAttribute [] classpathAttributes =
jetEmitterClasspathEntry.getExtraAttributes();
if (classpathAttributes != null)
{
for (IClasspathAttribute classpathAttribute :
classpathAttributes)
{
if
(classpathAttribute.getName().equals(CodeGenUtil.EclipseUtil .PLUGIN_ID_CLASSPATH_ATTRIBUTE_NAME))
{
Bundle bundle =
Platform.getBundle(classpathAttribute.getValue());
if (bundle != null)
{
bundles.add(bundle);
continue LOOP;
}
}
}
}
// For any entry that doesn't correspond to a plugin in the
running JVM, compute a URL for the classes.
//
urls.add(new URL("platform:/resource" +
jetEmitterClasspathEntry.getPath() + "/"));
}

// Define a class loader that looks up classes using the URLs
or the parent class loader,
// and failing those, tries to look up the class in each
bundle in the running JVM.
//
URLClassLoader theClassLoader =
new URLClassLoader(urls.toArray(new URL [0]),
jetEmitter.classLoader)
{
@Override
public Class<?> loadClass(String className) throws
ClassNotFoundException
{
try
{
return super.loadClass(className);
}
catch (ClassNotFoundException exception)
{
for (Bundle bundle : bundles)
{
try
{
return bundle.loadClass(className);
}
catch (ClassNotFoundException exception2)
{
// Ignore because we'll rethrow the original
exception eventually.
}
}
throw exception;
}
}
};
Class<?> theClass =
theClassLoader.loadClass
((packageName.length() == 0 ? "" : packageName + ".") +
jetCompiler.getSkeleton().getClassName());
String methodName = jetCompiler.getSkeleton().getMethodName();
Method [] methods = theClass.getDeclaredMethods();
for (int i = 0; i < methods.length; ++i)
{
if (methods[i].getName().equals(methodName))
{
jetEmitter.setMethod(methods[i]);
break;
}
}
}

subProgressMonitor.done();
}
catch (CoreException exception)
{
throw new JETException(exception);
}
catch (Exception exception)
{
throw new JETException(exception);
}
finally
{
progressMonitor.done();
}
}

Mire wrote:
> I'm trying to load a .jar containing a class file during runtime of a
> program. So that, for example, I can use different versions of given
> file (all containing the same method(s)). I understand so far that I
> have to write my own ClassLoader .. and in order not having to set a
> new ClassPath, cache it in form of a byte array for example, no? I
> think I have already managed to load a .class using my own
> FileClassLoader. However..
>
> What I can't find any information about is:
> 1.) How to go from there, meaning how to use a loadClassFromJar Method
> with a classname String as parameter .. besides, I'm not 100% sure how
> to make sure I really succeeded at loading a .class file and
> 2.) How to use my Method of the class file, once I succeed at loading
> it. I have seen solutions on the web using the runnable interface. I
> do not wish to do this tho. All I need is something to call a
> println("hello world") kinda Method from a test class that I seemed to
> be able to load.. so that I can later implement the loader it into my
> project.
>
> TIA for any hints,
> -M.
Re: loading a class or a .jar during runtime [message #212950 is a reply to message #212934] Wed, 30 May 2007 20:31 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mire.darkwind-guild.org

Thanks a lot for your fast answer and the detailed snippet. But I'm
afraid me fully understanding and using your code would produce an
unacceptable overhead.. also, because I just started utilizing EMF
functionality about 2 months ago. (This newsgroup was called "newcomer"
after all, heh)
However, I understand that I can use the URLClassloader seperately from
EMF to create a dynamic ClassLoader.. and that I will have to set a
classpath regardless of how. After loading, I can use
getDeclaredMethods() to find out which methods my class offers.
Another question that arose during my research: When I use
myClass.newInstance(), I have not been able to cast the return into my
desired Object so I can actually use its methods .. nor can I find any
instancing in the snip you provided. I apologize for my ignorance of not
having heard about JETEmitter before; from what I can see in the snippet
tho, you're giving the methods of the loaded class to a jetEmitter
object? I'm not sure how effective and appropriate that would be for my
case.

Thanks again for your already provided assistance
-M.

> Mire,
>
> Here's a big snip of code from EMF's JETEmitter, which plays class
> loader games to be able to load an execute dynamic JET templates (which
> involves translating the JET template to Java, compiling that Java code,
> and then loading the classes dynamically). Basically we are using
> URLClassLoader to most of this, so it's actually quite simple. Include
> the URL for where your jar is in the list, e.g., file:/c:/tmp/my.jar,
> and then load classes via that class loader. After you have the Class,
> you can look up Methods and invoke them.
>
>
> (Snippet here)
>
> Mire wrote:
>> I'm trying to load a .jar containing a class file during runtime of a
>> program. So that, for example, I can use different versions of given
>> file (all containing the same method(s)). I understand so far that I
>> have to write my own ClassLoader .. and in order not having to set a
>> new ClassPath, cache it in form of a byte array for example, no? I
>> think I have already managed to load a .class using my own
>> FileClassLoader. However..
>>
>> What I can't find any information about is:
>> 1.) How to go from there, meaning how to use a loadClassFromJar Method
>> with a classname String as parameter .. besides, I'm not 100% sure how
>> to make sure I really succeeded at loading a .class file and
>> 2.) How to use my Method of the class file, once I succeed at loading
>> it. I have seen solutions on the web using the runnable interface. I
>> do not wish to do this tho. All I need is something to call a
>> println("hello world") kinda Method from a test class that I seemed to
>> be able to load.. so that I can later implement the loader it into my
>> project.
>>
>> TIA for any hints,
>> -M.
Re: loading a class or a .jar during runtime [message #212958 is a reply to message #212950] Wed, 30 May 2007 20:47 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Mire,

Mire wrote:
> Thanks a lot for your fast answer and the detailed snippet. But I'm
> afraid me fully understanding and using your code would produce an
> unacceptable overhead..
Well, yes. :-) It's really only the different uses of a derived
URLClassLoader that are interesting for you.
> also, because I just started utilizing EMF functionality about 2
> months ago.
2 months. You're not a newbie anymore then!
> (This newsgroup was called "newcomer" after all, heh)
> However, I understand that I can use the URLClassloader seperately
> from EMF to create a dynamic ClassLoader.. and that I will have to set
> a classpath regardless of how. After loading, I can use
> getDeclaredMethods() to find out which methods my class offers.
Yes, exactly.
> Another question that arose during my research: When I use
> myClass.newInstance(), I have not been able to cast the return into my
> desired Object so I can actually use its methods .
Yes, casting would imply that the object is an instance of the thing you
are casting to, so unless the loaded object implements an API of a
static class, you won't have a compile-time class you can cast it to.
> . nor can I find any instancing in the snip you provided.
Yep. You'd need to do stuff like this:

Method method = javaClass.getMethod("create", String.class);
object = method.invoke(null, lineDelimiter);
I.e., look for a method with a specific signature and then invoke it.
In this case I'm invoking a static method, so the first argument is
null. But probably you'll be passing an instance object for the first
argument.
> I apologize for my ignorance of not having heard about JETEmitter
> before; from what I can see in the snippet tho, you're giving the
> methods of the loaded class to a jetEmitter object? I'm not sure how
> effective and appropriate that would be for my case.
I'm not sure how you want to use the instance, but I think you'd either
cast to an interface implemented by the instance or look for methods
with an expected name and signature and then invoke that method with
appropriate arguments.
>
> Thanks again for your already provided assistance
> -M.
>
>> Mire,
>>
>> Here's a big snip of code from EMF's JETEmitter, which plays class
>> loader games to be able to load an execute dynamic JET templates
>> (which involves translating the JET template to Java, compiling that
>> Java code, and then loading the classes dynamically). Basically we
>> are using URLClassLoader to most of this, so it's actually quite
>> simple. Include the URL for where your jar is in the list, e.g.,
>> file:/c:/tmp/my.jar, and then load classes via that class loader.
>> After you have the Class, you can look up Methods and invoke them.
>>
>>
>> (Snippet here)
>>
>> Mire wrote:
>>> I'm trying to load a .jar containing a class file during runtime of
>>> a program. So that, for example, I can use different versions of
>>> given file (all containing the same method(s)). I understand so far
>>> that I have to write my own ClassLoader .. and in order not having
>>> to set a new ClassPath, cache it in form of a byte array for
>>> example, no? I think I have already managed to load a .class using
>>> my own FileClassLoader. However..
>>>
>>> What I can't find any information about is:
>>> 1.) How to go from there, meaning how to use a loadClassFromJar
>>> Method with a classname String as parameter .. besides, I'm not 100%
>>> sure how to make sure I really succeeded at loading a .class file and
>>> 2.) How to use my Method of the class file, once I succeed at
>>> loading it. I have seen solutions on the web using the runnable
>>> interface. I do not wish to do this tho. All I need is something to
>>> call a println("hello world") kinda Method from a test class that I
>>> seemed to be able to load.. so that I can later implement the loader
>>> it into my project.
>>>
>>> TIA for any hints,
>>> -M.
Re: loading a class or a .jar during runtime [message #213326 is a reply to message #212958] Fri, 01 June 2007 17:02 Go to previous message
Eclipse UserFriend
Originally posted by: mire.darkwind-guild.org

Some waste of Newsgroup-Space.. but.. I just wanted to thank you again,
Ed! Finally got it all working, loading a class and using a method of
it.. guess it's all easy once one figured it out!

> Mire,
>
> Mire wrote:
>> Thanks a lot for your fast answer and the detailed snippet. But I'm
>> afraid me fully understanding and using your code would produce an
>> unacceptable overhead..
> Well, yes. :-) It's really only the different uses of a derived
> URLClassLoader that are interesting for you.
>> also, because I just started utilizing EMF functionality about 2
>> months ago.
> 2 months. You're not a newbie anymore then!
>> (This newsgroup was called "newcomer" after all, heh)
>> However, I understand that I can use the URLClassloader seperately
>> from EMF to create a dynamic ClassLoader.. and that I will have to set
>> a classpath regardless of how. After loading, I can use
>> getDeclaredMethods() to find out which methods my class offers.
> Yes, exactly.
>> Another question that arose during my research: When I use
>> myClass.newInstance(), I have not been able to cast the return into my
>> desired Object so I can actually use its methods .
> Yes, casting would imply that the object is an instance of the thing you
> are casting to, so unless the loaded object implements an API of a
> static class, you won't have a compile-time class you can cast it to.
>> . nor can I find any instancing in the snip you provided.
> Yep. You'd need to do stuff like this:
>
> Method method = javaClass.getMethod("create", String.class);
> object = method.invoke(null, lineDelimiter);
> I.e., look for a method with a specific signature and then invoke it.
> In this case I'm invoking a static method, so the first argument is
> null. But probably you'll be passing an instance object for the first
> argument.
>> I apologize for my ignorance of not having heard about JETEmitter
>> before; from what I can see in the snippet tho, you're giving the
>> methods of the loaded class to a jetEmitter object? I'm not sure how
>> effective and appropriate that would be for my case.
> I'm not sure how you want to use the instance, but I think you'd either
> cast to an interface implemented by the instance or look for methods
> with an expected name and signature and then invoke that method with
> appropriate arguments.
>>
>> Thanks again for your already provided assistance
>> -M.
>>
>>> Mire,
>>>
>>> Here's a big snip of code from EMF's JETEmitter, which plays class
>>> loader games to be able to load an execute dynamic JET templates
>>> (which involves translating the JET template to Java, compiling that
>>> Java code, and then loading the classes dynamically). Basically we
>>> are using URLClassLoader to most of this, so it's actually quite
>>> simple. Include the URL for where your jar is in the list, e.g.,
>>> file:/c:/tmp/my.jar, and then load classes via that class loader.
>>> After you have the Class, you can look up Methods and invoke them.
>>>
>>>
>>> (Snippet here)
>>>
>>> Mire wrote:
>>>> I'm trying to load a .jar containing a class file during runtime of
>>>> a program. So that, for example, I can use different versions of
>>>> given file (all containing the same method(s)). I understand so far
>>>> that I have to write my own ClassLoader .. and in order not having
>>>> to set a new ClassPath, cache it in form of a byte array for
>>>> example, no? I think I have already managed to load a .class using
>>>> my own FileClassLoader. However..
>>>>
>>>> What I can't find any information about is:
>>>> 1.) How to go from there, meaning how to use a loadClassFromJar
>>>> Method with a classname String as parameter .. besides, I'm not 100%
>>>> sure how to make sure I really succeeded at loading a .class file and
>>>> 2.) How to use my Method of the class file, once I succeed at
>>>> loading it. I have seen solutions on the web using the runnable
>>>> interface. I do not wish to do this tho. All I need is something to
>>>> call a println("hello world") kinda Method from a test class that I
>>>> seemed to be able to load.. so that I can later implement the loader
>>>> it into my project.
>>>>
>>>> TIA for any hints,
>>>> -M.
Previous Topic:Problem Reading Cheat Sheet instructions
Next Topic:Can I build eclipse for Solaris on Windows PC?
Goto Forum:
  


Current Time: Fri Mar 29 01:15:39 GMT 2024

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

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

Back to the top