为什么我的java代码在eclipse中运行而不是作为.exe运行。或.class文件

时间:2013-08-07 21:50:17

标签: java eclipse compilation exe launch4j

我有这个很酷的程序,我在java中使用eclipse编写。当我在eclipse中运行它时,它的工作方式完全符合预期。我将其导出为jar文件,因此我可以使用Launch4j将其转换为我成功执行的可执行文件(.exe文件扩展名),但现在当我尝试打开可执行文件时,它表示该程序不兼容。我尝试在命令行编译代码,当我输入“java Calculator”试图运行程序时,它工作得很好。所以我的问题是为什么可执行文件不起作用?任何帮助将不胜感激。源代码完整显示在下方。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class Calculator implements ActionListener {

JFrame frame = new JFrame("Calculator");
JPanel panel = new JPanel(new GridLayout(4,4));

JTextArea text = new JTextArea(1,17);
JButton but1 = new JButton("1");
JButton but2 = new JButton("2");
JButton but3 = new JButton("3");
JButton but4 = new JButton("4");
JButton but5 = new JButton("5");
JButton but6 = new JButton("6");
JButton but7 = new JButton("7");
JButton but8 = new JButton("8");
JButton but9 = new JButton("9");
JButton but0 = new JButton("0");

JButton butadd = new JButton("+");
JButton butsub = new JButton("-");
JButton butmulti = new JButton("*");
JButton butdiv = new JButton("/");
JButton buteq = new JButton("=");

JButton butclear = new JButton("C");

double number1, number2, result;
int addc = 0, subc = 0, multic = 0, divc = 0;

public Calculator()
{
    frame.setVisible(true);
    frame.setSize(210,220);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    text.setEditable(false);
    frame.add(panel);
    frame.add(text, BorderLayout.NORTH);


    panel.add(but1);
    panel.add(but2);
    panel.add(but3);
    panel.add(but4);
    panel.add(but5);
    panel.add(but6);
    panel.add(but7);
    panel.add(but8);
    panel.add(but9);
    panel.add(but0);

    panel.add(butadd);
    panel.add(butsub);
    panel.add(butmulti);
    panel.add(butdiv);
    panel.add(buteq);
    panel.add(butclear);



    but1.addActionListener(this);
    but2.addActionListener(this);
    but3.addActionListener(this);
    but4.addActionListener(this);
    but5.addActionListener(this);
    but6.addActionListener(this);
    but7.addActionListener(this);
    but8.addActionListener(this);
    but9.addActionListener(this);
    but0.addActionListener(this);

    butadd.addActionListener(this);
    butsub.addActionListener(this);
    butmulti.addActionListener(this);
    butdiv.addActionListener(this);
    buteq.addActionListener(this);
    butclear.addActionListener(this);


}
public void colorChange(){
Random rand=new Random();
float r=rand.nextFloat(); 
float g= rand.nextFloat();
float b=rand.nextFloat();
Color randomColor=new Color(r,g,b);

but1.setBackground(randomColor); 
but2.setBackground(randomColor);
but3.setBackground(randomColor);
but4.setBackground(randomColor); 
but5.setBackground(randomColor); 
but6.setBackground(randomColor); 
but7.setBackground(randomColor);
but8.setBackground(randomColor);
but9.setBackground(randomColor);
but0.setBackground(randomColor); 
butadd.setBackground(randomColor);
butsub.setBackground(randomColor);
butmulti.setBackground(randomColor);
butdiv.setBackground(randomColor); 
butclear.setBackground(randomColor); 
buteq.setBackground(randomColor); 

but1.setForeground(Color.white); 
but2.setForeground(Color.white);
but3.setForeground(Color.white);
but4.setForeground(Color.white); 
but5.setForeground(Color.white); 
but6.setForeground(Color.white); 
but7.setForeground(Color.white);
but8.setForeground(Color.white);
but9.setForeground(Color.white);
but0.setForeground(Color.white); 
butadd.setForeground(Color.white);
butsub.setForeground(Color.white);
butmulti.setForeground(Color.white);
butdiv.setForeground(Color.white); 
butclear.setForeground(Color.white); 
buteq.setForeground(Color.white);


}


@Override
public void actionPerformed(ActionEvent arg0) 
{
    Object source = arg0.getSource();

    if(source == butclear)
    {
        number1 = 0.0;
        number2 = 0.0;
        text.setText("");
        colorChange();
    }

    if(source == but1)
    {
        text.append("1");
    }

    if(source == but2)
    {
        text.append("2");
    }

    if(source == but3)
    {
        text.append("3");
    }

    if(source == but4)
    {
        text.append("4");
    }

    if(source == but5)
    {
        text.append("5");
    }

    if(source == but6)
    {
        text.append("6");
    }

    if(source == but7)
    {
        text.append("7");
    }

    if(source == but8)
    {
        text.append("8");
    }

    if(source == but9)
    {
        text.append("9");
    }

    if(source == but0)
    {
        text.append("0");
    }

    if(source == butadd)
    {
        number1 = numberReader();
        text.setText("");
        addc = 1;
        subc = 0;
        multic = 0;
        divc = 0;
    }

    if(source == butsub)
    {
        number1 = numberReader();
        text.setText("");
        addc = 0;
        subc = 1;
        multic = 0;
        divc = 0;
    }

    if(source == butmulti)
    {
        number1 = numberReader();
        text.setText("");
        addc = 0;
        subc = 0;
        multic = 1;
        divc = 0;
    }

    if(source == butdiv)
    {
        number1 = numberReader();
        text.setText("");
        addc = 0;
        subc = 0;
        multic = 0;
        divc = 1;
    }

    if(source == buteq)
    {
        number2 = numberReader();

        if(addc > 0)
        {
            result = number1 + number2;
            text.setText(Double.toString(result));
        }

        if(subc > 0)
        {
            result = number1 - number2;
            text.setText(Double.toString(result));
        }

        if(multic > 0)
        {
            result = number1 * number2;
            text.setText(Double.toString(result));
        }

        if(divc > 0)
        {
            result = number1 / number2;
            text.setText(Double.toString(result));
        }
    }
    colorChange(); 
}

public double numberReader()
{
    Double num1;
    String s;
    s = text.getText();
    num1 = Double.valueOf(s);

    return num1;
}

public static void main(String[] args)
{
    new Calculator();
}
}

这是错误代码。 PrintProgram Compatibility Troubleshooter Publisher详细信息

发现的问题 不兼容的ProgramIncompatible程序 计算器不兼容。检测到检测到 修复应用程序计算器已完成 发现问题检测详情 6检测到不兼容的程序检测 计算器不兼容。 修复应用程序计算器已完成 提供修复不兼容程序的步骤。 信息兼容模式 适用的兼容模式: Windows XP(Service Pack 3) 用户验证:修复工作检测详细信息展开 收集信息 电脑名称:JOSHVAYLELAPTOP Windows版本:6.2 架构:x64 时间:2013年8月7日星期三下午7:40:32 发布商详情展开 程序兼容性疑难解答 查找并修复在此版本的Windows上运行旧程序的问题。包装版本:1.5 发布者:Microsoft Windows 程序兼容性疑难解答 查找并修复在此版本的Windows上运行旧程序的问题。 包版本:1.0 出版商:微软公司

1 个答案:

答案 0 :(得分:1)

是否有理由需要使用.exe而不是可执行jar?如果你不确定,请看这里http://www.excelsior-usa.com/articles/java-to-exe.html它作为一个可执行的jar运行,没有任何问题。