1

Hi I made a small program with eclipse and I am trying to send it to another friends computer so they can use the program as well. I found a straight forward tutorial on the internet at: How to distribute java project built in Eclipse? and the guys solution was:

You can right-click the project, select Export and choose Java, then JAR as the format.

However when I did just that and tried sending it to a friends computer they got this error:

Could not find the main class: pisavior.PISavior Program will now exit.

I tried exporting it as a runnable jar file and plain jar file. Neither worked. I think the problem may be with my main class? Maybe its not being read? Or possibly the libraries aren't being sent? Can anyone give me some direction please? Much appreciated!

this is my code, kinda long...

    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) {
    }
}
Community
  • 1
  • 1
  • Q: Do you happen to have a class "PISavior" in package "pisavior" in your Jar file? – paulsm4 Jul 26 '12 at 02:36
  • When I compile it, it just makes a pisavior.jar file. Do i need to put that file and the .class file in a zipped folder together? – Benjamin Braga Jul 26 '12 at 02:45
  • There are many factors: it's just a question of a) how many you know about, and b) how many Eclipse does for you automatically. I believe Eclipse will do *most* of the work for you. It sounds like you have an (auto-generated) "Manifest", and it specifies "pisavior.PISavior" as your main class. I *hope* you *have* a class "PISavior" in your project. I also hope PISavior has a "static void Main()" method. – paulsm4 Jul 26 '12 at 04:12

4 Answers4

3

For starters when you export the jar like that you won't get the Lib jars into it. You have to add the fat-jar plugin to Eclipse then it's easy.

The right click on the project and choose build fat-jar enter image description here

After that you'll go to the below screen -(Choose your main class) enter image description here

Then -

enter image description here

Now you'll have all your library jar included in your .jar file.

Chan
  • 2,601
  • 6
  • 28
  • 45
  • [1]: http://i.stack.imgur.com/1eDin.png [2]: http://i.stack.imgur.com/1eDin.png [3]: http://i.stack.imgur.com/1eDin.png [4]: http://i.stack.imgur.com/1eDin.png I followed your instructions as seen above, yet to no avail. My brothers computer was unable to find the main class file. Which is weird. There is a static main class easily accessible.. – Benjamin Braga Jul 26 '12 at 11:38
  • does the jar run in your own computer? – Chan Jul 26 '12 at 16:52
  • what is the version of java on your computer and brother's computer? And does other jars run on brothers computer? – Chan Jul 27 '12 at 02:08
  • We both have up to date javas, considering i re installed windows last week and his computer is 2 months old. I've yet to try any different .jars none to try really. I dont know if showing the code will help any? – Benjamin Braga Jul 27 '12 at 12:13
  • [1]: http://i.stack.imgur.com/80FDS.png [2]: http://i.stack.imgur.com/mJENd.png [3]: http://i.stack.imgur.com/jGA21.png fixed the images, you can see where it says select files for .jar theres just the one. should there be more? and if so , you said that ill have to include all my library jars into the .jar file. im not sure what you mean by that, the imports i used? swing? awt? – Benjamin Braga Jul 27 '12 at 13:07
  • can you please post the full stack trace? – Chan Jul 27 '12 at 16:36
  • Uhm, not sure how to do that. I thought the stack trace was if there were errors while compiling. I don't get any errors while running in the compiler. – Benjamin Braga Jul 27 '12 at 22:23
  • no no -go to the cmd. After that - `java -jar pathtoyourjar/yourjar.jar`. After that you'll get errorrs – Chan Jul 28 '12 at 01:32
1

I think Eclipse 3.5 has this ability built in, to export jar's packaged with all dependencies. Depending on the complexity of your setup, though, in some cases it may not work. Give that a try first, since it's already there.

File -> Export... Runnable JAR File... Package required libraries into generated JAR

Jason Dunkelberger
  • 1,207
  • 14
  • 18
0

Take a look at this

Basically you must specify your main class in your META-INF folder of the jar file.

Community
  • 1
  • 1
Ale Zalazar
  • 1,875
  • 1
  • 11
  • 14
0

You should export your project without class file. In other machine, build your project again and run it.But don't forget to point eclipse default runtime JRE to JDK of your system before building process.

Sai Ye Yan Naing Aye
  • 6,622
  • 12
  • 47
  • 65