运行JAR文件时不加载Swing组件...抛出ClassNotFoundException

时间:2015-10-20 09:36:53

标签: java swing debugging jar

在IDE(BlueJ)中执行代码时运行正常。但是在我创建了包含所有资源和清单的JAR文件之后,Swing组件不再需要加载。当我从命令行运行它而不是使用“java -jar AntiJumbler.jar”双击图标时,抛出了ClassNotFoundException:

  

线程“main”中的异常java.lang.NoClassDefFoundError:   AntiJumbler $ 1           在AntiJumbler。(AntiJumbler.java:37)           在AntiJumbler.main(AntiJumbler.java:182)引起:java.lang.ClassNotFoundException:AntiJumbler $ 1           at java.net.URLClassLoader.findClass(URLClassLoader.java:381)           at java.lang.ClassLoader.loadClass(ClassLoader.java:424)           at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:331)           at java.lang.ClassLoader.loadClass(ClassLoader.java:357)           ......还有2个

这是我的代码:

    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;

    public class AntiJumbler extends JFrame
    {
    JTextField word;    //The user enters the jumbled word here...
    JTextArea solution;    //Used to display messages for the user; Solutions are displayed here...
    JTextArea name, name2;        //a banner that reads ANTIJUMBLER...
    JCheckBox def;

    AntiJumbler()
    {
        //Preparing the JFrame...
        super("AntiJumbler");
        URL url = AntiJumbler.class.getResource("background.png"); 
        setLayout(new BorderLayout());
        setContentPane(new JLabel(new ImageIcon(url)));    //background.png: the background image...
        setSize(500, 300);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        setVisible(true);
        //...The JFrame is now ready.

        //word...
        word = new JTextField();
        word.setForeground(Color.white);;
        Font font_1 = new Font("Arial", Font.BOLD, 14);
        word.setFont(font_1);
        word.setOpaque(false);
        word.setCaretColor(Color.white);
        word.setBackground(new Color(0.5f, 0.5f, 0.5f, 0.5f)) ;
        word.addActionListener(
        new ActionListener()
        {
        public void actionPerformed(ActionEvent ae)
        {
        if(ae.getActionCommand()!=null && ae.getActionCommand().trim().length()>0 && ae.getActionCommand().matches("[a-zA-Z]+"))
        try{
        solution.setText("\nUnjumbling " + ae.getActionCommand() + "...");
        String[] ans = solve(ae.getActionCommand().trim().toLowerCase());
        String str1 = (ans.length==0)?"No":Integer.toString(ans.length);
        String str2 = (ans.length)>1?" matches":" match";
        solution.append("\n\n" + str1 + str2 + " found...");
        for(String temp: ans)
        {
        solution.append("\n\n" + temp);
        if(def.isSelected() && Desktop.isDesktopSupported()) 
        Desktop.getDesktop().browse(new java.net.URI("https://dictionary.reference.com/browse/"+temp));
        }
        }
        catch(Exception e)
        {}
        if(ae.getActionCommand().matches("[a-zA-Z]+")==false && ae.getActionCommand().trim().length()>0)
        solution.setText("\nSorry. Jumbled words can contain only letters.\n\nAntiJumbler is a simple application to unjumble jumbled words.\n\nAntiJumbler is developed by Arpan.");
        word.setText("");
        }
        }
        );
        add("North", word);
        //...word is now ready for use

        //solution...
        solution = new JTextArea();
        solution.setOpaque(false);
        solution.setForeground(Color.white);

        solution.setEditable(false);
        Font font2 = new Font("Arial", Font.BOLD, 15);
        solution.setFont(font2);
        solution.setText("\n\n\n\nAntiJumbler is a simple application to unjumble jumbled words.\nEnter a jumbled word in the above textfield to begin.\nAntiJumbler is developed by Arpan.");

        JScrollPane jp = new JScrollPane(solution);
        jp.setBorder(null);
        jp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jp.getViewport().setOpaque(false);
        jp.setOpaque(false);
        jp.setBackground(new Color(50, 50, 60, 250));
        jp.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
                @Override
                public void adjustmentValueChanged(final AdjustmentEvent e) {
                    repaint();
                }
            });
            jp.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {
                @Override
                public void adjustmentValueChanged(final AdjustmentEvent e) {
                    repaint();
                }
            });
        add("Center", jp);
        //...solution is now ready for use

        //name...
        name = new JTextArea();
        name.setBorder(null);
        Font font = new Font("Algerian", Font.BOLD, 15);
        name.setFont(font);
        name.setForeground(Color.cyan);
        name.setOpaque(false);
        name.setText("A\nN\nT\nI\nJ\nU\nM\nB\nL\nE\nR");
        name.setEditable(false);
        add("West", name);
        name2 = new JTextArea();
        name2.setBorder(null);
        name2.setFont(font);
        name2.setForeground(Color.cyan);
        name2.setOpaque(false);
        name2.setText("A\nN\nT\nI\nJ\nU\nM\nB\nL\nE\nR");
        name2.setEditable(false);
        add("East", name2);
        //...name

        def = new JCheckBox("Open Definitions in Web Browser");
        def.setOpaque(false);
        def.setForeground(Color.white);
        add("South", def);

        setVisible(true);
        revalidate();
        repaint();
    }    

    //This method checks whether two strings are anagrams or not...
    boolean areAnagrams(String x, String y)
    {
        String x_copy = x.replaceAll(" ", "");
        String y_copy = y.replaceAll(" ", "");
        if(x_copy.length()!=y_copy.length())
        return false;
        char x_arr[] = x_copy.toLowerCase().toCharArray();
        char y_arr[] = y_copy.toLowerCase().toCharArray();
        Arrays.sort(x_arr);
        Arrays.sort(y_arr);
        return Arrays.equals(x_arr, y_arr);
    }    

    //This method returns the set of solutions for a jumbled word...
    String[] solve(String x)
    {
        URL url = AntiJumbler.class.getResource("dict.txt"); 
        Scanner sc;
        File dictionary=new File("dict.txt");
        try
        { 
        try
        {
        dictionary = new File(url.toURI());
        }    
        catch(URISyntaxException e)
        {
        dictionary = new File(url.getPath());
        }
        sc = new Scanner(dictionary);
        ArrayList<String> words = new ArrayList<String>();
        while(sc.hasNextLine())
        {
        String i = sc.nextLine();
        if(areAnagrams(i, x))
        words.add(i);
        }
        return (String[])words.toArray(new String[0]);
        }
        catch(FileNotFoundException e)
        {
        final JPanel panel = new JPanel();
        JOptionPane.showMessageDialog(panel, "dict.txt not found in the same folder!", "Error", JOptionPane.ERROR_MESSAGE);
        System.exit(0);
        }
        catch(InputMismatchException e)
        {}
        return new String[0];
    } 

    static public void main(String[] args)throws Exception
    {
        AntiJumbler arpan = new AntiJumbler();
    }
    }

任何形式的帮助都将受到高度赞赏。提前谢谢!

0 个答案:

没有答案