【问题标题】:SwingWorker not working when I create a jar file当我创建一个 jar 文件时,SwingWorker 不工作
【发布时间】:2017-07-19 13:04:44
【问题描述】:

我有以下代码:

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

}

当我把它变成一个类文件时,它工作正常,但是当我把它变成一个 jar 文件时,我收到以下错误:

线程“AWT-EventQueue-0”中的异常 java.lang.NoClassDegFoundError: KaraokeMachine$1

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

【问题讨论】:

  • 异常堆栈跟踪也是文本。请不要截图它们 - 将它们作为(格式化!)文本添加到您的问题中!
  • 好的,谢谢。我会这样做的。
  • 更好 - 但现在你删除了“太多”。在这种情况下,可以,但是,通常:可以将“完整”堆栈跟踪复制到问题中。编辑器总是可以删除不需要的东西 - 但他不能添加缺少的东西。
  • 我无法在命令提示符之外复制和粘贴,但如果在这种情况下可以,我会离开它。
  • 是的。完毕。 :) 谢谢

标签: java swing jar noclassdeffounderror swingworker


【解决方案1】:

该异常意味着您的类路径中缺少一些匿名内部类。所以答案很可能是:当您将类“捆绑”到该类文件中时,您忘记可以将类命名为 A$1.class

KaraokeMachine.class 是“主”类,但在这里:

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

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

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

【讨论】:

  • 我的 jar 文件的内容是:META-INF/ META-INF/MANIFEST.MF KaraokeMachine.class everything is awesome.wav 是不是少了什么?
  • 哦,看。它是空的。惊喜。严重的是:您缺乏基本知识;同时,你试图一次学习各种东西。就像用 swing 做 Java UI 编程,然后使用 JAR 文件。我的 2 美分:退后一步。阅读涵盖真正基础知识的教程。当您在该领域没有任何经验时,不要进行“反复试验”......请参阅我关于使用 JAR 顺便说一句的帮助的更新答案。
  • 好的,我来看看真正的基础知识。
  • 好的。没问题。
猜你喜欢
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 2013-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-05
相关资源
最近更新 更多