Creating a JAR File in JDeveloper

A question on the OTN JDev forum asked how to build a jar and include all dependent libraries into the jar.
Well you can argument that ‘all’ dependent libraries are way too much and that only libraries not already available on the target should go into the jar, but this it up to the developer.
To create an archive you select the ‘Depolyment’ section in the project or application (for EAR archives) properties and click the ‘New’ button.
First declare which type of archive (JAR, WAR, EAR, ADFLIB …) you like to build. In the case described here we select ‘JAR File’ and choose name like MyJar

After clicking OK we see the default dialog to create an archive.
The part we are interested in is the ‘Library Dependencies’ which show all the libraries (which contain the actual jars) the project need to compile and run. Users might get confused with this section, as it does not put the selected jars into the archive, but is only the list of jars needed to compile or run the project.
To really add the jars to the archive you need to create a new ‘File Group’ of type ‘Libraries’

Select the location in the archive you like to put the jars (e.g. libs) and select the ‘Contributors’

where you select the libraries which should be packed into the archive. As you see there are some libraries with gray check marks. These are the libraries which are marked as ‘Deploy by default’ in the library definition. Here you can select some or all of the other libraries you like.
You can edit the ‘Deploy by default’ for your own libraries in the ‘Manage Libraries’ dialog

Now when you deploy the profile you get a jar containing the selected jars from the libraries.

14 thoughts on “Creating a JAR File in JDeveloper

  1. This worked well, but what is the path syntax for these additional jars if a reference to them needs to be made in the class-path of the manifest file?

    • This depends on the folder you specified in the ‘File Contributors’ section. In my case I used ‘libs’. The resulting class path entry in the MANIFEST.MF file look like:
      Class-Path: libs/abc.jar libs/xyz.jar

      But be aware of that this don’t enanble you to run the app directly from the jar (using java -jar xxx.jar).
      Sample:
      The manifest contains a Main-Class setting to launch our application, and entries for the supporting JAR files a.jar and b.jar.

      Manifest-Version: 1.0
      Class-Path: main/main.jar lib/a.jar lib/b.jar
      Main-Class: com.main.Main

      What happens if you run this assuming you are in the folder which contains main,a and b?

      $ java -jar myapp.jar
      A()
      B()

      Looks good, exactly what we want right? Wrong. What’s happening here is that the Class-Path entries are being resolved in the file-system, not inside the JAR file. If you move this JAR file somewhere else and try to run it, you get the following:

      $ java -jar myapp.jar
      Exception in thread “main” java.lang.NoClassDefFoundError: com/main/Main

      This is most unfortunate, and most frustrating, and pretty much unexpected by developers who follow this route.

      • Timo,
        This is exactly the problem I have. I have created the File Groups as described, but will eventually need to run this from a linux box. First I want to do it from c:\temp to make sure it all works.
        What am I to do — I cannot run a batch process in Jdeveloper at 12am at night!

        Thanks,
        Stuart

  2. I tried to deploy a jar from JDeveloper, it displays the *. jar but indicates that you can not find the libraries that add to the project, check the jar to the libraries are, but the manifest does not refer to pursue them.
    What are the steps you should follow to generate the *. jar for my application can find the libraries that have added and are in the *. jar
    I use JDeveloper 11g release 2 version (11.1.2.1.0)
    C:\JDeveloper\mywork\LookAndFeel\Look\deploy>java -jar otro.jar
    Exception in thread “main” java.lang.NoClassDefFoundError: de/javasoft/plaf/synt
    hetica/SyntheticaBlackMoonLookAndFeel
    Caused by: java.lang.ClassNotFoundException: de.javasoft.plaf.synthetica.Synthet
    icaBlackMoonLookAndFeel
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: look.LookAndFeel. Program will exit.

    • Firat of all you need to understand that you can’t start an app from a jar if the app needs other jars inside the application jar. If you just want to use java -jar xyz.jar then you must put all classes into your jar or make hte needed jars available via the class path.
      So the question is where is the missing class defined?

      • Timo, how could please make sure the path is correctly configured in the JDeveloper IDE?, this only happens to me with third-party libraries, with the standard jar libraries are generated without any problem.
        Thanks for your time

      • Now you are confusing me. A jar is a jar. What do you mean by ‘it only happens with third-party jars, but not with standard jars’?

  3. When I create a program with JDeveloper or Java standard library of JDeveloper and do a deploy, generate your *. jar and I can run on any client.
    When I add an external library, such as the library mail.jar, mysql-connector-java-1.5.16-bin.jar, etc.. my program from the JDeveloper IDE the program works, but when I do the deploy, the program runs because you can not find the libraries, although the libraries are in the same *. jar.
    If you like so you can better understand your shipping a simple program I made in JDeveloper and that from the ide works fine, but when I generated the jar gives me the error in a quote reproduced above.
    If you can send your mail, I would greatly appreciate, mine is jperedoapb@yahoo.com.
    Thank you very much for your time and patience

    • No need for a sample. This behavior is normal, even if it’s frustrating. Read my answer to comment #1 in this article. It exactly describes your use case.
      As I said before, putting jars into your own jar will not make your jar runnable as
      java -jar your.jar
      Not even if you set up the manifest file correctly. For this to work, you need to write your own class loader which looks for jars inside other jars. As for now the normal class loader we all use doesn’t do this. One solution is to unzip the your jar into an empty folder (your jar is a zip file with an other suffix), and then run it as java -jar your.jar. Then the jars you have packed inside the jar are available outside the jar and the class loader will detect them.

  4. Dear Timo,
    These are the steps I’ve done it again.
    1.- Project properties
    2.- Libraries and classpath
    3.- Add Library
    4.- User
    5.- new
    6.- Library Name: look
    Location User
    Class Path
    7.- Puch botton “Add Entry
    8.- c:\oracle\Middleware\jdeveloper\jdev\lib\
    select archive’s jar
    9.- Push OK
    10.- Pusk OK
    followed your recommendations further in this article for deployment:
    “c:\work\java -jar demo4.jar
    Exception in thread “main” java.lang.NoClassDefFoundError: de/javasoft/plaf/synthetica/SyntheticaBlackMoonLookAndFeel
    Caused by: java.lang.ClassNotFoundException: de.javasoft.plaf.synthetica.SyntheticaBlackMoonLookAndFeel”
    in the main program “import” are properly

  5. Pingback: java - creare un file di libreria di java

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.