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.
Related
when making jar file in java, how to make jar file in a different folder where other files are not located?
I have two folders f1&f2
in f1/ft1/ft2/ft3 I have the java code in f2/ft1 I have few csv fine files to be used in the java code, and I have to create jar in f2/ft2. how to do it?
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.
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).
In computer science I have learned that .jar files are basically a compressed set of .java files that have been compiled. So, when you have a project, instead of those 20 .java files you can have a pile of compressed classes (a .jar). Last year in CSI we worked with a .jar file called DanceStudio, which we had to use to make feet walk across the floor. This year, we are working with a different program to better understand java, so i unzipped the .jar file contained 26 classes, which I then decompiled. I wanted to try to create a program by compiling all of the .java files with the others necessary to make the program run (Walker, Foot, ETC.) When I try to compile all of these files, it will say that I have duplicate files (Walker, Foot, ETC.) What I don't understand is why this would compile if the .jar file was basically the same thing, just in a compressed form. What also confuses me is that the Foot, ETC files in the .jar are actually more complicated and have more code.
Could someone please explain how the .jar file actually works and separates these files apart, and how it could run with a duplicate class that isn't in the .jar file?
First of all, you're missing one step in your explanation of a .jar file.
A .jar file is a collection of .class files. And .class files are what is produced by compiling a .java file.
Usually a single .java file will produce a single .class file, because it will contain a single type definition. But there are several ways for a .java file to produce more than one .class files (inner/nested classes, anonymous classes, top-level non-public classes, ...), so it's not necessarily a 1-to-1 association between .java files and .class files.
Then there's the confusion why the decompiled Java source code looks more complicated than the original Java source. This one is easy to answer: the compilation step was not designed to be reversable.
When the Java compiler turns .java files to .class files it produces a format that is best suited for being executed. That format will not represent the exact same concepts that the Java source file does. For example: there's no classical "if" in the Java bytecode. It will be implemented be appropriate jump commands.
All of this means that the process of converting .class files back to .java files is complicated and usually non-perfect.
You generally compile your (clear text) .java source files into (binary) .class files.
If you use packages, then the class files will be in different subdirectories (representing the package).
A .jar file is a compressed binary file that puts all the .classes in the right directories in one compact, easy to manage file.
.jar file can also contain other files, such as manifests, bitmaps and resources.
.jar files can also be "signed" to insure the integrity/authenticity of their contents.
Here are some good links:
http://en.wikipedia.org/wiki/JAR_%28file_format%29
http://download.oracle.com/javase/tutorial/deployment/jar/
'Hope that helps
About your duplicate: Maybe your .jar is still in your build path, so when you try to compile your project with the decompiled class, you will have duplicate. check and remove the .jar if its still in your build path.
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