创建 jar 文件时 SwingWorker 不工作

SwingWorker not working when I create a jar file

我有以下代码:

import javax.swing.*;
import java.awt.event.*;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class KaraokeMachine extends JFrame implements ActionListener
{
ClassLoader Idr = this.getClass().getClassLoader();
java.applet.AudioClip everythingIsAwesome = JApplet.newAudioClip( Idr.getResource( "everything is awesome.wav" ) );
JLabel lbl1 = new JLabel( "" );
JButton btn = new JButton( "Play" );
JPanel pnl = new JPanel();
final Executor executor = Executors.newCachedThreadPool();

public KaraokeMachine()
{
    super( "Karaoke" );
    setSize( 520, 280 );
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    pnl.add( lbl1 );
    pnl.add( btn );
    btn.addActionListener( this );
    add( pnl ); setVisible( true );
}

public void actionPerformed( ActionEvent event )
{
    if ( event.getSource() == btn )
    {
            SwingWorker<Void, String> worker = new SwingWorker<Void, String>()
            {
                @Override
                protected Void doInBackground() throws Exception
                {
                    everythingIsAwesome.play();
                    this.publish("Everything");
                    Thread.sleep( 600 );
                    this.publish("Everything is");
                    Thread.sleep( 400 );
                    this.publish("Everything is Awesome!");
                    Thread.sleep( 2000 );
                    this.publish("Everything");
                    Thread.sleep( 600 );
                    this.publish("Everything is");
                    Thread.sleep( 400 );
                    this.publish("Everything is cool");
                    Thread.sleep( 400 );
                    this.publish("Everything is cool when");
                    Thread.sleep( 400 );
                    this.publish("Everything is cool when you're");
                    Thread.sleep( 400 );
                    this.publish("Everything is cool when you're part");
                    Thread.sleep( 150 );
                    this.publish("Everything is cool when you're part of");
                    Thread.sleep( 150 );
                    this.publish("Everything is cool when you're part of a");
                    Thread.sleep( 150 );
                    this.publish("Everything is cool when you're part of a team");
                    Thread.sleep( 1000 );
                    this.publish("Everything");
                    Thread.sleep( 600 );
                    this.publish("Everything is");
                    Thread.sleep( 400 );
                    this.publish("Everything is Awesome!");
                    Thread.sleep( 1500 );
                    this.publish("When");
                    Thread.sleep( 300 );
                    this.publish("When you're");
                    Thread.sleep( 300 );
                    this.publish("When you're livin'");
                    Thread.sleep( 300 );
                    this.publish("When you're livin' in");
                    Thread.sleep( 300 );
                    this.publish("When you're livin' in a");
                    Thread.sleep( 300 );
                    this.publish("When you're livin' in a dream!");
                    Thread.sleep( 300 );
                    return null;
                }
            @Override
            protected void process( List<String> res )
            {
                for(String text : res)
                {
                    lbl1.setText(text); 
                }
                }
        };
        executor.execute(worker);
    }
}
public static void main( String[] args )
{
    KaraokeMachine karaoke = new KaraokeMachine();
}

}

当我将其制作成 class 文件时,它工作正常,但是当我将其制作成 jar 文件时,出现以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDegFoundError: KaraokeMachine

有谁知道如何更改代码以便 swingworker 在 jar 文件中工作?

该异常意味着您的 class 路径中缺少某些 匿名内部 class。所以答案很可能是:当你 "bundle" 你的 classes 进入那个 class 文件时,你 忘记了 classes 可以被命名为 A.class.

KaraokeMachine.class是"main"class,但是这里:

SwingWorker<Void, String> worker = new SwingWorker<Void, String>() {

您正在创建一个匿名内部 class - 它进入 KaraokeMachine$1.class。为了 运行 您的应用程序,您需要 所有 这些 class 文件存在。

换句话说:你的JAR文件内容不完整。仔细看看您是如何 构建 该 JAR 文件的。例如,参见 here