打包Jar后包Twitter4j不存在
Package Twitter4j does not exist after packaging Jar
我正在尝试使用 Java 创建一个 Twitter 应用程序,运行 来自 Idea 的应用程序工作正常并且发送了推文,但是在 运行 mvn clean package
和 java -jat TwitterClient-1.0-SNAPSHOT.jar
,然后单击发送推文按钮我收到以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: twitter4j/conf/ConfigurationBuilder
at company.fourleafclover.TwitterClient.Main.actionPerformed(Main.java:59)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6617)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6382)
at java.desktop/java.awt.Container.processEvent(Container.java:2264)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4993)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2322)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4934)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4563)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4504)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2308)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2773)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.lang.ClassNotFoundException: twitter4j.conf.ConfigurationBuilder
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
... 36 more
我的主要 class 和 pom.xml:
package company.fourleafclover.TwitterClient;
import twitter4j.*;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.conf.ConfigurationBuilder;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
System.out.println("Started!");
final JFrame frame = new JFrame("Twitter Client");
JLabel lblKey = new JLabel("API Key:");
JTextField tfKey = new JTextField(20);
lblKey.setLabelFor(tfKey);
JLabel lblSecret = new JLabel("API Secret:");
JTextField tfSecret = new JTextField(20);
lblSecret.setLabelFor(tfSecret);
JLabel lblToken = new JLabel("Access Token:");
JTextField tfToken = new JTextField(20);
lblToken.setLabelFor(tfToken);
JLabel lblASecret = new JLabel("Access Secret:");
JTextField tfAsecret = new JTextField(20);
lblASecret.setLabelFor(tfAsecret);
JLabel lblmsg = new JLabel("Message:");
JTextField tfmsg = new JTextField(20);
lblmsg.setLabelFor(tfmsg);
JButton btnGet = new JButton("Send tweet");
btnGet.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String consumerKey = new String(tfKey.getText());
String consumerSecret = new String(tfSecret.getText());
String oAuthAccessToken = new String(tfToken.getText());
String oAuthAccessTokenSecret = new String(tfAsecret.getText());
String Msg = new String(tfmsg.getText());
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(consumerKey)
.setOAuthConsumerSecret(consumerSecret)
.setOAuthAccessToken(oAuthAccessToken)
.setOAuthAccessTokenSecret(oAuthAccessTokenSecret);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
try {
Status status = twitter.updateStatus(Msg);
System.out.println("Successfully updated the status to [" + status.getText() + "].");
} catch (Exception te) {
te.printStackTrace();
}
JOptionPane.showMessageDialog(frame,
"Tweet sent!");
}
});
JButton btnLogin = new JButton("button");
JPanel panel = new JPanel();
panel.setLayout(new SpringLayout());
panel.add(lblKey);//API Key
panel.add(tfKey);//Api Key
panel.add(lblSecret);//Access Secret
panel.add(tfSecret);panel.add(lblToken);//API Token
panel.add(tfToken);//API Token
panel.add(lblASecret);
panel.add(tfAsecret);
panel.add(lblmsg);
panel.add(tfmsg);
panel.add(btnLogin);
panel.add(btnGet);
jtextfielddemo.SpringUtilities.makeCompactGrid(panel,
6, 2, //rows, cols
6, 6, //initX, initY
6, 6); //xPad, yPad
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(470, 200);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>company.fourleafclover</groupId>
<artifactId>TwitterClient</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>4.0.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>company.fourleafclover.TwitterClient.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
我也试过将 Twitter4j-core 下载并导入模块设置,但没有效果。 Module Settings
您的构建标签有问题。
这对我来说很好用。您不必选择带有 jar-with-dependencies
的那个
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Your.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
</build>
我正在尝试使用 Java 创建一个 Twitter 应用程序,运行 来自 Idea 的应用程序工作正常并且发送了推文,但是在 运行 mvn clean package
和 java -jat TwitterClient-1.0-SNAPSHOT.jar
,然后单击发送推文按钮我收到以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: twitter4j/conf/ConfigurationBuilder
at company.fourleafclover.TwitterClient.Main.actionPerformed(Main.java:59)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6617)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6382)
at java.desktop/java.awt.Container.processEvent(Container.java:2264)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4993)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2322)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4934)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4563)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4504)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2308)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2773)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.lang.ClassNotFoundException: twitter4j.conf.ConfigurationBuilder
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
... 36 more
我的主要 class 和 pom.xml:
package company.fourleafclover.TwitterClient;
import twitter4j.*;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.conf.ConfigurationBuilder;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
System.out.println("Started!");
final JFrame frame = new JFrame("Twitter Client");
JLabel lblKey = new JLabel("API Key:");
JTextField tfKey = new JTextField(20);
lblKey.setLabelFor(tfKey);
JLabel lblSecret = new JLabel("API Secret:");
JTextField tfSecret = new JTextField(20);
lblSecret.setLabelFor(tfSecret);
JLabel lblToken = new JLabel("Access Token:");
JTextField tfToken = new JTextField(20);
lblToken.setLabelFor(tfToken);
JLabel lblASecret = new JLabel("Access Secret:");
JTextField tfAsecret = new JTextField(20);
lblASecret.setLabelFor(tfAsecret);
JLabel lblmsg = new JLabel("Message:");
JTextField tfmsg = new JTextField(20);
lblmsg.setLabelFor(tfmsg);
JButton btnGet = new JButton("Send tweet");
btnGet.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String consumerKey = new String(tfKey.getText());
String consumerSecret = new String(tfSecret.getText());
String oAuthAccessToken = new String(tfToken.getText());
String oAuthAccessTokenSecret = new String(tfAsecret.getText());
String Msg = new String(tfmsg.getText());
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(consumerKey)
.setOAuthConsumerSecret(consumerSecret)
.setOAuthAccessToken(oAuthAccessToken)
.setOAuthAccessTokenSecret(oAuthAccessTokenSecret);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
try {
Status status = twitter.updateStatus(Msg);
System.out.println("Successfully updated the status to [" + status.getText() + "].");
} catch (Exception te) {
te.printStackTrace();
}
JOptionPane.showMessageDialog(frame,
"Tweet sent!");
}
});
JButton btnLogin = new JButton("button");
JPanel panel = new JPanel();
panel.setLayout(new SpringLayout());
panel.add(lblKey);//API Key
panel.add(tfKey);//Api Key
panel.add(lblSecret);//Access Secret
panel.add(tfSecret);panel.add(lblToken);//API Token
panel.add(tfToken);//API Token
panel.add(lblASecret);
panel.add(tfAsecret);
panel.add(lblmsg);
panel.add(tfmsg);
panel.add(btnLogin);
panel.add(btnGet);
jtextfielddemo.SpringUtilities.makeCompactGrid(panel,
6, 2, //rows, cols
6, 6, //initX, initY
6, 6); //xPad, yPad
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(470, 200);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>company.fourleafclover</groupId>
<artifactId>TwitterClient</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>4.0.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>company.fourleafclover.TwitterClient.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
我也试过将 Twitter4j-core 下载并导入模块设置,但没有效果。 Module Settings
您的构建标签有问题。
这对我来说很好用。您不必选择带有 jar-with-dependencies
的那个 <build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Your.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
</build>