counting files into jar

N

Novello

Hello,

I've a problem that I cannot solve and maybe you can help me.

Supposed to have the following package into a jar:

MyJar.jar
../main
main.class ( runnable )
../systems
class1.class
class2.class
...
classN.class


Can I get the classname for each file into systems package that is inside my
current jar ?
Any ideas?

thank you all!
 
T

Thomas Weidenfeller

Novello said:
Supposed to have the following package into a jar: [...]
Can I get the classname for each file into systems package that is inside my
current jar ?
Any ideas?

If you have a META-INF/INDEX.LIST file in the jar, you can just read
that one file and take the information from that index file. Of course
assuming your jar was created in a proper way and INDEX.LIST contents
and jar contents match.

If you don't have an INDEX.LIST file in the jar you need to get each
Entry, and convert that entry's name into a class name by removing the
".class" suffix and replacing all '/' with '.' in the entry names.

If you don't trust the jar at all, you have to unpack each file and
examine it with some tool, to figure out if entry name and class name as
provided in the .class file match.

/Thomas
 
N

Novello

thank you all !

I solved this way:
The only problem is the .jar file, I set myfile.jar but if the user change
this file name this code will not work anymore.
I will search for jar file in the current dir , this will works.
ciao!

=============================================

String repo = SYSTEMS_REPO.replace(".","/");
String system = "";

try {
JarInputStream jis = new JarInputStream(new
FileInputStream("myfile.jar"));

// Print the attributes for each jar entry.
while (true) {
final JarEntry je = jis.getNextJarEntry();
if (je==null) break;
//System.out.println(je.toString());
if (je.toString().contains(repo))
system = je.toString().substring(repo.length());
if (system.contains(".class")){
system = system.substring(0,system.length()-6);
// strip ISystem.class that's an Interface and do not
interest endusers
if (!system.contains(("/"))){
System.out.println("["+system.toLowerCase()+"]");
if (fullList) showSystemInfo(system);
}
}

}
jis.close();
} catch (Exception e) {
e.printStackTrace();
}
=======================================================
 
F

Ferenc Hechler

I solved this way:
The only problem is the .jar file, I set myfile.jar but if the user
change
this file name this code will not work anymore.
I will search for jar file in the current dir , this will works.
ciao!

If the jar is started using "java -jar myapp.jar" you can get the jar name
using System-Properties:

String jarFilename = System.getProperty("java.class.path");

Best Regards,
feri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top