4

I have built a jar file using Netbeans and it's working good in my system. But I want to make jar files which is capable of running in all systems, which has JRE and it should work correctly even the classpath is not set in that system.

package circle;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Circle {


    public static void main(String[] args) {


        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Circle().createAndShowGUI(); 
            }
        });
    }

    private void createAndShowGUI() {
        JFrame f = new JFrame("Swing Paint Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        f.add(new MyPanel());
        f.pack();
        f.setVisible(true);

    }

    class MyPanel extends JPanel {

        public MyPanel() {

        setBorder(BorderFactory.createLineBorder(Color.black));

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
               startX=e.getX();
               startY=e.getY();
            }
        });

        addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
               X=e.getX();
               Y=e.getY();
               repaint();
            }
        });

    }



        public Dimension getPreferredSize() {
        return new Dimension(250,200);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);       
        //g.setColor(Color.RED);
        //g.fillRect(squareX,squareY,squareW,squareH);
        g.setColor(Color.BLACK);
        g.drawOval(startX,startY,X-startX,Y-startY);
        g.fillOval(startX,startY,X-startX,Y-startY);
    }  


    }

        private int startX,startY,X,Y;
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 4
    Have you tested your Jar file on other systems? Are you sure that it doesn't work on these? – Hovercraft Full Of Eels Sep 01 '12 at 16:07
  • @Jeffrey: thanks for the edit to Kalyan's post. For many of the volunteers on this site, English is not their first or even second language. It's hard enough trying to figure out what the original poster wants or is asking, they should strive to avoid making it harder by avoiding use of non-standard abbreviations. – Hovercraft Full Of Eels Sep 01 '12 at 16:09
  • 2
    Make sure you add the "Main-Program" argument to MANIFEST.MF – Chris Sep 01 '12 at 16:20
  • You should use a unique package name, to avoid name-space collisions when your jar is used in other projects. Recommendated name-space scheme is to use a reverted domain name scheme, e.g. com.google.maps or org.apache.tomcat ... you don't have to actually own the domain, but just circle might be not enough. – Ridcully Sep 01 '12 at 16:27
  • To provide a very easy experience for the end user, launch the frame using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). It offers auto-update, desktop-integration, splash screens, .. JWS works on any OS for which there is a J2SE, and is supplied and supported by Oracle. Classes need to be in a Jar (and possibly digitally signed) to be deployed using JWS. However it does not require a manifest, since those details are provided in the launch file (the JWS based `.jnlp` file type). – Andrew Thompson Sep 01 '12 at 17:10

3 Answers3

2

You can use an ant script to build the runnable JAR.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar" name="Create Runnable Jar for Project SimpleApp with libraries in sub-folder">
    <!--ANT 1.7 is required -->
    <target name="create_run_jar">
        <jar destfile="C:/Workspaces/SimpleApp/SimpleApp.jar">
            <manifest>
                <attribute name="Main-Class" value="SimpleApp"/>
                <attribute name="Class-Path" value=". SimpleApp_lib/lib1.jar SimpleApp_lib/lib2.jar"/>
            </manifest>
            <fileset dir="C:/Workspaces/SimpleApp/bin"/>
        </jar>
        <delete dir="C:/Workspaces/SimpleApp/SimpleApp_lib"/>
        <mkdir dir="C:/Workspaces/SimpleApp/SimpleApp_lib"/>
        <copy file="C:/path/to/lib/lib1.jar" todir="C:/Workspaces/SimpleApp/SimpleApp_lib"/>
        <copy file="C:/path/to/lib/lib2.jar" todir="C:/Workspaces/SimpleApp/SimpleApp_lib"/>
    </target>
</project>

In this example, the project's SimpleApp depends on two libs: lib1.jar and lib2.jar, which are output to bin with a MANIFEST.MF having the attributes specified.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Roman C
  • 49,761
  • 33
  • 66
  • 176
2

The following posting has a brief explanation of how to create an executable jar using ANT:

This posting explains how the manifestclasspath task can assist with creating the classpath manifest entry (making the construction of executable jars more robust and less error prone):

Finally a more complex example demonstrating the use of ivy to manage your project's 3rd party dependencies when creating an executable jar:

Community
  • 1
  • 1
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
1

Simply You can create a JAR-file by executing following command:

jar -c excel.jar MANIFEST.MF *.class

The MANIFEST.MF-file should contain following line:

Main-Class: createExcel

But consider following tips too :

There are several ways:

  1. Create a jar file and put your classes (without dependencies) there. Use some tool (any IDE has it) to do this and specify class with main function. You can also do it manually from command-line. When user want to run it he should specify classpath and all dependencies should be in that classpath.

  2. Create the same jar and create .bat or .sh file in which set classpath and run your jar.

  3. Create cross-platform installer with some special tool (but good tools aren't free).

Remember that Netbeans can help you a lot ;)

Mehdi
  • 4,396
  • 4
  • 29
  • 30
  • *"good tools aren't free"* Aah.. coming at the 'JWS is not good' from a different direction this time, I see. ;) – Andrew Thompson Sep 01 '12 at 17:05
  • Netbeans is open source and a great tool, And also GetDown is open source and a great tool too ;) – Mehdi Sep 01 '12 at 17:08
  • [`javax/jnlp/ServiceManager.java`](http://jdk-source-code.googlecode.com/svn/trunk/jdk6u21_src/deploy/src/javaws/share/classes/javax/jnlp/ServiceManager.java). Keep throwing up these silly justifications for using value laden terms such as 'good/better' and I will need to serially down-vote the answers. Please save me the effort. If you wish to point out that 'Feature X is provided by tool A but not B' go for it, but leave out the value judgements. *Then we can have a discussion about the things provided by JWS that you won't find in **any** 3rd party tool.* – Andrew Thompson Sep 01 '12 at 17:21
  • Common man... ;) if you want to show that your idea is opposite of others, just tell your idea and compare it with other tools logically ;) then community will judge! I'm able to upgrade JRE with GetDown, So if you can do that with JWS, tell me and I will appreciate it and thank. and my participation here is not for upvotes, it is just because I love sharing knowledge and learning from knowledgeable people like you. we can have a friendly chat about features of GetDown and JWS or other technologies and share the result here with the community. So why calling each other silly and condemning! – Mehdi Sep 01 '12 at 18:27
  • *"I'm able to upgrade JRE with GetDown"* Irrelevant. The JRE is auto-updating. OTOH JWS provides the ability to specify an apps. JRE version requirements. So if a JWS app. needs a particular micro-version, it can specify it. – Andrew Thompson Sep 02 '12 at 02:14
  • *"The JRE is auto-updating"* but not when your clients are not able to connect to Internet! for security reasons most of our clients can not connect to Internet and our application is deployed in about 5000 places. Most of the time we have the best technical solutions but for some policies like that we can not use them. Maybe thats my problem that I'm always involved in national and governmental projects :( if you have solution for that, I'm really open to know it. By the way here is not good place to talk about that do you want to chat? – Mehdi Sep 02 '12 at 04:06
  • *"do you want to chat?"* Not especially, but it seems obvious to me that your particular and unusual requirements are fueling your definition of 'better/good'. Don't get me wrong, 'Get Down' is probably a fine and worthy tool, but imply that JWS is not and I will challenge that assertion. – Andrew Thompson Sep 02 '12 at 04:18
  • ok, Thanks. Here is a place that all experts including you and I will tell their solutions and experiences and the community will choose the best solutions, and the best tool is a tool that fits with your requirements ;) – Mehdi Sep 02 '12 at 04:28