简体   繁体   中英

why does my java code run in eclipse but not as an .exe. or .class file

I have this cool program that I wrote in java using eclipse. When I run it in eclipse it works exactly as it's supposed to. I exported it as a jar file so I could use Launch4j to convert it to an executable file(.exe file extension) which I did successfully, but now when I try to open the executable it says the program is incompatible. i tried compiling the code at command line and when I typed "java Calculator" to try to run the program and it worked perfectly. So my question is why won't the executable work? Any help would be greatly appreciated. The source code is displayed below in its entirety.

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

And here is the error code. PrintProgram Compatibility Troubleshooter Publisher details

Issues found Incompatible ProgramIncompatible Program Calculator is incompatible. Detected Detected Fix application Calculator Completed Issues found Detection details 6 Incompatible Program Detected Detected Calculator is incompatible. Fix application Calculator Completed Provides steps to fix the incompatible program. InformationalCompatibility Mode Compabitiliy modes applied: Windows XP (Service Pack 3) User verification: Fix worked Detection details Expand Collection information Computer Name: JOSHVAYLELAPTOP Windows Version: 6.2 Architecture: x64 Time: Wednesday, August 7, 2013 7:40:32 PM Publisher details Expand Program Compatibility Troubleshooter Find and fix problems with running older programs on this version of Windows. Package Version: 1.5 Publisher: Microsoft Windows Program Compatibility Troubleshooter Find and fix problems with running older programs on this version of Windows. Package Version: 1.0 Publisher: Microsoft Corporation

Is there a reason it needs to be a .exe instead of an executable jar? If you are not sure, look here http://www.excelsior-usa.com/articles/java-to-exe.html It ran for me as an executable jar with no problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM