Java programming jar 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

Читайте также:  How to create zip files in java

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.

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.

Источник

Using JAR Files: The Basics

JAR files are packaged with the ZIP file format, so you can use them for tasks such as lossless data compression, archiving, decompression, and archive unpacking. These tasks are among the most common uses of JAR files, and you can realize many JAR file benefits using only these basic features.

Even if you want to take advantage of advanced functionality provided by the JAR file format such as electronic signing, you’ll first need to become familiar with the fundamental operations.

To perform basic tasks with JAR files, you use the Java Archive Tool provided as part of the Java Development Kit (JDK). Because the Java Archive tool is invoked by using the jar command, this tutorial refers to it as ‘the Jar tool’.

As a synopsis and preview of some of the topics to be covered in this section, the following table summarizes common JAR file operations:

AppletClassName.class archive width=width height=height> 

This section shows you how to perform the most common JAR-file operations, with examples for each of the basic features:

Creating a JAR File

This section shows you how to use the Jar tool to package files and directories into a JAR file.

Viewing the Contents of a JAR File

You can display a JAR file’s table of contents to see what it contains without actually unpacking the JAR file.

Extracting the Contents of a JAR File

You can use the Jar tool to unpack a JAR file. When extracting files, the Jar tool makes copies of the desired files and writes them to the current directory, reproducing the directory structure that the files have in the archive.

Updating a JAR File

This section shows you how to update the contents of an existing JAR file by modifying its manifest or by adding files.

Running JAR-Packaged Software

This section shows you how to invoke and run applets and applications that are packaged in JAR files.

Additional References

The documentation for the JDK includes reference pages for the Jar tool:

Источник

Lesson: Packaging Programs in JAR Files

The Java™ Archive (JAR) file format enables you to bundle multiple files into a single archive file. Typically a JAR file contains the class files and auxiliary resources associated with applets and applications.

The JAR file format provides many benefits:

  • Security: You can digitally sign the contents of a JAR file. Users who recognize your signature can then optionally grant your software security privileges it wouldn’t otherwise have.
  • Decreased download time: If your applet is bundled in a JAR file, the applet’s class files and associated resources can be downloaded to a browser in a single HTTP transaction without the need for opening a new connection for each file.
  • Compression: The JAR format allows you to compress your files for efficient storage.
  • Packaging for extensions: The extensions framework provides a means by which you can add functionality to the Java core platform, and the JAR file format defines the packaging for extensions. By using the JAR file format, you can turn your software into extensions as well.
  • Package Sealing: Packages stored in JAR files can be optionally sealed so that the package can enforce version consistency. Sealing a package within a JAR file means that all classes defined in that package must be found in the same JAR file.
  • Package Versioning: A JAR file can hold data about the files it contains, such as vendor and version information.
  • Portability: The mechanism for handling JAR files is a standard part of the Java platform’s core API.

This lesson has four sections:

Using JAR Files: The Basics

This section shows you how to perform basic JAR-file operations, and how to run software that is bundled in JAR files.

Working with Manifest Files: The Basics

This section explains manifest files and how to customize them so you can do such things as seal packages and set an application’s entry point.

Signing and Verifying JAR Files

This section shows you how to digitally sign JAR files and verify the signatures of signed JAR files.

This section introduces you to some of the JAR-handling features of the Java platform. The JAR file format is an important part of the Java platform’s extension mechanism. You can learn more about that aspect of JAR files in the The Extension Mechanism trail of this tutorial.

Questions and Exercises: JAR

Test what you’ve learned about JAR.

Additional References

The documentation for the Java Development Kit (JDK) includes information about the Jar tool:

Источник

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