使用Eclipse分发Java程序

时间:2012-07-26 02:30:51

标签: java eclipse jar distribute

您好我用eclipse创建了一个小程序,我正在尝试将它发送给另一台朋友计算机,以便他们也可以使用该程序。我在互联网上找到了一个直接的教程: How to distribute java project built in Eclipse? 那些家伙的解决方案是:

  

您可以右键单击该项目,选择Export并选择Java,然后选择   JAR作为格式。

然而,当我这样做并尝试将其发送到朋友计算机时,他们遇到了这个错误:

  

无法找到主要类:pisavior.PISavior程序现在将退出。

我尝试将其导出为可运行的jar文件和普通的jar文件。都没有奏效。 我认为这个问题可能与我的主要课程有关?也许它没有被阅读?或者可能没有发送库?谁能给我一些方向吗?非常感谢!

这是我的代码,有点长......

    import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class PISavior implements MouseListener {

    Random random = new Random();
    private static int amount; 
    private static int editLine;

    private static String locations[];
    private static String usernames[];
    private static String passwords[];

    private final char LOWERCASE[] = 
    {
        'a', 'b', 'c', 'd', 'e',
        'f', 'g', 'h', 'i', 'j',
        'k', 'l', 'm', 'n', 'o',
        'p', 'q', 'r', 's', 't',
        'u', 'v', 'w', 'x', 'y', 
        'z'
    };
    private final char UPPERCASE[] = 
    {
        'A', 'B', 'C', 'D', 'E',
        'F', 'G', 'H', 'I', 'J',
        'K', 'L', 'M', 'N', 'O',
        'P', 'Q', 'R', 'S', 'T',
        'U', 'V', 'W', 'X', 'Y', 
        'Z'
    };
    private final char NUMBERS[] =
    {
        '0', '1', '2', '3', '4',
        '5', '6', '7', '8', '9'
    };

    private static volatile boolean edit = false;

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();

    JCheckBox jcb_Integers = new JCheckBox("Integers");
    JCheckBox jcb_Capitals = new JCheckBox("Capitals");

    JScrollPane jscroll;

    JLabel jlb_Location = new JLabel("Location");
    JLabel jlb_Username = new JLabel("Username");
    JLabel jlb_Password = new JLabel("Password");
    JLabel jlb_Amount = new JLabel("Amount:");
    JLabel jlb_Length = new JLabel("Length:");
    JLabel jlb_Locations[];
    JLabel jlb_Usernames[];
    JLabel jlb_Passwords[];

    JButton jbt_AddLocation = new JButton("Add Location");
    JButton jbt_Save = new JButton("Save");
    JButton jbt_Add = new JButton("Add");
    JButton jbt_Randomize = new JButton("Randomize");
    JButton jbt_Edit = new JButton("Edit");

    JTextField jtf_Locations[];
    JTextField jtf_Usernames[];
    JTextField jtf_Passwords[];
    JTextField jtf_Location = new JTextField();
    JTextField jtf_Username = new JTextField();
    JTextField jtf_Password = new JTextField();
    JTextField jtf_Integers = new JTextField();
    JTextField jtf_Capitals = new JTextField();
    JTextField jtf_Length = new JTextField();

    public static void main(String args[]) throws FileNotFoundException{
        new PISavior();
    }

    public PISavior() throws FileNotFoundException{
        panel.setLayout(null);
        panel.addMouseListener(this);
        panel.setBounds(0, 0, 400,400);
        load();
        addMainUI();
        //======================================================================
        // ** Main User Interface
        //======================================================================
        panel.add(jlb_Location);
        panel.add(jlb_Username);
        panel.add(jlb_Password);
        jbt_AddLocation.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                edit = false;
                removeMainUI();
                addLocationUI();
                panel.repaint();
            }
    });
        jbt_Save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    save();
                } catch (IOException ex) {
                    Logger.getLogger(PISavior.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    });
        //======================================================================
        // ** Add Location User Interface
        //======================================================================
        jtf_Location.setBounds(5, 25, 100, 20);
        jtf_Username.setBounds(5, 65, 100, 20);
        jtf_Password.setBounds(5, 105, 100, 20);
        jlb_Amount.setBounds(210, 10, 100, 20);
        jcb_Integers.setBounds(130, 30, 80, 20);
        jcb_Integers.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jtf_Integers.setEnabled(jcb_Integers.isSelected());
            }
    });
        jcb_Capitals.setBounds(130, 60, 80, 20);
        jcb_Capitals.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jtf_Capitals.setEnabled(jcb_Capitals.isSelected());
            }
    });
        jtf_Integers.setBounds(210, 30, 50, 20);
        jtf_Integers.setEnabled(false);
        jtf_Integers.setText("0");
        jtf_Capitals.setBounds(210, 60, 50, 20);
        jtf_Capitals.setEnabled(false);
        jtf_Capitals.setText("0");
        jlb_Length.setBounds(135, 90, 80, 20);
        jtf_Length.setBounds(210, 90, 50, 20);
        jtf_Length.setText("0");
        jbt_Randomize.setBounds(140, 120, 100, 20);
        jbt_Randomize.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int length = Integer.parseInt(jtf_Length.getText());
                int integers = Integer.parseInt(jtf_Integers.getText());
                int capitals = Integer.parseInt(jtf_Capitals.getText());
                if (length != 0 && length >= (integers + capitals)) {
                    jtf_Password.setText(getRandomPassword(length, jcb_Integers.isSelected() != false ? integers : 0, jcb_Capitals.isSelected() != false ? capitals : 0));
                }
            }
    });
        jbt_Add.setBounds(20, 135, 60, 15);
        jbt_Add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addInformation(jtf_Location.getText().length() == 0 ? "(Empty)" : jtf_Location.getText(), jtf_Username.getText().length() == 0 ? "(Empty)" : jtf_Username.getText(), jtf_Password.getText().length() == 0 ? "(Empty)" : jtf_Password.getText());
                jtf_Location.setText("");
                jtf_Username.setText("");
                jtf_Password.setText("");
                removeLocationUI();
                addMainUI();
                panel.repaint();
            }
        });
        jbt_Edit.setBounds(20, 135, 60, 15);
        jbt_Edit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int i = editLine;
                locations[i] = jtf_Location.getText();
                usernames[i] = jtf_Username.getText();
                passwords[i] = jtf_Password.getText();
                jlb_Locations[i].setText(locations[i]);
                jlb_Usernames[i].setText(usernames[i]);
                jlb_Passwords[i].setText(passwords[i]);
                jtf_Location.setText("");
                jtf_Username.setText("");
                jtf_Password.setText("");
                removeLocationUI();
                addMainUI();
                panel.repaint();
            }
        });

        jscroll = new JScrollPane(panel);
        frame.setTitle("Personal Information Savior"); 
        frame.setSize(300,240);
        frame.setContentPane(jscroll);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    public void removeMainUI() {
        if (amount != 0) {
            for (int i = 0; i < amount; i++) {
                panel.remove(jlb_Locations[i]);
                panel.remove(jlb_Usernames[i]);
                panel.remove(jlb_Passwords[i]);
            }
        }
        panel.remove(jbt_AddLocation);
        panel.remove(jbt_Save);
        panel.repaint();
    }

    public void addMainUI() {
        jlb_Location.setBounds(10, 5, 100, 20);
        jlb_Username.setBounds(100, 5, 100, 20);
        jlb_Password.setBounds(200, 5, 100, 20);
        jbt_AddLocation.setBounds(75, (20 * amount) + 25, 110, 15);
        jbt_Save.setBounds(90, (20 * amount) + 45, 80, 15);
        jlb_Location.setText("Location");
        jlb_Username.setText("Username");
        jlb_Password.setText("Password");
        panel.setPreferredSize(new Dimension(200, amount * 30));
        if (amount != 0) {
            for (int i = 0; i < amount; i++) {
                jlb_Locations[i].setBounds(10, 25 + (i * 20), 100, 20);
                jlb_Usernames[i].setBounds(100, 25 + (i * 20), 100, 20);
                jlb_Passwords[i].setBounds(200, 25 + (i * 20), 100, 20);
                panel.add(jlb_Locations[i]);
                panel.add(jlb_Usernames[i]);
                panel.add(jlb_Passwords[i]);
            }
        }
        panel.add(jbt_AddLocation);
        panel.add(jbt_Save);
        panel.repaint();
    }

    public void removeLocationUI() {
        panel.remove(jtf_Location);
        panel.remove(jtf_Username);
        panel.remove(jtf_Password);
        panel.remove(jlb_Amount);
        panel.remove(jcb_Integers);
        panel.remove(jcb_Capitals);
        panel.remove(jtf_Integers);
        panel.remove(jtf_Capitals);
        panel.remove(jlb_Length);
        panel.remove(jtf_Length);
        panel.remove(jbt_Randomize);
        panel.remove(jbt_Add);
        panel.remove(jbt_Edit);

        panel.repaint();
    }

    public void addLocationUI() {
        if (!edit) {
            jlb_Location.setText("New Location:");
            jlb_Location.setBounds(5, 5, 100, 20);
            jlb_Username.setText("New Username:");
            jlb_Username.setBounds(5, 45, 100, 20);
            jlb_Password.setText("New Password:");
            jlb_Password.setBounds(5, 85, 100, 20);
            panel.add(jtf_Location);
            panel.add(jtf_Username);
            panel.add(jtf_Password);
            panel.add(jlb_Amount);
            panel.add(jcb_Integers);
            panel.add(jcb_Capitals);
            panel.add(jtf_Integers);
            panel.add(jtf_Capitals);
            panel.add(jlb_Length);
            panel.add(jtf_Length);
            panel.add(jbt_Randomize);
            panel.add(jbt_Add);
            panel.repaint();
        } else {
            jlb_Location.setText("New Location:");
            jlb_Location.setBounds(5, 5, 100, 20);
            jlb_Username.setText("New Username:");
            jlb_Username.setBounds(5, 45, 100, 20);
            jlb_Password.setText("New Password:");
            jlb_Password.setBounds(5, 85, 100, 20);
            panel.add(jtf_Location);
            panel.add(jtf_Username);
            panel.add(jtf_Password);
            panel.add(jlb_Amount);
            panel.add(jcb_Integers);
            panel.add(jcb_Capitals);
            panel.add(jtf_Integers);
            panel.add(jtf_Capitals);
            panel.add(jlb_Length);
            panel.add(jtf_Length);
            panel.add(jbt_Randomize);
            panel.add(jbt_Edit);
            panel.repaint();
        }
    }

    public String getRandomPassword(int length, int integers, int capitals) {
        int lowers = length - (integers + capitals);
        String password = "";
        String scramble[] = new String[length];
        if (capitals != 0) {
            for (int i = 0; i < capitals; i++) {
                while (true) {
                    int randomScramble = random.nextInt(length);
                    if (scramble[randomScramble] == null || scramble[randomScramble].equals("")) {
                        scramble[randomScramble] = Character.toString(UPPERCASE[random.nextInt(UPPERCASE.length)]);
                        break;
                    }
                }
            }
        }
        if (integers != 0) {
            for (int i = 0; i < integers; i++) {
                while (true) {
                    int randomScramble = random.nextInt(length);
                    if (scramble[randomScramble] == null || scramble[randomScramble].equals("")) {
                        scramble[randomScramble] = Character.toString(NUMBERS[random.nextInt(NUMBERS.length)]);
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < lowers; i++) {
            while (true) {
                int randomScramble = random.nextInt(length);
                if (scramble[randomScramble] == null || scramble[randomScramble].equals("")) {
                    scramble[randomScramble] = Character.toString(LOWERCASE[random.nextInt(LOWERCASE.length)]);
                    break;
                }
            }
        }
        for (int i = 0; i < length; i++) {
            password += scramble[i];
        }
        return password;
    }

    public void save() throws IOException {
        File dir = new File("C:/PISavior");
        if (!dir.exists()) {
            dir.mkdir();
        }
        File myfile = new File("C:/PISavior/myfile.txt");
        myfile.delete();
        myfile.createNewFile();
        PrintWriter output = new PrintWriter(myfile);
        output.print(amount + "*");
        for (int i = 0; i < amount; i++) {
            output.print(locations[i] + "*");
            output.print(usernames[i] + "*");
            output.print(passwords[i] + "*");
        }
        output.close();
    }

    public void load() throws FileNotFoundException {
        File myfile = new File("C:/PISavior/myfile.txt");
        if (myfile.exists()) {
            Scanner input = new Scanner(myfile);
            while (input.hasNext())
            {
                input.useDelimiter("[*]");
                amount = input.nextInt();

                locations = new String[amount];
                usernames = new String[amount];
                passwords = new String[amount];
                jlb_Locations = new JLabel[amount];
                jlb_Usernames = new JLabel[amount];
                jlb_Passwords = new JLabel[amount];

                for (int i = 0; i < amount; i++) {
                    locations[i] = input.next();
                    jlb_Locations[i] = new JLabel(locations[i]);
                    usernames[i] = input.next();
                    jlb_Usernames[i] = new JLabel(usernames[i]);
                    passwords[i] = input.next();
                    jlb_Passwords[i] = new JLabel(passwords[i]);
                }
            }
        }
    }

    public void addInformation(String info1, String info2, String info3) {
        if (amount == 0) {
            amount++;
            locations = new String[amount];
            usernames = new String[amount];
            passwords = new String[amount];
            jlb_Locations = new JLabel[amount];
            jlb_Usernames = new JLabel[amount];
            jlb_Passwords = new JLabel[amount];
            locations[0] = info1;
            usernames[0] = info2;
            passwords[0] = info3;
            jlb_Locations[0] = new JLabel(locations[0]);
            jlb_Usernames[0] = new JLabel(usernames[0]);
            jlb_Passwords[0] = new JLabel(passwords[0]);
        } else {
            String temp1[] = new String[amount];
            String temp2[] = new String[amount];
            String temp3[] = new String[amount];

            for (int i = 0; i < amount; i++) {
                temp1[i] = locations[i];
                temp2[i] = usernames[i];
                temp3[i] = passwords[i];
            }

            amount++;

            locations = new String[amount];
            usernames = new String[amount];
            passwords = new String[amount];

            jlb_Locations = new JLabel[amount];
            jlb_Usernames = new JLabel[amount];
            jlb_Passwords = new JLabel[amount];

            for (int i = 0; i < temp1.length; i++) {
                locations[i] = temp1[i];
                usernames[i] = temp2[i];
                passwords[i] = temp3[i];

                jlb_Locations[i] = new JLabel(temp1[i]);
                jlb_Usernames[i] = new JLabel(temp2[i]);
                jlb_Passwords[i] = new JLabel(temp3[i]);
            }

            locations[amount-1] = info1;
            usernames[amount-1] = info2;
            passwords[amount-1] = info3;

            jlb_Locations[amount-1] = new JLabel(info1);
            jlb_Usernames[amount-1] = new JLabel(info2);
            jlb_Passwords[amount-1] = new JLabel(info3);
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        int y = e.getY();
        if (y > 25 && y < (amount * 20) + 25) {
            for (int i = 0; i < amount; i++) {
                if (y < (i*20) + 45) {
                    edit = true;
                    editLine = i;
                    jtf_Location.setText(locations[i]);
                    jtf_Username.setText(usernames[i]);
                    jtf_Password.setText(passwords[i]);
                    removeMainUI();
                    addLocationUI();
                }
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent me) {
    }

    @Override
    public void mousePressed(MouseEvent me) {
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }
}

4 个答案:

答案 0 :(得分:3)

对于初学者来说,当您导出这样的jar时,你就不会将Lib jar放入其中。你必须将fat-jar插件添加到Eclipse然后它很容易。

右键单击该项目并选择build fat-jar enter image description here

之后你会转到下面的屏幕 - (选择你的主要课程) enter image description here

然后 -

enter image description here

现在,您的.jar文件中将包含所有库jar。

答案 1 :(得分:1)

我认为Eclipse 3.5内置了这种能力,可以导出与所有依赖项打包在一起的jar。但是,根据设置的复杂程度,在某些情况下可能无法正常工作。首先尝试一下,因为它已经存在了。

档案 - &gt;导出...可运行的JAR文件...将所需的库打包到生成的JAR中

答案 2 :(得分:0)

查看this

基本上,您必须在jar文件的META-INF文件夹中指定主类。

答案 3 :(得分:0)

您应该在没有类文件的情况下导出项目。在其他机器中,再次构建项目并运行它。但是在构建过程之前,不要忘记将eclipse默认运行时JRE指向系统的JDK。