Solved

Get a class from a jar gievn the className

Posted on 2011-09-20
9
242 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 79
windows explorer path to command prompt 5 44
by zero exception 10 41
What's wrong with this code? 4 23
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

777 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