• Advertisement
Sign in to follow this  

[java] Jar and Files

This topic is 4762 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Good day! This is my second first post here! (My old account has expired, I guess) So yay! Got a nice question for you all, Before we go any further: yes I searched the forums and google. Oke so I made a small application, which reads a directory (File.listfiles()) and than creates an instance of all classes found in the directory. That's pretty basic, right? Untill I thought of JAR files. How would one be able to read a directory in the same JAR file and create a class of every class in that directory? From what I've seen, I can't use a File object and an URL isn't the way to go either? Thanks for your time, Wesley

Share this post


Link to post
Share on other sites
Advertisement
You can use java.util.zip and java.util.jar to open and read files inside the jar.

Share this post


Link to post
Share on other sites
Hey man, thanks

Oke, but does that work if you use webstart? I mean can you read the same Jar file as the class that reads them is in?

I'm gonna check it out now,
Wesley

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I have a big chunk of source code that does all this for you automatically.

Except...it goes through EVERYTHING that your JVM can find, all classes, all JAR files, all *anything else you can think of*.

The purpose is to automatically discover all instances of a given interface, so e.g. you make an interface "ExternalPlugin" and use this at runtime to find all instances (just 3 method calls) - but anyone can drop ANY kind of class files (in JAR's, or just classes) into their JVM, or current directory, or classpath, and those plugins will be automatically discovered.

So...it's pretty much overkill for your needs, but I suspect you could just copy/paste out the code that you need (it's in two classes, and separated into methods for dealing with class files, dealing with jar files, etc).

If you want it...the code is open-source, but I just haven't got around to putting it up on the web yet. I also have it as a compiled JAR so you can just include it directly in your application don't even have to bother compiling it. It's going to get a permanent home at the javagamesfactory.org sooner or later, along with 5 or 6 other open-source java games snippets that people have donated. How urgently do you need this? Can you wait a few weeks?

Share this post


Link to post
Share on other sites
Ahh that would be great man! I can wait a few weeks, the project deadline is in about 2 months. But hey, if you want to, I can host it for you, got some free bandwidth/space here, just email me :)

Wesley

wesley(at)wickedwesley(dot)com

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ah, hosting's not the problem. JGF has ethernet connectivity (double-digit megabit connection). The problem is just writng the webpages for it, adding features to let people comment / offer improvements, writing up the explanations of how to use it, etc.

If it gets more urgent, send an email to ceo @ grexengine.com with "jgf" somewhere in the subject and that'll get to me. I'll try and get an announcement on GD.net when the source code pages go live so you should hear about it when it happens.

Share this post


Link to post
Share on other sites
Aaaaah great dude! We love you! ;-) I'll just keep checking the announcements and stuff. Looking foward to your 'app' :D Sounds very cool.

Cheers!
Wesley

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It's not much code (only a kb or so) but it's a bitch to write (so many completely differnet ways of loading classes etc and lots of weird ways to go wrong). It's also something you only rarely need (like, once per project) but when you need it you *really* need it.

The main thing (more than you need in your case) can be very slow (30 seconds!) to preload all classes to do the checks, so it takes a set of arguments for which packages and classnames to ignore, e.g. you can tell it to only look for classes which are in package "com.myapp.plugins" which gets it down to a few seconds or so. It's because it's going through the thousands of classes on your classpath...

Anyway, as I said, you can just copy/paste out the bit you need. But I'm not even sure which version is the latest right now, so need to get my ass in gear and get it verified then online :(.

Share this post


Link to post
Share on other sites
Just answering the first question: "How would one be able to read a directory in the same JAR file and create a class of every class in that directory?"

try {
String jarname = "onejar.jar";
JarInputStream stream = new JarInputStream(getClass().getResourceAsStream(jarname));
URLClassLoader loader = new URLClassLoader(new URL[] { getClass().getResource(jarname) },
getClass().getClassLoader());

JarEntry entry = stream.getNextJarEntry();
while (entry != null) {

if (!entry.isDirectory()) {
if (entry.getName().endsWith("class")) {
// is this what you mean by
// create a class of every class in that directory?
try {
String classname = entry.getName();
Class aClass = loader.loadClass(classname.substring(0, classname.length()-6));
System.out.println(aClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}

entry = stream.getNextJarEntry();
}
} catch (java.io.IOException e) {
e.printStackTrace();
}



Is this what you mean? Anyway what for?
Sorry if that's not what you looking for :-)

Share this post


Link to post
Share on other sites
Quote:
Original post by role
Just answering the first question: "How would one be able to read a directory in the same JAR file and create a class of every class in that directory?"

*** Source Snippet ***

Is this what you mean? Anyway what for?
Sorry if that's not what you looking for :-)


Yes that's what I'm looking for :D! Thank you! (Now I also love you :P) I'm gonna test it now, thanks!

Wesley

Edit: Sorry forget to tell you; I'm using it for a game state machine, it loads every state in a certain directory. Also need it for a plugin-thingy in the future .

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement