When I clean and build a project in NetBeans, the .jar file appears in the dist folder, like it's supposed to. But what if I have multiple files under the project? What happens to those files? E.g. I have a Game project, and under it are the different characters(knight, rogue, etc.) but I only see a game.jar file when I clean and build, I want to know what happens to the individual files. Thanks
Those files should be in the jar file as compiled .class files. It's easy to double check what's in the jar file since it's in zip format. You can use a program like 7-Zip to open it, or rename it to the zip extension (e.g. from mygame.jar to mygame.zip) and whatever OS you're using probably has some way to open it.
When you open or extract the jar file you'll find the compiled class files in a directory structure that reflects your package structure. For example, if you have Knight.java in the directory src/game/characters/Knight.java in the jar file you'll find something like classes/game/characters/Knight.class.
The name "jar" is an abbreviation of "Java archive". It stores all the classes and other resources (for example, images) in a project.
The classes you have defined in .java files will be compiled into .class files - these are contained in the .jar file.
All resources get compiled into the JAR file. If you want a separate JAR for the resources, you'll need to split the project into two maven projects: one jar for the code, one for the resources. You can then create a third project that would generate a distribution.
That's a lot of work, though. It's.a lot easier tO keep everything in one JAR unless you have explicit dynamic loading requirements.
Related
So, at work, we have a java application that we license to clients. Frequently, our clients require very specific things that we don't want in the core product, so we make those changes, package them in a jar, and place the jar in the libs directory of the branch in our VCS that we use for that client.
We want to start packaging the .java files in the jar along with the .class files, just as an extra backup for our source code. The thing is, in our main codebase, all our .class files are in a filetree starting at a folder called "out", and all our .java files are in a filetree starting at a folder called "development".
So, I don't want the folder that's unique to both of them to be where I run the jar command from the command line. The reason for this is the jar utility builds a folder structure that mirrors what it sees when it builds the jar, and I need my folder structure to start at com.companyname.etc... for the class file.
Is there any way to specify totally divergent filepaths when creating a jar? Or to specify the folder structure that should be the destination when the files are dropped in the jar?
Thanks in advance.
I would like to "dump" or "unpack" a library project's files into the main project, which would let me customize the library's contents for that project. Is that possible, and what is a way to do so? Can I just copy the entire library project's src/, res/, and manifest file into the libs/ folder of the main project?
(Please note that I do not want to touch the files from the original library project because I have other projects that are using it.)
If your library is a .jar containing .java source files, then you can unpack it using, eg. unzip, and move the contents of its src and res directories into your project's src and res directories.
If, however, this library contains only compiled Java .class files, there's not much you will be able to do with them. Unless you want to try to decompile them, the library must be used as-is.
I think that is better change the project name of the library and make a copy of this. This allow you to modify the files and dont mix it with your actual project.
In my Java application,I want to add a directory inside my package structure. This directory contains some XML files. Since these files cannot be compiled, I want to copy these files during war make process. How to write a makefile for this?
Without knowing precisely what your directory structure looks like, I would simply say that you handle this in the same fashion as putting your class files into a .war file. A .war file is little more than a .zip file and doesn't really care what you put into it.
Going forwards, you should dump the makefile and investigate something more Java-centric. I would suggest Maven for its convention-directed approach. For example, the war goal of Maven will simply assume a directory structure and successfully build a .war file of suitable contents within those directories.
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!