2

Imagine I have a file that contains the following import statement:

import a.b.c.D;

We can assume this class (a.b.c.D) has already been compiled. How can I find (programmatically) the location of the .class file on the disk, starting from the "a.b.c.D" string?

Consider that:

  • c in a.b.c.D might be a class, despite this being highly unorthodox.
  • D might be defined in file E.java (Java allows the definition of multiple class per file, as long as only one is public)
  • the .class file might be in a .jar or .zip file

Ideally, I don't want to write an algorithm to traverse directories and .jar as that seems to involve quite a bit of work.

Norswap
  • 11,740
  • 12
  • 47
  • 60
  • seems like duplicate of http://stackoverflow.com/questions/1983839/determine-which-jar-file-a-class-is-from – akostadinov Jun 03 '13 at 16:29
  • @akostadinov No, because (at first look at least) that answer assumes that the class is loaded in the Java program, which is not the case for me. I should have mentioned that. (I'm writing a program that works on other Java files.) – Norswap Jun 03 '13 at 16:33
  • So you can only find jars containing the class but you can never know which of them will be loaded at the other program runtime. There are a lot such questions. Basically you open the jars and list what classes are in there. – akostadinov Jun 03 '13 at 17:00
  • Yes, but as said in my post "Ideally, I don't want to write an algorithm to traverse directories and .jar as that seems to involve quite a bit of work." – Norswap Jun 03 '13 at 23:12
  • so how do you think you can find out location of a class if you 1. are not runnign the program and 2. don't want to traverse directory trees?! There are plenty of ready to borrow algorithms in the Internet to traverse directory trees and reading zip files. You don't need any special libraries besides the java jre. – akostadinov Jun 04 '13 at 18:40
  • Of course parsing the directory tree must happen in the end; but I was hoping there was some code out there that did that already (maybe even helpful methods in the standard library). – Norswap Jun 04 '13 at 18:51

0 Answers0