Difference between java class and jar - java

What is the difference between a java class file and a jar file?

A Jar file is an Zip archive containing one or multilple java class files. This makes the usage of libraries (consisting of multiple classes) more handy. Directories and Jar files are added to the classpath and available to the ClassLoader at runtime to find particular classes inside of it.

A JAR file has many files into it. We generally use .jar files to distribute Java applications or libraries, in the form of Java class files and associated metadata and resources (text, images, etc.). you can say JAR = Java ARchive.
All java files are compiled into class files and then we use that class file not the .java file. A class is a construct that is used as a blueprint to create instances of the class.
See Wikipedia for more information: Wikipedia for JAR and Wikipedia for class

A java file contains Java code. A Java file is compiled to produce a class file that can be loaded by the JVM. The contents of a class file are rather well explained here. A Jar file is an archive of otherfile, most likely class files.

JAR file is the compressed file format. You can store many files in a JAR file. JAR stands for the Java Archive. This file format is used to distribute a set of java classes. This file helps you to reduce the file size and collect many file in one by compressing files
a Class is explained here

Related

Usage of jar with .java files and odd behavior of the compiler

I was curious about the differences between .jar with .class files and .jar with .java files. I partially got the answer here, But then what is the usefulness of .java files in the jar?
My guess is that the java files in the jar are like an interface that prevents compilation error, because I solved the IllegalAccessError thrown on runtime by replacing jar files with .class with jar files with .java specifically when using Xposed Framework. (Got the hint from this thread.)
Also
Thank you for your explanations and they were helpful. But I want to learn more about the differences in compiler's view, because I am wondering why my app works fine even if I only included the jar with java files, not class files (zxing). Also there are some cases that throws IllegalAccessException when I include the jar with class files, but not thrown when I include the jar with java files(xposed), even though I have to include at least one of them to make the compiler(AIDE) not complain about references, like unknown package. Why does the compiler not complain when I include only jar with java files though the compiler would not be able to resolve the actual implementation of the referred classes?
A .jar file is basically just a .zip file with another extension.
A .jar file with .class files have a special purpose and may have special meta-data (e.g. in META-INF folder).
A .jar file .java files is just a .zip file.
It is however common for open-source libraries to provide 3 .jar files:
One with .class files, to be used by your code, both to compile and to run your code.
One with .java files, to be used by your IDE, so you can drill into the library code and see it. Especially useful when stepping through the code with a debugger.
One with javadoc files (.html files), to be used by your IDE, so you can read the documentation about the classes and methods in the library. You do read the documentation, right?
None of those 3 files have to be named .jar. They could be renamed .zip so you could easily open them in your favorite Zip utility, or they could be renamed .foo just because...
They should be named .jar, to clarify that they are Java ARchives.
Its simple - *.java files are sources, *.class files are compiled classes.
What is used on runtime by JVM?? *.class files. Why would you put source files inside library? IDK, usally sources are distributed as separate jar, but all in all it is done to allow you to check library code without decompilation.

Theoretical sequence: how is a .jar file created?

How are .jars created?
I know that an IDE like Eclipse can create Bytecode (.class) from developed Sourcecode (.java). And it can under "Export" create an .jar.
And know i want to know: Is an .jar created:
direct from Sourcecode?
from Bytecode which was copied?
with a totally other technique?
Source code files (.java files) are compiled to bytecode by Java Compiler. Bytecode is then stored in .class files.
These files are then packed together using jar tool to create JAR (Java Archive) file. JAR file is a zip archive usually containing:
.class files,
jar manifest
application resources
You can read more about Jar files on oficial documentation: https://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html
A .jar is just an archive of .class files along with associated resources (if any) and metadata (if any). In fact, "jar" means Java ARchive, and the file format is the same as .zip files. From the Oracle JAR file overview :
JAR consists of a zip archive, as defined by PKWARE, containing a manifest file and potentially signature files, as defined in the JAR File Specification.
So source code is compiled to .class files (bytecode) which is then wrapped up in a .jar archive via the jar tool (or anything else that can correctly create .jar files per the spec).

What is a class file? What is in that file?

I know this probably won't matter to most beginners who just want to write app and that they will probably know it as they do more in Java, but it's been six months learning Java, I have no idea what this is which is the very first chapter of my Java book. So I guess I should ask.
From what I read on the book, a file class contains source code which compiler translate using resources from Java library. And it can only be created when all syntax error is fixed.
So is it just a normal file we store in our computer with a .java ending?
And also where is Java library? And how big is it?
When using java, you will encounter a few different types of files:
.java file: These are the text files where source code lives. Those .java files are compiled to .class files.
.class file: This is byte code compiled by the javac program. The javac program creates these files from .java files. The java classloader loads classes from these files and not from the .java files.
.jar file: This is what you're referring to as a java library. It is a collection of .class files, .properties files, and other files. It's just a zip file with the extension changed to .jar
.war file: This is a web application archive. It's very similar to a .jar file. It contains classes in /WEB-INF/classes and jars in /WEB-INF/LIB
.ear file: This is a way to package multiple wars into one file.
.properties file: java.util.Properties can be read easily from these. It's a text file, with each line in the format of key=value.

Writing A Text File To A Jar

I'm writing a program which 'builds' another. I plan to save the settings insite the generated jar file, but am having some trouble with writing to an external jar. Any help is appreciated.
A jar is just a zip file so all you have to do is zip up your files and name the zip with a .jar extension.
One handy library to create jar archives is Shrinkwrap which allows you to put classes, resources, etc into jar files using a Java API
A better solution would be to create a second jar, and write your own ClassLoader to load classes from that jar. A kind of plugin mechanism.

Working with Java JAR files from cmdline

Let us have a Java application, contained in files A.java and B.java with dependency on somejar.jar file. The questions are:
how to create a self-runnable JAR file with all the dependencies and sources compiled within? The main condition is using the standard Java utilities only (given with JDK; e.g.: java, javac and jar) and NOT any of build tools like Maven, Ant or any other.
how to use external JAR files within my application? For example, the algorithm is: if the 'otherjar.jar' is present near the main application JAR, we should call method Moo::method1 from that class, passing the new instance of Foo class to it. Moo and Foo should be present in 'otherjar.jar' file. Still, the 'config.xml' file should be there too.
From the link that #michael667 gave, you may be more interested in this section: Adding Classes to the JAR File's Classpath. In particular this note:
Note:
The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path.
It's not a possible thing to do with the standard java tools if you don't code your own classloader. There are tools out there, like One-jar which can provide you of such classloader.
Of course, you could always manually use the exploding-jar approach, but that doesn't seem to be what you really want.
You may also find the answers to this question useful: Classpath including JAR within a JAR
This should answer most of your questions: http://download.oracle.com/javase/tutorial/deployment/jar/basicsindex.html

Categories

Resources