虽然从命令提示符运行完美,但双击时jar文件无法打开

时间:2015-03-20 11:18:51

标签: java swing

我正在尝试创建项目的可执行文件。我创建了我的应用程序的.jar文件。

双击时,此jar文件无法打开。 当我使用

从命令提示符运行jar文件时,它成功运行
java -jar app.jar

可能是什么原因? 这是我的源代码:

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);             
        }
    });
}

}

1 个答案:

答案 0 :(得分:-2)

如果您使用Eclipse或Netbeans作为IDE,这些是两种可能的解决方案:

Eclipse

  
      
  1. 从菜单栏的“文件”菜单中,选择“导出”。   
        
    1. 展开Java节点并选择Runnable JAR文件。单击“下一步”。
    2.   
    3. 在打开Runnable JAR导出向导的Runnable JAR文件规范页面中,选择一个' Java Application'启动配置   用于创建可运行的JAR。
    4.   
    5. 在“导出目标”字段中,键入或单击“浏览”以选择JAR文件的位置。
    6.   
    7. 选择合适的库处理策略。
    8.   
    9. 或者,您也可以创建ANT脚本以快速重新生成以前创建的可运行JAR文件。
    10.   
  2.   

Netbeans

  

[...]

     
      
  1. 在NetBeans IDE中,选择“文件”>新项目。
  2.   
  3. 在“新建项目”向导的“常规类别”页面中,选择“包含现有来源的Java项目”。单击“下一步”。
  4.   
  5. 在向导的“名称和位置”页面上,填写项目的名称和文件夹。保留Set as Main Project复选框   选择。点击下一步。例如,为Project Name键入AnotherGrep。   对于“项目文件夹”字段,您可以接受默认值。该   项目文件夹不必与源位于同一位置   您要导入项目的文件。
  6.   
  7. 在向导的“现有源”页面上,指定将在项目中的源。单击“添加文件夹”按钮   位于“源包文件夹”字段的右侧。然后导航到   您刚刚解压缩的DeploymentTutorial文件夹   系统,展开文件夹,选择src文件夹,然后单击“打开”。该   src文件夹已添加到“源包文件夹”字段中。
  8.   
  9. 单击“完成”。
  10.