Jar file generation

I have successfully generated a my_program.jar file of my Java application using artifacts.
When I start the my_program.jar file it works.

Now I have written a User Guide and generated a user_guide.jar file of it (by another application).
I can successfully access this user_guide.jar file from within my application by a construction similar to:

    ClassLoader cl = UserGuideGUIBuilder.class.getClassLoader();
    URL url = HelpSet.findHelpSet(cl, "User_Guide_helpset.hs");
    helpViewer = new JHelp(new HelpSet(cl, url));
    helpViewer.setCurrentID("User_Guide_Intro");
    JFrame frame = new JFrame();
    frame.setTitle("User Guide");
    frame.setSize(1300,800);
    frame.getContentPane().add(helpViewer);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setVisible(true);

as I have incorporated the user_guide.jar file into the dependencies.

When I start my program from Intellij IDEA all works fine and I have access to the User Guide.

Now my question: How can I incorporate the user_guide.jar into the my_program.jar?

Thank you for any hints.

9 comments
Comment actions Permalink

IIRC, you have to add  a classpath entry pointing to your help jar (and all the other library jars your application needs)  in the manifest file of your my_program.jar.

See here the howto:

Adding Classes to the JAR File's Classpath: http://download.oracle.com/javase/tutorial/deployment/jar/downman.html
Working with Manifest Files: http://download.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

0
Comment actions Permalink

Thank you for your answer, but this did not solve the problem.
I don't have all the other jar's in the manifest file and the generated jar works anyway although the manifest file does not contain any classpath.

I can immediately see that the user_guide.jar is not integrated from the size of the output.jar.
My app is 4 MB and the user_guide.jar is 7 MB so the generated jar should be around 11 MB, but it is still 4 MB.

I can't see a reason why I should handle the user_guide.jar differently from all the others (jfreechart.jar, jcommon.jar etc.)

Thank you also for the links. For me it would be desirable to just learn one interface (Intellij IDEA) before digging into command line stuff.

I'm sure it's a very simple thing but can anybody see where is the problem ???

0
Comment actions Permalink

Hi Alexander,

Generally, it looks like our problem here is ambiguos meaning of 'incorporate one jar into another' statement.

Please clarigy the following:

  • am I right assuming that you want to create an 'executable jar' (the one that is expected to be launched via 'java -jar' command)
  • as far as I see you said that there are other third-party jars that are 'incorporated to your jar'? It's not clear why the same approach is not used for 'user_guide.jar' then;


Regards, Denis

0
Comment actions Permalink

Hi Denis,

It's a kind of jar file that can be double clicked on a windows PC in order to run it like an exe file. It's one I can make an exe file from with a tool (launch4j), and then distribute it to another PC, where it should run just as is.

Yes, I don't want to handle it differently. I just wonder why the user_guide.jar is not automatically treated the same way as jfreechart.jar etc.

In the Project Structure, Modules, Dependencies I have included the user_guide.jar as a Single-Entry Module Library, just as the others.

I have made an JAR artifact and chosen 'Build on make'. I have created the manifest file. The Manifest file consists of the single line: Manifest-Version: 1.0.

I have not drag'n'dropped any elements from the right side to the left (should I do this ?) in the Artifacts screen.

How and when the JAR is generated is still a mystery to me. Do I have to Run the Program, Make Project, Compile Project, Generate Ant Build, Build Artifact or Create MSI for Project ?

At the moment only the last option creates a JAR file every time.

In the Help File there is a section 'Build Jars' which refers to a 'Build | Build JAR' entry. I cannot find this option in my version of Intellij (9.0.1) Where is this ???

I'm sorry to ask so much questions but I can't find the answers in the manual. This is a kind of manual with entries like this "If you want to add an element, then press add element'. It would never say WHY.

Thanks you all for your help!

0
Comment actions Permalink

Hi Alexander,

Ok, so, the task is to create an executable jar that don't have dependencies to external jars.

The trick is to have a jar with valid manifest setup and all necessary *.class files included to it. Feel free to get more information about that.

I'm not sure about the code you use to load the classes (
    URL url = HelpSet.findHelpSet(cl, "User_Guide_helpset.hs");
    helpViewer = new JHelp(new HelpSet(cl, url));
    helpViewer.setCurrentID("User_Guide_Intro");
)
In theory it may handle non-inlined jars packed into another jar.

Anyway, it seems that we have a classic example of leaky abstraction here, i.e. you know what do you want to get (*.jar file that may be executed by double-click from windows explorer) but we have no information on how that is expected to work and what's wrong with the current setup. We should identify the problem before thinking on how to fix it.

About IDEA artifact documentation - you can check the concept here.

Regards, Denis

0
Comment actions Permalink

Denis,

I'm looking for a simple JAR generation feature.

I have now seen that many of us had similar problems since Intellij 9.

My question of 'Where is Build | Build Jar' is resolved: It was a feature of Intellij 8 an is sadly gone.

Artifacts are not as simple and I have not found a straightforward method so far for Intellij 9.

I have set up a new artifact and this time I am even not able to generate a JAR at all (without the user_guide.jar).

It's a nightmare ... Is Intellij 9 able to build the build.xml file or is this up to me ?

If anyone can give a step by step procedure for a simple Java standalone (no Web) application I would highly appreciate it.

Alexander

0
Comment actions Permalink

To create a jar artifact containing your application you can do the following:
1) open File | Project Structure
2) select 'Artifacts' item
3) press '+' button and select 'Jar' item
4) select ' compile output' item in 'Available elements' tree and double-click on it, it will be placed inside 'jar' node in the left tree
5) press 'OK' in the dialog and invoke Build | Build Artifact action

IDEA will produce a jar file containing class files of your module. If you want to place additional resources inside the jar you can drag'n'drop
corresponding items from 'Available elements' tree or use '+' button above 'Output layout' tree.

Denis,

>

I'm looking for a simple JAR generation feature.

>

I have now seen that many of us had similar problems since Intellij 9.

>

My question of 'Where is Build | Build Jar' is resolved: It was a feature of Intellij 8 an is sadly gone.

>

Artifacts are not as simple and I have not found a straightforward method so far for Intellij 9.

>

I have set up a new artifact and this time I am even not able to generate a JAR at all (without the user_guide.jar).

>

It's a nightmare ... Is Intellij 9 able to build the build.xml file or is this up to me ?

>

If anyone can give a step by step procedure for a simple Java standalone (no Web) application I would highly appreciate it.

>

Alexander

>

---
Original message URL: http://devnet.jetbrains.net/message/5270607#5270607



--
Nikolay Chashnikov
Software Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

0
Comment actions Permalink

I have found it after all. For thos who are interested:

In Intellij 9, goto:  Window - Tool Windows - Ant Build

On the context menu 'Create MSI for Project xxx' choose 'Jump to Source'

You will see the build.xml file (if this file does not exist, right click in the project pane, choose New and create it)

In this file you see a list of jar files to be included. In this list, in my case, the Javahelp jh.jar and the user_guide.jar were missing. That was the whole story.

Intellij 9 apparently is not so intelligent to create it based on the dependencies. My build.xml looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_runnable_jar" name="Create MSI for Project xxx">
    <!--ANT 1.7 is required -->
    <target name="create_runnable_jar">
        <jar destfile="yyy.jar" filesetmanifest="mergewithoutmain">
            <manifest>
                <attribute name="Built-By" value="ipi"/>
                <attribute name="Main-Class" value="zzz.Main"/>
                <attribute name="Class-Path" value="."/>
            </manifest>

            <fileset dir="out/production/I-GEN-Trader"/>
            <fileset dir="out/production/TWS"/>
            <zipfileset excludes="META-INF/*.SF" src="lib/substance.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="lib/miglayout-3.7.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="lib/jcommon-1.0.16.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="lib/jfreechart-1.0.13.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="manual/out/user_guide.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="lib/jh.jar"/>
        </jar>
    </target>
</project>

When you have that, just double click on 'create runnable.jar' (expand 'Create MSI for Project xxx') and the desired yyy.jar will be created.

After a day of fiddling around with this I'm feeling like the Winnebago Man or like Roddick loosing to Federer. His commentary was:
f ... f...  f... (17 times)

Alexander

0
Comment actions Permalink

Although I have found the solution that works for me (see my previous post), I have tried your solution exactly as you suggested but with no success.

The jar file created is only 600 KB (instead if 12 MB) and upon execution there is a Java Execution Error window popping up.

But as I said I am happy with my solution now.

0

Please sign in to leave a comment.