3

I am Trying to create executable of my project. I have created .jar file of my application.

This jar file does not open when I double click on it. It runs successfully when I run jar file from command prompt using

java -jar app.jar

What could be the reason? this is my source code :

package app;

import java.awt.Color;


public class MainApplication extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainApplication frame = new MainApplication();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public MainApplication() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(130, 20, 1100, 700);  
    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    ImageIcon F1Icon = new ImageIcon("C:\\Users\\Bruce\\workspace\\OSTNashik\\src\\app\\F1.jpg");   
    JLabel labelImage = new JLabel("");
    labelImage.setBounds(381, 11, 230, 161);        
    contentPane.add(labelImage);        
    Image image = F1Icon.getImage(); // transform it 
    Image newimg = image.getScaledInstance(230, 190,  java.awt.Image.SCALE_SMOOTH); // scale it the smooth way  
    F1Icon = new ImageIcon(newimg);
    labelImage.setIcon(F1Icon);

    ImageIcon osTechIcon = new ImageIcon("C:\\Users\\Bruce\\workspace\\OSTNashik\\src\\app\\a.gif"); 
    JLabel osTechLabel = new JLabel("");
    osTechLabel.setBounds(22, 11, 230, 111);
    contentPane.add(osTechLabel);
    Image image1 = osTechIcon.getImage(); // transform it 
    Image newimg1 = image1.getScaledInstance(230, 130,  java.awt.Image.SCALE_SMOOTH); // scale it the smooth way  
    osTechIcon = new ImageIcon(newimg1);
    osTechLabel.setIcon(osTechIcon);

    ImageIcon ninedotIcon = new ImageIcon("C:\\Users\\Bruce\\workspace\\OSTNashik\\src\\app\\ninedots.jpg");
    JLabel nineDotLabel = new JLabel("");
    nineDotLabel.setBounds(826, 11, 248, 111);
    contentPane.add(nineDotLabel);
    Image image2 = ninedotIcon.getImage(); // transform it 
    Image newimg2 = image2.getScaledInstance(230, 110,  java.awt.Image.SCALE_SMOOTH); // scale it the smooth way  
    ninedotIcon = new ImageIcon(newimg2);
    nineDotLabel.setIcon(ninedotIcon);

    JPanel panel = new JPanel();
    panel.setBackground(Color.CYAN);
    panel.setBounds(25, 200, 512, 439);
    contentPane.add(panel);
    panel.setLayout(null);

    JButton btnLogANew = new JButton("Log New Call ");      
    btnLogANew.setBounds(10, 84, 220, 23);
    panel.add(btnLogANew);

    JButton btnViewOpenCalls = new JButton("View Open Calls");
    btnViewOpenCalls.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            OpenCallSummary frame = new OpenCallSummary();
            frame.setVisible(true);
        }
    });
    btnViewOpenCalls.setBounds(10, 132, 220, 23);
    panel.add(btnViewOpenCalls);

    JButton btnEnggDesk = new JButton("View Engineer Desk Calls");
    btnEnggDesk.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            EnggDeskCallSummary frame = new EnggDeskCallSummary();
            frame.setVisible(true);
        }
    });
    btnEnggDesk.setBounds(10, 178, 220, 23);
    panel.add(btnEnggDesk); 

    JLabel lblServiceCenterRelated = new JLabel("Service Center Related Tasks");
    lblServiceCenterRelated.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 16));
    lblServiceCenterRelated.setBounds(138, 25, 212, 23);
    panel.add(lblServiceCenterRelated);

    JButton closedCalls = new JButton("View Closed Calls");
    closedCalls.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            ClosedCallSummary cc=new ClosedCallSummary();
            cc.setVisible(true);
        }
    });
    closedCalls.setBounds(10, 271, 220, 23);
    panel.add(closedCalls);

    JButton btnViewEdmaCalls = new JButton("View EDMA Calls");
    btnViewEdmaCalls.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            EDMACallSummary ed=new EDMACallSummary();
            ed.setVisible(true);
        }
    });
    btnViewEdmaCalls.setBounds(10, 225, 220, 23);
    panel.add(btnViewEdmaCalls);

    JPanel panel_2 = new JPanel();
    panel_2.setBackground(SystemColor.inactiveCaptionText);
    panel_2.setBounds(251, 89, 251, 99);
    panel.add(panel_2);
    panel_2.setLayout(null);

    JButton btnTravelPending = new JButton("View Pending Travel Approvals");
    btnTravelPending.setBounds(10, 21, 220, 23);
    panel_2.add(btnTravelPending);

    JButton btnViewApprovedTravel = new JButton("View Approved Travel Approvals");
    btnViewApprovedTravel.setBounds(10, 55, 220, 23);
    panel_2.add(btnViewApprovedTravel);

    JPanel panel_3 = new JPanel();
    panel_3.setBackground(SystemColor.inactiveCaptionText);
    panel_3.setLayout(null);
    panel_3.setBounds(251, 199, 251, 99);
    panel.add(panel_3);

    JButton btnViewMaterialRequired_1 = new JButton("View Material Required Calls");
    btnViewMaterialRequired_1.setBounds(10, 21, 220, 23);
    panel_3.add(btnViewMaterialRequired_1);

    JButton btnViewMaterialApproved = new JButton("View Material Approved Calls");
    btnViewMaterialApproved.setBounds(10, 55, 220, 23);
    panel_3.add(btnViewMaterialApproved);
    btnViewMaterialApproved.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            MaterialApprovedSummary ma=new MaterialApprovedSummary();
            ma.setVisible(true);
        }
    });
    btnViewMaterialRequired_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            MaterialRequiredSummary mr=new MaterialRequiredSummary();
            mr.setVisible(true);
        }
    });
    btnViewApprovedTravel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ApprovedTravelSummary ats=new ApprovedTravelSummary();
            ats.setVisible(true);
        }
    });
    btnTravelPending.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            PendingTravelApprovalSummary ps=new PendingTravelApprovalSummary();
            ps.setVisible(true);
        }
    });

    JPanel panel2 = new JPanel();
    panel2.setBackground(Color.CYAN);
    panel2.setBounds(547, 200, 527, 439);
    contentPane.add(panel2);
    panel2.setLayout(null);

    JLabel lblOtherTasks = new JLabel("Reports");
    lblOtherTasks.setBounds(238, 22, 209, 22);
    lblOtherTasks.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 16));
    panel2.add(lblOtherTasks);

    JButton enggwisebtn = new JButton("Engineer Wise Reports");
    enggwisebtn.setBounds(48, 127, 181, 23);
    panel2.add(enggwisebtn);

    JButton opencallbtn = new JButton("Open Calls Reports");
    opencallbtn.setBounds(48, 172, 181, 23);
    panel2.add(opencallbtn);

    JButton closedcallbtn = new JButton("Closed Calls Reports");
    closedcallbtn.setBounds(48, 220, 181, 23);
    panel2.add(closedcallbtn);

    JButton btnOtherCallsReports = new JButton("Other Calls Reports");
    btnOtherCallsReports.setBounds(48, 268, 181, 23);
    panel2.add(btnOtherCallsReports);

    JButton btnSearchByRma = new JButton("Search By RMA Number");
    btnSearchByRma.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            RMASearch rma=new RMASearch();
            rma.setVisible(true);
        }
    });
    btnSearchByRma.setBounds(48, 83, 181, 23);
    panel2.add(btnSearchByRma);



    btnLogANew.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Main m=new Main();
            m.setVisible(true);             
        }
    });
}

}

Bhushan Gadekar
  • 83
  • 1
  • 10
  • 1
    is that a desktop app or what? of-course the jar would perform some back-end operation..I hope it is a console based app.. right? – Danyal Sandeelo Mar 20 '15 at 11:20
  • its a desktop app. i had printed window status in console in one child frame, could that be a problem? i want my app to be executable by double click only – Bhushan Gadekar Mar 20 '15 at 11:22
  • @BhushanGadekar You've said multiple times you want your app runnable by double click only, and that it runs fine from the jar when executed from the command line. What you haven't told us is what platform you're on, what version of Java you have installed and what configuration you have tried. – Elliott Frisch Mar 20 '15 at 11:24
  • Maybe it starts but directly ends because of an error, before your frame is displayed. Maybe some lines of code showing your startup will also help. Additionally, you should add some logging statements to your application, so you can easily see the point where it fails. – Seelenvirtuose Mar 20 '15 at 11:28
  • @ElliottFrisch i am running windows 7 & jdk 7.Also i am not able to see the error log as when i double click on jar file , console window opens & closes within a second – Bhushan Gadekar Mar 20 '15 at 11:31
  • @BhushanGadekar If that console window opens and closes immediately, then the assumption in my comment becomes certainty: You have an error during startup! – Seelenvirtuose Mar 20 '15 at 11:32
  • @Seelenvirtuose but how could the same jar file run when i run from console? – Bhushan Gadekar Mar 20 '15 at 11:34
  • @BhushanGadekar I don't know what you do in your startup. Show that code! – Seelenvirtuose Mar 20 '15 at 11:36
  • @Seelenvirtuose i have uploaded my source code – Bhushan Gadekar Mar 20 '15 at 11:41
  • I directly tried it out. It works perfectly. Running on command line and double-clicking the JAR, they both open the window. I cannot see any problem in the startup code (other than being not very clean). I see, that you have an `e.printStacktrace()` in your main method. Maybe a `Thread.sleep(30*1000)` will also help to simply see the error message, if any. – Seelenvirtuose Mar 20 '15 at 11:49
  • I would always recommend to create a small .bat (for windows) and .sh (for unix) to start a runnable jar. Because e.g. with some Linux distributions/settings a runnable jar is not executed automatically but the archive is shown in an archive manager. So a .bat in the main-folder and then a lib or bin folder where the jar is located would be my recommendation (even if it doesn't really answer your question). – MalaKa Mar 20 '15 at 12:44
  • One reason it may work from command line and not from double click is file associations and/or paths. Check that the java.exe associated with the .jar file type is the same java.exe that is getting invoked on the command line. If you have an older install of java that was linked to .jar files, it may fail by not being able to load Java 7 classes and that would account for the window starting/closing. – Jere Mar 20 '15 at 16:37

1 Answers1

-2

If you're using Eclipse or Netbeans as IDE these are two possibile solutions:

Eclipse:

  1. From the menu bar's File menu, select Export.
    1. Expand the Java node and select Runnable JAR file. Click Next.
    2. In the Opens the Runnable JAR export wizard Runnable JAR File Specification page, select a 'Java Application' launch configuration to use to create a runnable JAR.
    3. In the Export destination field, either type or click Browse to select a location for the JAR file.
    4. Select an appropriate library handling strategy.
    5. Optionally, you can also create an ANT script to quickly regenerate a previously created runnable JAR file.

Netbeans:

[...]

  1. In NetBeans IDE, choose File > New Project.
  2. In the General Category page of the New Project wizard, select Java Project With Existing Sources. Click Next.
  3. On the Name and Location page of the wizard, fill in a name and folder for your project. Leave the Set as Main Project checkbox selected. Click Next. For example, type AnotherGrep for Project Name. For the Project Folder field, you can just accept the default. The project folder does not have to be in the same location as the source files that you are importing into the project.
  4. On the Existing Sources page of the wizard, you specify the sources that will be in the project. Click the Add Folder button that is to the right of the Source Package Folders field. Then navigate to the DeploymentTutorial folder that you have just unzipped on your system, expand the folder, select the src folder, and click Open. The src folder is added to your Source Package Folders field.
  5. Click Finish.
IlGala
  • 3,331
  • 4
  • 35
  • 49