What exactly does a jar file contain?
As an intern, I use company code in my projects and they usually send me a jar
file to work with. I add it to the build path
in Eclipse
and usually all is fine and dandy.
However, I got curious to know, what each class contained and when I try to open one of the classes in the jar
file, it tells me that I need a source file.
What does this mean? I come from a C/C++ background so is a jar
similar to an already compiled .o
file and all I can see is the .h
stuff? Or is there actual code in the jar
file that I'm using that's encrypted so I can't read it?
Thanks for all the answers!
Edit:
Thanks, guys, I knew it was a sort of like an archive but I was confused to why when I tried to open the .class
files, I got a bunch of random characters. The output was similar when I tried to open a .o
file in C so I just wanted to make sure.
Thanks!
A JAR file is actually just a ZIP file. It can contain anything - usually it contains compiled Java code (*.class), but sometimes also Java sourcecode (*.java).
However, Java can be decompiled - in case the developer obfuscated his code you won't get any useful class/function/variable names though.
However, I got curious to what each class contained and when I try to open one of the classes in the jar file, it tells me that I need a source file.
A jar file is basically a zip file containing .class files and potentially other resources (and metadata about the jar itself). It's hard to compare C to Java really, as Java byte code maintains a lot more metadata than most binary formats - but the class file is compiled code instead of source code.
If you either open the jar file with a zip utility or run jar xf foo.jar
you can extract the files from it, and have a look at them. Note that you don't need a jar file to run Java code - classloaders can load class data directly from the file system, or from URLs, as well as from jar files.