Should you use .class files or .java files in a .jar library?
I want to take a class that I've written in a .java file and add it to a jar so I can use it as a library in my tomcat's WEB-INF/lib directory.
I understand that jars can contain .class files, .java files, or both (or just about anything else). I'm wondering what is specifically needed at runtime for my jar to be readable by the JVM. The answer to this question told me that .class files will work, but will .java files work as well?
I've found many descriptions of how to create a jar, and lots of information on the differences between .java files and .class files. What I'm lacking, I guess, is an understanding of how the jar libraries are interpreted by the JVM. Are they compiled at runtime?
Thank you
Jar files are collections of compiled java classes. You need the .class file in a jar. The .java file is you source file.
No. You'll need the byte-code (or class files) for the JVM to load the classes (although you can include the .java files, they won't be executed by the Java Runtime). A jar file is a zip file (with a tiny bit of metadata).
Also, you can add classes to your WEB-INF/classes folder (in addition, well instead of, to the WEB-INF/lib for jars, as you mentioned).
Related
my tomcat lib directory consists of a jar which i want to replace with a directory containing all the .java files contained in the above mentioned jar.
Will addition of that directory to classpath in setenv.sh solve the problem?
eg PATH=$CLASSPATH:$XYZ/folder/*
Thanks in advance.
The jar file consists of all already compiled and packed up files. If you want to get the .Java files consisted in the jar file, you could decompile them, but it's not the best solution. You should rather find the source of the library (the jar file) or ask the maker of the .jar file to share it with you. There is no easy method of decompiling the .class files (consisted in the .jar file). It is possible, and you will get working .Java files, but the code will have a lot of strange blocks and things.
I believe you need to have *.class files instead of *.java
While going throw some open source programs source code, I realized that some projects have .class and .java inside their source code files! I understand that .class is an already compiled .java file, and that .class is binary but is it possible to have compiled and uncompiled files in a project? if yes, then why to do that? what are the benefits?
long story short: am trying to study test classes in different projects, where I realized that some programs have test files under the (build) folder, where they have .class as extension! Do these classes differ in behave than test classes located under test package with .java extension?
is there anyway to decompile them?
thanks
Class files and Java files share the same subdirectory structure defined by the package structure.
IDEs like eclipse allow you to put the class files either into the same folders as the corresponding source files or into another (duplicated) folder structure.
Which of these options you choose is a matter of taste, I'd say.
It is possible, in fact if you run the java compiler (javac) without specifying the '-d' option to indicate a specific directory where to place the generated classes, by default it will place them in the same directory as the source files.
There is no conflict doing this, as the javac compiler and jvm launcher (java) are not looking at the same file extension.
However, it is generally considered a best practice to place the generated class files in a separate folder under the project: classes, bin, etc.
I had created a jar file with three classes using this cmd line:
C:\...\db>jar cvf views.jar Line.java Points.java Shapes.java
I can add the jar file to IDE but I can't import it to the code.
Another thing is the classes in the jar file are xxx.java, but when looking in another jar file i noticed that the classes are xxx.class
I don't know if that is the problem or not.
Jar is nothing but archiving (zipping)
You are clearly zipping the .java files to your jar file.
First Compile your .java files using javac
than issue your jar command on the generated .class files
Refer How to Create Jar
You should refer to .class files when creating the JAR file instead of the source .java file.
Here you can find a tutorial about JAR creating from Oracle official site.
jar is an archive tool which is just packing your compiled java files. This means that you should compile those .java files first, and than add generated .class files in jar.
I would normally expect a .jar file to contain classes rather than .java files (or at least the source could be alongside the classes)
You can zip anything into a .jar file (it's a .zip by another name - see here for more info) but I suspect the IDE is expecting .class files. Note that my IDE (Intellij) allows me to specify a jar/zip containing source, but that's usually alongside a jar containing the compiled code.
You package java source files instead of compiled class files in the jar. If you want you jar to be used in IDE you should package class files. Something like
C:\...\db>javac Line.java Points.java Shapes.java
and then
C:\...\db>jar cvf views.jar Line.class Points.class Shapes.class
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.
The best way to understand what the jar file contains is by executing this :
Go to command line and execute jar tvf jarfilename.jar
A jar file is a zip file with some additional files containing metadata. (Despite the .jar extension, it is in zip format, and any utilities that deal with .zip files are also able to deal with .jar files.)
http://docs.oracle.com/javase/8/docs/technotes/guides/jar/index.html
Jar files can contain any kind of files, but they usually contain class files and supporting configuration files (properties), graphics and other data files needed by the application.
Class files contain compiled Java code, which is executable by the Java Virtual Machine.
http://en.wikipedia.org/wiki/Java_class_file
JAR stands for Java ARchive. It's a file format based on the popular ZIP file format and is used for aggregating many files into one. Although JAR can be used as a general archiving tool, the primary motivation for its development was so that Java applets and their requisite components (.class files, images and sounds) can be downloaded to a browser in a single HTTP transaction, rather than opening a new connection for each piece. This greatly improves the speed with which an applet can be loaded onto a web page and begin functioning. The JAR format also supports compression, which reduces the size of the file and improves download time still further. Additionally, individual entries in a JAR file may be digitally signed by the applet author to authenticate their origin.
Jar file contains compiled Java binary classes in the form of *.class which can be converted to readable .java class by decompiling it using some open source decompiler. The jar also has an optional META-INF/MANIFEST.MF which tells us how to use the jar file - specifies other jar files for loading with the jar.
Jar( Java Archive) contains group of .class files.
1.To create Jar File (Zip File)
if one .class (say, Demo.class) then use command jar -cvf NameOfJarFile.jar Demo.class (usually it’s not feasible for only one .class file)
if more than one .class (say, Demo.class , DemoOne.class) then use command jar -cvf NameOfJarFile.jar Demo.class DemoOne.class
if all .class is to be group (say, Demo.class , DemoOne.class etc) then use command jar -cvf NameOfJarFile.jar *.class
2.To extract Jar File (Unzip File)
jar -xvf NameOfJarFile.jar
3.To display table of content
jar -tvf NameOfJarFile.jar
A .jar file is akin to a .exe file.
In essence, they are both executable files.
A jar file is also a archive (JAR = Java ARchive). In a jar file, you will see folders and class files. Each .class file is similar to a .o you might get from C or C++, and is a compiled java archive.
If you wanted to see the code in a jar file, download a java decompiler (located here: http://java.decompiler.free.fr/?q=jdgui) and a .jar extractor (7zip works fine).
JD-GUI is a very handy tool for browsing and decompiling JARs
A .jar file contains compiled code (*.class files) and other data/resources related to that code. It enables you to bundle multiple files into a single archive file. It also contains metadata. Since it is a zip file it is capable of compressing the data that you put into it.
Couple of things i found useful.
http://www.skylit.com/javamethods/faqs/createjar.html
http://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html
The book OSGi in practice defines JAR files as, "JARs are archive files based on the ZIP file format,
allowing many files to be aggregated into a single file. Typically the files
contained in the archive are a mixture of compiled Java class files and resource
files such as images and documents. Additionally the specification defines a
standard location within a JAR archive for metadata — the META-INF folder
— and several standard file names and formats within that directly, most
important of which is the MANIFEST.MF file."
Just check if the aopalliance.jar file has .java files instead of .class files. if so, just extract the jar file, import it in eclipse & create a jar though eclipse. It worked for me.
While learning about JAR, I came across this thread, but couldn't get enough information for people like me, who have .NET background, so I'm gonna add few points which can help persons like myself with .NET background.
First we need to define similar concept to JAR in .NET which is Assembly and assembly shares a lot in common with Java JAR files.
So, an assembly is the fundamental unit of code packaging in the .NET environment. Assemblies are self contained and typically contain the intermediate code from compiling classes, metadata about the classes, and any other files needed by the packaged code to perform its task. Since assemblies are the fundamental unit of code packaging, several actions related to interacting with types must be done at the assembly level. For instance, granting of security permissions, code deployment, and versioning are done at the assembly level.
Java JAR files perform a similar task in Java with most differences being in the implementation. Assemblies are usually stored as EXEs or DLLs while JAR files are stored in the ZIP file format.
Source of Information -> 5- Assemblies
In the tutorial I found out that jar files can be created in the following way:
jar cf jar-file input-file(s)
However, it was not clear what are the input-file(s). Is that .java files or .class files? From the examples on the same page I can assume that should be .class files.
But now it is not clear which .class files should I put there. After the compilation of .java files I have a lot of .class files. One of the reason of that is that I have a lot files like that: GameWindow$2$10class, GameWindow$2$7.class and so on. Should I include all of them into the command line for the creation of the .jar file?
To run my application I use java Game. So, my be I need to use only Game.class file when I create a .jar file? On the other hand other class files corresponds to classes used by the application.
My software use external libraries (during compilation I specify .jar files of these libraries). Will .jar file of my application be able to run on the computer which does not contain the .jar file of used library?
However, it was not clear what are the input-file(s). Is that .java files or .class files? From the examples on the same page I can assume that should be .class files.
Yes, you need to include the class files.
I have a lot files like that: GameWindow$2$10class, GameWindow$2$7.class and so on. Should I include all of them into the command line for the creation of the .jar file?
Yes, these are from inner classes; you need them as well.
To run my application I use java Game. So, my be I need to use only Game.class file when I create a .jar file?
No, class Game will use other classes, which in turn use others. You need them all.
Will .jar file of my application be able to run on the computer which does not contain the .jar file of used library?
No.
That said, creating a JAR manually is a good learning experience, but not something you'd really do in practice.
You should probably look into automating you JAR building. Check out e.g. Ant: http://ant.apache.org/
You can use a wild card to add the classes in the current directory into the JAR-file.
jar cf mynewjar.jar *.class
When you compile your source file into byte code, all classes inside that source will be generated as separate .class files, so unless your game.java has more than the Game class, the game class would be sufficient.
The Jar-file could contain any file you want, but to hold a Java-program you need at least the .class-files, so you have to include them. The Game-class you are talking about may be dependant on the other classes. You can check that: delete all .class-files and recompile only Game.java (javac Game.java). All other classes that are compiled are a dependency. All of these have to be included in the Jar-file, that your program can be run. The class-files generated, that have not a corresponding .java-file (i.e. your GameWindow$2$7.class) are inner anonymous classes, in your example a inner class of the class GameWindow. If other libraries are needed, these must be present on other computers, that your program can be run. But you can include the content of the other jars into your jar, so that all that is needed is bundled into one file.
The .jar file must contain all your classes that are needed during runtime. That includes the GameWindow$2$10 classes, they are the anonymous inner classes that you wrote in your GameWindow class.
Other .jar files are not included in your .jar file, but you can reference them using the Class-path attribute in your manifest.
regarding your external dependencies, this will NOT work unless you compile in the dependencies as .class files, or use something like FatJar http://fjep.sourceforge.net/ to add them into one big jar.
You can create a .jar file, zipping the folder with the compiled classes and renaming the file.
For instance, if your class files are located in the "game" folder, zip it to game.zip, and rename it to game.jar
This is really simple and it works!