Creating jar from java file

How do I make a JAR from a .java file?

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. The MyApp.java starts like a class with package, imports then public class MyApp and has a main function, but it’s still .java file! I’m writing it in JDeveloper 11g if it helps. Any ideas how to make a JAR from these files?

Agree with Chuck. Take a look at ANT. IDEs are great for developing, but you need to understand how things works. java files are compiled via javac to .class files, after this, .class files can be packaged into a jar with the jar command. (Ant has tasks for doing this, even jdeveloper has some wizard for doing this).

JDeveloper itself should have the capability to create jar files. A quick google search comes up with a howto here: tompeez.wordpress.com/2011/06/01/…

8 Answers 8

Go to the directory where you have your .java files

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

I’ve tried compiling from the command line, but I get an error: Frame1.java:23: package oracle.jdeveloper.layout does not exist How do I solve that? Sorry for the newbie questions.

you have to build the classpath referring all the libraries you’re referencing in your classes. Check with your jdeveloper installation, there should be some ant build example (build.xml file) and at least a couple of good tutorials on how to use jDeveloper with ANT on Oracle’s site.

I’ve added an option to my jar command to automatically add the Main-Class to manifest. jar cfe Main.jar Main * (I’ve got confused though, I’ve had to put the Main-Class name after the output file name.)

I’d like to point out: You have to move to build directory . If you try jar cvf YourJar.jar ./build/* , you will have trouble running your jar file later.

javac MyApp.java jar -cf myJar.jar MyApp.class 

Sure IDEs avoid using command line terminal

The above worked, however I needed to specify the .class of myApp. So it was jar -cf myJar.jar myApp.class

Ok this is the solution I would have liked to find, instead here I write it:

First create the directory structure corresponding to the package defined for the .java file, if it is my.super.application create the directory my and inside it super and inside it the .java file App.java

javac -cp /path/to/lib1.jar:/path/to/lib2.jar path/to/my/super/App.java 

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

jar -cvfe App.jar App my/app/ 

the above will create the application with its corresponding Manifest indicating the App as the main class.

Читайте также:  Среднеквадратическое отклонение python pandas

Including the required libraries inside the jar file is not possible using java or jar command line parameters.

  1. manually extract libraries to the root folder of the jar file
  2. use an IDE such as Netbeans and insert a rule inside post-jar section of nbproject/build-impl.xml to extract the libraries inside the jar. See below.
        

the file.reference names are found inside project.properties file after you added the libraries to the Netbeans IDE.

Источник

Creating a JAR File

The basic format of the command for creating a JAR file is:

jar cf jar-file input-file(s) 

The options and arguments used in this command are:

  • The c option indicates that you want to create a JAR file.
  • The f option indicates that you want the output to go to a file rather than to stdout.
  • jar-file is the name that you want the resulting JAR file to have. You can use any filename for a JAR file. By convention, JAR filenames are given a .jar extension, though this is not required.
  • The input-file(s) argument is a space-separated list of one or more files that you want to include in your JAR file. The input-file(s) argument can contain the wildcard * symbol. If any of the «input-files» are directories, the contents of those directories are added to the JAR archive recursively.

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:

jar cmf jar-file existing-manifest input-file(s) 

Warning: The manifest must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

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:

jar cvf TicTacToe.jar TicTacToe.class audio images

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:

adding: TicTacToe.class (in=3825) (out=2222) (deflated 41%) adding: audio/ (in=0) (out=0) (stored 0%) adding: audio/beep.au (in=4032) (out=3572) (deflated 11%) adding: audio/ding.au (in=2566) (out=2055) (deflated 19%) adding: audio/return.au (in=6558) (out=4401) (deflated 32%) adding: audio/yahoo1.au (in=7834) (out=6985) (deflated 10%) adding: audio/yahoo2.au (in=7463) (out=4607) (deflated 38%) adding: images/ (in=0) (out=0) (stored 0%) adding: images/cross.gif (in=157) (out=160) (deflated -1%) adding: images/not.gif (in=158) (out=161) (deflated -1%)

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:

jar cvf0 TicTacToe.jar TicTacToe.class audio images

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:

jar cf ImageAudio.jar -C images . -C audio .

The -C images part of this command directs the Jar tool to go to the images directory, and the . following -C images directs the Jar tool to archive all the contents of that directory. The -C audio . part of the command then does the same with the audio directory. The resulting JAR file would have this table of contents:

META-INF/MANIFEST.MF cross.gif not.gif beep.au ding.au return.au yahoo1.au yahoo2.au

By contrast, suppose that you used a command that did not employ the -C option:

jar cf ImageAudio.jar images audio

The resulting JAR file would have this table of contents:

META-INF/MANIFEST.MF images/cross.gif images/not.gif audio/beep.au audio/ding.au audio/return.au audio/yahoo1.au audio/yahoo2.au

Источник

How can I create a .jar file?

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?

6 Answers 6

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?

That said, creating a JAR manually is a good learning experience, but not something you’d really do in practice.

Источник

Оцените статью