0

I know there is this article on JAR files on Linux but the solution involves some Linux specific commands I don't know, and ultimately I'd like to understand if I can access my JAR (and JARs it depends on) without having to write a script.

I am on Windows 7 and my main Java class is org.sync.MainEntry located in my main JAR called synchronizer.jar. I placed the absolute path of all the other JARs it depends on into my CLASSPATH variable in Computer -> Advanced Settings -> Environment Variables. I even tried moving those JARs into the same folder as my main JAR.

But, when I execute (in command-prompt, navigated to my main JAR, with or without other JARs) the following call:

java org.sync.MainEntry

I get an "Unable to access jarfile org.sync.MainEntry" error. So I move ALL the JARs into the same place and have to do

java -cp my_other_jar.jar;my_other_other_jar.jar;synchronizer.jar org.sync.MainEntry

and that finally works. Or I can replace all the jars with just *. But, I either need to move all the JARs to the same place or put in full paths and I feel there's gotta be a way to do with without creating a shell script or writing out the full JAR paths each time.

Based on the article mentioned above, I am happy to accept that CLASSPATH doesn't see jars, just the classes inside, but a JAR is an archive, maybe I can extract them somehow and point the CLASSPATH to there?

Thanks in advance for your help!

1 Answer 1

0

JAR files are application libraries. You can compare them to DLLs. However a DLLs is always only a library of code that an executable would use. A JAR can contain either only library code but it can also contain the application itself. Another difference is that Windows can centralize/register DLLs so that they can be discovered. No such concept exist for JARs. They are really just artifacts that belong to a specific Java application. They can be shared at runtime but it is rare to see. A Java application will typically deliver with it all JARs needed to run it and never rely on any JARs to already exist on the target system (except those that are part of Java itself). Java people tend to not care about such duplication between applications because JARs are often rather small in size.

A JAR is really just a ZIP file of .class files that the Java Virtual Machine uses. Inside the ZIP there's always a so called MANIFEST file and this is just about the only thing that makes it different from a traditional ZIP. (when it is really just a ZIP why the heck can't you browse it Windows? You need to ask Microsoft about that. Use a proper tool for dealing with compressed files, I recommend 7Zip, rather than relying on Windows own tools)

For any Java application to launch it needs to know where to find these .class files. These files can exist either individually on the disk or they can be bundled in a JAR file. The result is the same. There's no point in extracting the .class files from the JAR though.

A Java application will typically come with a launcher script so that the 'customer' will not have to write his own little .BAT file (or whatever) just to launch the application. In case no such script exist you have to do it on your own.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .