Java how to compile to jar
Java how to compile to jar
Creating a JAR File
The basic format of the command for creating a JAR file is:
The options and arguments used in this command are:
The c and f options can appear in either order, but there must not be any space between them.
This command will generate a compressed JAR file and place it in the current directory. The command will also generate a default manifest file for the JAR archive.
The metadata in the JAR file, such as the entry names, comments, and contents of the manifest, must be encoded in UTF8.
You can add any of these additional options to the cf options of the basic command:
Option | Description |
---|---|
v | Produces verbose output on stdout while the JAR file is being built. The verbose output tells you the name of each file as it’s added to the JAR file. |
0 (zero) | Indicates that you don’t want the JAR file to be compressed. |
M | Indicates that the default manifest file should not be produced. |
m | Used to include manifest information from an existing manifest file. The format for using this option is: |
-C | To change directories during execution of the command. See below for an example. |
When you create a JAR file, the time of creation is stored in the JAR file. Therefore, even if the contents of the JAR file do not change, when you create a JAR file multiple times, the resulting files are not exactly identical. You should be aware of this when you are using JAR files in a build environment. It is recommended that you use versioning information in the manifest file, rather than creation time, to control versions of a JAR file. See the Setting Package Version Information section.
An Example
Let us look at an example. A simple TicTacToe applet. You can see the source code of this applet by downloading the JDK Demos and Samples bundle from Java SE Downloads. This demo contains class files, audio files, and images having this structure:
TicTacToe folder Hierarchy
The audio and images subdirectories contain sound files and GIF images used by the applet.
You can obtain all these files from jar/examples directory when you download the entire Tutorial online. To package this demo into a single JAR file named TicTacToe.jar, you would run this command from inside the TicTacToe directory:
The audio and images arguments represent directories, so the Jar tool will recursively place them and their contents in the JAR file. The generated JAR file TicTacToe.jar will be placed in the current directory. Because the command used the v option for verbose output, you would see something similar to this output when you run the command:
You can see from this output that the JAR file TicTacToe.jar is compressed. The Jar tool compresses files by default. You can turn off the compression feature by using the 0 (zero) option, so that the command would look like:
You might want to avoid compression, for example, to increase the speed with which a JAR file could be loaded by a browser. Uncompressed JAR files can generally be loaded more quickly than compressed files because the need to decompress the files during loading is eliminated. However, there is a tradeoff in that download time over a network may be longer for larger, uncompressed files.
The Jar tool will accept arguments that use the wildcard * symbol. As long as there weren’t any unwanted files in the TicTacToe directory, you could have used this alternative command to construct the JAR file:
Though the verbose output doesn’t indicate it, the Jar tool automatically adds a manifest file to the JAR archive with path name META-INF/MANIFEST.MF. See the Working with Manifest Files: The Basics section for information about manifest files.
In the above example, the files in the archive retained their relative path names and directory structure. The Jar tool provides the -C option that you can use to create a JAR file in which the relative paths of the archived files are not preserved. It’s modeled after TAR’s -C option.
As an example, suppose you wanted to put audio files and gif images used by the TicTacToe demo into a JAR file, and that you wanted all the files to be on the top level, with no directory hierarchy. You could accomplish that by issuing this command from the parent directory of the images and audio directories:
By contrast, suppose that you used a command that did not employ the -C option:
The resulting JAR file would have this table of contents:
I was writing a simple program using a Java application (not application that has projects, but application within a project; .java) that has a single frame. Both of the files are .java so I can’t write a manifest needed by the JAR.
Any ideas how to make a JAR from these files?
8 Answers 8
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Open a command prompt.
Create a directory build
Run java compilation from the command line
if there are no errors, in the build directory you should have your class tree
move to the build directory and do a
For adding manifest check jar command line switches
Simply with command line:
Sure IDEs avoid using command line terminal
Ok this is the solution I would have liked to find, instead here I write it:
then from command line:
Notice the above will include multiple libraries, if under windows use «,» to separate multiple files otherwise under GNU/Linux use «:» To create a jar file
the above will create the application with its corresponding Manifest indicating the App as the main class.
Including the required libraries inside the jar file is not possible using java or jar command line parameters.
and I get ‘Unable to access jarfile cf’. I’m doing something wrong but I don’t know what. I’m also using Eclipse IDE if that means something.
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Additionally, if you want to make it executable, you need to indicate an entry point (i.e., a class with public static void main(String[] args) ) for your application. This is usually accomplished by creating a manifest file that contains the Main-Class header (e.g., Main-Class: myClass ).
However, as Mark Peters pointed out, with JDK 6, you can use the e option to define the entry point:
Finally, you can execute it:
See also
Sine you’ve mentioned you’re using Eclipse. Eclipse can create the JARs for you, so long as you’ve run each class that has a main once. Right-click the project and click Export, then select «Runnable JAR file» under the Java folder. Select the class name in the launch configuration, choose a place to save the jar, and make a decision how to handle libraries if necessary. Click finish, wipe hands on pants.
Which reads: «create verbose jarFilename manifestFilename», followed by the files you want to include.
Note that the name of the manifest file you supply can be anything, as jar will automatically rename it and put it into the right place within the jar file.
then we create jar file by this command :
Let we have one package named one and every class are inside it. then we create jar file by this command :
then we open manifest.txt inside directory META-INF in anyname.jar file and write
in third line., then we run jar file by this command :
How to make an executable JAR file?
I have a program which consists of two simple Java Swing files.
How do I make an executable JAR file for my program?
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
A jar file is simply a file containing a collection of java files. To make a jar file executable, you need to specify where the main Class is in the jar file. Example code would be as follows.
Compile your classes. To make a jar, you also need to create a Manifest File ( MANIFEST.MF ). For example,
Place the compiled output class files (JarExample.class,JarExample$1.class) and the manifest file in the same folder. In the command prompt, go to the folder where your files placed, and create the jar using jar command. For example (if you name your manifest file as jexample.mf)
It will create executable jarexample.jar.
In Eclipse you can do it simply as follows :
Right click on your Java Project and select Export.
Select the Launch Configuration and choose project file as your Main class
Select the Destination folder where you would like to save it and click Finish.
Here it is in one line:
where MainClass is the class with your main method, and package is MainClass ‘s package.
This answer inspired by Powerslave’s comment on another answer.
If you use maven, add the following to your pom.xml file:
It’s too late to answer for this question. But if someone is searching for this answer now I’ve made it to run with no errors.
Now go to the command line [ CMD ] in your project and type mvn package and it will generate a jar file as something like ProjectName-0.0.1-SNAPSHOT.jar under target directory.
How to compile, package and run a Java program using command-line tools (javac, jar and java)
— The Java source file is under a package.
— There’s an external library.
— The JAR file is an executable JAR.
The Java program we use in this tutorial is a JDBC client that connects to a MySQL database named Students and inserts a row into the table students. Here’s the MySQL script to create the database:
You need to download the JDBC driver library for MySQL in order to run the program.
1. Organizing Directories Structure
The way you organize files is very important, as it affects the readability, maintainability and extensibility of a program when it evolves over times. Therefore it’s recommended to follow a common, standard directory structure as shown in the screenshot above. Let’s understand each directory in details:
2. Compile the program using javac command
In case the source files reference third-party library (JAR) files, use the -cp option and specify the JAR file. For example:
If there are multiple JAR files, they must be separated by semicolon like this:
3. Create executable JAR file using jar command
The first line specifies the class will be called when the JAR file gets executed. This class must have main() method. You see the StudentsInsert class is specified with its fully qualified name (including package name).
The second line specifies the third-party libraries referenced by the program. Here we specify the MySQL Connector library JAR file. In case you refer multiple JAR files, separate them by spaces like this:
Class-Path: lib\mysql-connector-java-5.1.21-bin.jar lib\log4j-1.2.17.jar
Here we specify the paths relative to the JAR file being created.
NOTE: There must be a blank line at the end of the manifest file, otherwise it won’t work.
Now type the following command to package the JAR file:
The c option is for creating a JAR file.
The f option specifies the JAR file name.
The m option specifies where to look at the manifest file.
You should see the StudentsInsert.jar file created. The whole directory structure would look like this:
NOTE: If the JAR file doesn’t depend on any third-part libraries, we don’t have to create the manifest file. Instead, we can use the e option to specify the main class.
4. Run the program using java command
Enter the inputs when requested. The following screenshot depicts the running of this program:
We hope you find this tutorial helpful for your Java development using command line tools. You can download the whole project under the Attachments section to experiment yourself.
Other Java Tools Tutorials:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.
Источники информации:
- http://stackoverflow.com/questions/9941296/how-do-i-make-a-jar-from-a-java-file
- http://stackoverflow.com/questions/4597866/java-creating-jar-file
- http://stackoverflow.com/questions/5258159/how-to-make-an-executable-jar-file
- http://www.codejava.net/java-core/tools/how-to-compile-package-and-run-a-java-program-using-command-line-tools-javac-jar-and-java