0

I have a project in Java but when I export a runnable jar, the program doesn't work. Can anyone help me?

Here is my project code:

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Window.Type;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class Main {
    //main
     static ArrayList<Font> fontList= new ArrayList<Font>();
    String[] alfabe ={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","R","S","T","U","V","X","Q","W","Y","Z",
                      "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","r","s","t","u","v","x","q","w","y","z",
                      "0","1","2","3","4","5","6","7","8","9"};
    String[] trAlfabe = {"a","b","c","ç","d","e","f","g","ğ","h","ı","i","j","k","l","m","n","o","ö","p","r","s","ş","t","u","ü","v","y","z"};
    private JFrame frmKelimeArac;
    private JTextField textField;

    String[] fakeAlfabe = alfabe;
    String[] fakeTrAlfabe = trAlfabe;
    private JTextField textKelime;

    double kelimeSirasi;




    public static void main(String[] args) {
        final File folder = new File("Fonts/");
        listFilesForFolder(folder);
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main window = new Main();
                    window.frmKelimeArac.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

      public static  void listFilesForFolder(final File folder) {
            for (final File fileEntry : folder.listFiles()) {
                if (fileEntry.isDirectory()) {
                    listFilesForFolder(fileEntry);
                } else {
                     Font font;
                    try {
                        font = Font.createFont(Font.TRUETYPE_FONT, fileEntry);
                        font = font.deriveFont(Font.PLAIN,18);
                        fontList.add(font);
                    } catch (FontFormatException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            }
        }



    /**
     * Create the application.
     */
    public Main() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */

    @SuppressWarnings("unchecked")
    private void initialize() {
        frmKelimeArac = new JFrame();
        frmKelimeArac.setType(Type.UTILITY);
        frmKelimeArac.setTitle("Kelime Arac");
        frmKelimeArac.setBounds(100, 100, 691, 399);
        frmKelimeArac.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmKelimeArac.getContentPane().setLayout(null);

        textField = new JTextField();
        textField.setToolTipText("Kelime yaz\u0131n\u0131z");
        textField.setBounds(150, 11, 149, 20);
        frmKelimeArac.getContentPane().add(textField);
        textField.setColumns(10);

        JButton btnNewButton = new JButton("Sorgula");
        btnNewButton.setBounds(177, 42, 89, 23);
        frmKelimeArac.getContentPane().add(btnNewButton);


        final JLabel lblNewLabel = new JLabel("");

        lblNewLabel.setBounds(177, 78, 122, 14);
        frmKelimeArac.getContentPane().add(lblNewLabel);

        final JLabel lblKarakter = new JLabel("");
        lblKarakter.setBounds(177, 103, 122, 14);
        frmKelimeArac.getContentPane().add(lblKarakter);

        final JLabel lblSira = new JLabel("");
        lblSira.setBounds(455, 132, 160, 14);
        frmKelimeArac.getContentPane().add(lblSira);

        final JLabel lblFont = new JLabel("");
        lblFont.setBounds(177, 132, 201, 14);
        frmKelimeArac.getContentPane().add(lblFont);

        @SuppressWarnings("rawtypes")
        final JComboBox comboBox = new JComboBox();
        comboBox.setBounds(455, 11, 160, 20);
        for(Font font:fontList){
            comboBox.addItem(font.getFontName());
        }
        frmKelimeArac.getContentPane().add(comboBox);

        textKelime = new JTextField();
        textKelime.setToolTipText("Karakter");
        textKelime.setBounds(455, 45, 160, 20);
        frmKelimeArac.getContentPane().add(textKelime);
        textKelime.setColumns(10);
        JButton btnSorgula2 = new JButton("Sorgula");
        btnSorgula2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if(textKelime.getText().trim().length()==1){
                    int index = index(fakeAlfabe, textKelime.getText().trim());
                    double aratoplam = fakeAlfabe.length*comboBox.getSelectedIndex();
                    lblSira.setText(kelimeSirasi+"");
                    kelimeSirasi = aratoplam + index;
                }else if(textKelime.getText().trim().length()==2){
                    int index = index(alfabe2li(fakeAlfabe), textKelime.getText().trim());
                    double aratoplam = alfabe2li(fakeAlfabe).length*comboBox.getSelectedIndex();
                    kelimeSirasi = aratoplam + index + fakeAlfabe.length*fontList.size()*fakeAlfabe.length;
                    lblSira.setText(kelimeSirasi+"");
                }
            }
        });
        btnSorgula2.setBounds(487, 69, 89, 23);
        frmKelimeArac.getContentPane().add(btnSorgula2);

        JLabel lblNewLabel_1 = new JLabel("Kelime S\u0131ras\u0131 :");
        lblNewLabel_1.setBounds(71, 78, 89, 14);
        frmKelimeArac.getContentPane().add(lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("Karakter :");
        lblNewLabel_2.setBounds(71, 103, 65, 14);
        frmKelimeArac.getContentPane().add(lblNewLabel_2);

        JLabel lblNewLabel_3 = new JLabel("Font Ad\u0131 :");
        lblNewLabel_3.setBounds(71, 132, 65, 14);
        frmKelimeArac.getContentPane().add(lblNewLabel_3);

        JLabel lblDipNotMaksimum = new JLabel("Dip Not: Maksimum 6 karakter i\u00E7in \u00E7al\u0131\u015Fmaktad\u0131r.");
        lblDipNotMaksimum.setBounds(10, 336, 655, 14);
        frmKelimeArac.getContentPane().add(lblDipNotMaksimum);


    btnNewButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                if(textField.getText().trim().length()<7){
                int sira = hesapla(textField.getText().toLowerCase());
                lblNewLabel.setText(sira+"");
                int karakter = sira%fakeAlfabe.length;
                lblKarakter.setText(karakter+"");
                if(sira<=fakeAlfabe.length*fontList.size()){
                    sira--;
                    lblFont.setText(fontList.get((sira/(fakeAlfabe.length))).getFontName());
                    lblKarakter.setText(fakeAlfabe[sira%(fakeAlfabe.length)]);
                }else{

                    sira--;
                    String[] alfabe2 = alfabe2li(fakeAlfabe);
                    int yenisira =(sira-fakeAlfabe.length*fontList.size());
                    lblFont.setText(fontList.get(yenisira/(fontList.size()*fakeAlfabe.length*fakeAlfabe.length)).getFontName());
                    lblKarakter.setFont(new Font (fontList.get(yenisira/(fontList.size()*fakeAlfabe.length*fakeAlfabe.length)).getName(),Font.PLAIN,16));
                    lblKarakter.setText(alfabe2[yenisira%(alfabe2.length)]);
                }
            }else {
                JOptionPane.showMessageDialog(null, "6 karakterden uzun kelime girmeyiniz.", ";Uyarı", JOptionPane.INFORMATION_MESSAGE);            }
        }
    });


    }



private String[] alfabe2li(String[] karakter){
    String [] alfabe = new String[karakter.length*karakter.length];
    for(int i=0;i<karakter.length;i++){
        for (int j = 0; j < karakter.length; j++) {
            String string = karakter[i]+karakter[j];
            alfabe[i*karakter.length+j]=string;
        }
    }
    return alfabe;
}
    private int hesapla(String kelime) {
        // TODO Auto-a method stub
        int uzunluk = kelime.length();
        String ters="";
        for ( int i = uzunluk - 1 ; i >= 0 ; i-- )
            ters = ters + kelime.charAt(i);
        double toplam = 0;
        //sıra hesaplama
        for(int a=0;a<ters.length();a++){
            if(a==0){
                toplam = index(fakeTrAlfabe,ters.charAt(a)+"");
            }else{
                toplam = toplam+(index(fakeTrAlfabe,ters.charAt(a)+"")-1)*Math.pow(fakeTrAlfabe.length, a);
            }
        }
        double aratoplam=0;
        for(int b=1;b<ters.length();b++){
            aratoplam=aratoplam+Math.pow(fakeTrAlfabe.length, b);
        }

        return ((int)(toplam+aratoplam));
    }
    int index(String [] alfabee,String harf){
        for(int i=0;i<alfabee.length;i++){
            if(alfabee[i].equals(harf)){
                return i+1;
            }
        }

        return -1;
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    What means `the program doesn't work`? Any exceptions? – Jens Mar 17 '15 at 08:06
  • maybe you're missing some directorys by your jar (like [here](http://stackoverflow.com/questions/29082094/runnable-jar-file-not-working-first-to-third-exports-is-working-but-several-expo)in the comments) – Zorian Mar 17 '15 at 08:07
  • 1
    What is it showing? Please update the question with error logs and more info about the problem you are facing. Also it is worth mentioning how you exported the runnable jar. – includeMe Mar 17 '15 at 08:08
  • 1
    run your jar from the command line with "java -jar yourjar.jar" to be able to see any errors it generates – Subler Mar 17 '15 at 08:08
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Mar 17 '15 at 08:09
  • 1
    .. 2) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Mar 17 '15 at 08:12
  • @Jens when i run in the eclipse everything okey but when i exported and try to open .jar not opening anything. i am beginner for java – Hayrettin Çetintaş Mar 17 '15 at 08:15
  • @hiddenhazard how do you run the jar? – Jens Mar 17 '15 at 08:20
  • Please follow the advice offered by @Subler more than 30 minutes ago! With the information from the command line, we might have identified the problem by 20 minutes ago. – Andrew Thompson Mar 17 '15 at 08:44

1 Answers1

-1

I think your problem is the relative path in the following line:

final File folder = new File("Fonts/");

I suppose your jar file is not in the same folder as the "Fonts" folder. So it can't find the path "Fonts/", when you run the jar file.

Try copying your JAR file on the same level as the "Fonts" folder and try it again

/PATHONYOURLOCALDISK/Fonts
/PATHONYOURLOCALDISK/yourjar.jar
JanTheGun
  • 2,165
  • 17
  • 25
  • Moving directories around is at best a debugging technique. This answer should really be a comment, given you are making guesses.. – Andrew Thompson Mar 17 '15 at 10:34