Solved

Get a class from a jar gievn the className

Posted on 2011-09-20
9
247 Views
Last Modified: 2012-05-12
Hi Experts,
I need to create a method that  returns a class<?>  from a jarFile given a className :
Class<?>  getClassFromJar (JarFile jar, className){
      Return the Class / Null if not found
}
e.g.
Class<?> clazz = getClassFromJAr ( MyBundle.jar, “org.kal.os.factory.class”);
0
Comment
Question by:bachra04
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 2
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36569215
You mean that this jar is not in your classpath ?

Otherwise you could use Class.forName(...)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569280
0
 
LVL 2

Author Comment

by:bachra04
ID: 36569281
Sorry not to be clear, the jar contains multiple classes including className

So the method will extract all classes and if the class

matches the given className, it returns it.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 36569285
This is how you can do it with JCL:

JarClassLoader jcl = new JarClassLoader();

  //Loading classes from different sources
  jcl.add("myjar.jar");
  jcl.add(new URL("http://myserver.com/myjar.jar"));
  jcl.add(new FileInputStream("myotherjar.jar"));
  jcl.add("myclassfolder/");

  //Recursively load all jar files in the folder/sub-folder(s)
  jcl.add("myjarlib/");

  JclObjectFactory factory = JclObjectFactory.getInstance();

  //Create object of loaded class
  Object obj = factory.create(jcl, "mypack.MyClass");

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569295


So do you need this class for using it in the same code after you extract it?
Or you just need to extract it as a file?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569303
If you need to use it in your code then
the above snippet from JCL shows how to create instance of such class
0
 
LVL 2

Author Comment

by:bachra04
ID: 36569318
I need to extract it as a file
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569393
To extract as a file - jar is just a zip file - so
the same way you extract it from zip file should work
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36569400
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java exception bubble up 2 25
jsp login check 12 61
Facing this issue for maven proxy setting 2 40
printf performancy 11 98
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question