Eclipse导出runnable jar程序时不起作用

时间:2015-03-17 08:05:35

标签: java eclipse swing export runnable

我有一个Java项目但是当我导出一个可运行的jar时,该程序不起作用。任何人都可以帮助我吗?

这是我的项目代码:

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

1 个答案:

答案 0 :(得分:-1)

我认为您的问题是以下行中的相对路径:

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

我认为您的jar文件与“Fonts”文件夹不在同一个文件夹中。因此,当您运行jar文件时,它找不到路径“Fonts /”。

尝试将您的JAR文件复制到与“字体”文件夹相同的级别,然后再次尝试

/PATHONYOURLOCALDISK/Fonts
/PATHONYOURLOCALDISK/yourjar.jar