1

嗨,我用 eclipse 做了一个小程序,我想把它发送到另一台朋友的电脑上,这样他们也可以使用这个程序。我在互联网上找到了一个简单的教程: 如何分发在 Eclipse 中构建的 java 项目? 伙计们的解决方案是:

您可以右键单击项目,选择导出并选择 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

4 回答 4

3

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

右键项目,选择build fat-jar 在此处输入图像描述

之后,您将进入以下屏幕-(选择您的主要课程) 在此处输入图像描述

然后 -

在此处输入图像描述

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

于 2012-07-26T02:58:31.910 回答
1

我认为 Eclipse 3.5 内置了这种能力,可以导出包含所有依赖项的 jar 包。但是,根据设置的复杂性,在某些情况下它可能不起作用。先试试看,因为它已经在那里了。

文件 -> 导出... 可运行的 JAR 文件... 将所需的库打包到生成的 JAR 中

于 2012-07-26T03:12:01.690 回答
0

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

于 2012-07-26T04:58:12.863 回答
0

看看这个

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

于 2012-07-26T03:02:06.700 回答