未解决的编译问题:Twitter无法解析为某种类型

时间:2019-03-24 16:04:17

标签: java maven twitter

我正在构建一个Maven项目,用于使用twitterj4自动发布推文。 我已经在pom.xml中添加了twitterj4依赖项,但是,仍然存在错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Twitter cannot be resolved to a type
    TwitterFactory cannot be resolved to a type
    AccessToken cannot be resolved to a type
    AccessToken cannot be resolved to a type
    TwitterException cannot be resolved to a type

    at com.codebind.TwitterPost.main(TwitterPost.java:18)

我的Java版本是:

JDK:1.8.201
JRE:1.8.201
Apache Maven:3.6.0
我正在使用Eclipse IDE 2018-2019。
我已经在maven项目的lib中拥有所有twitterj4 -...- 4.0.7.jar。

package com.codebind;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;

public class TwitterPost {

    static String consumerKeyStr = "ic4nbUrAprObKHSZ31RNzDUH9";
    static String consumerSecretStr = 
    "6FOY7s3l2ALVAhjiK9H4L2RqvpWLLYOX9jgUOj7LDA5AjAp369";
    static String accessTokenStr = "1106831010304397312- 
    8ND3tBmpHQLbQMUMgTHZcnsHo6mabU";
    static String accessTokenSecretStr = 
    "jsq87Fd7zkKvudHZTtZmAVz8UD98go40kvZYIzJKAdN2n";

    public static void main(String[] args) {

        try {
            Twitter twitter = new TwitterFactory().getInstance();

            twitter.setOAuthConsumer(consumerKeyStr, consumerSecretStr);
            AccessToken accessToken = new 
    AccessToken(accessTokenStr,accessTokenSecretStr);

            twitter.setOAuthAccessToken(accessToken);

            twitter.updateStatus("Post using Twitter4J Again");

            System.out.println("Successfully updated the status in 
    Twitter.");
        } catch (TwitterException te) {
            te.printStackTrace();
        }
    }

}

这是我的pom.xml文件:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.twitter4j</groupId>
    <artifactId>twitter4j</artifactId>
    <version>4.0.7</version>
    <packaging>pom</packaging>
    <name>twitter4j</name>
    <description>A Java library for the Twitter API</description>
    <url>http://twitter4j.org/</url>
    <licenses>
    <license>
        <name>Apache License 2.0</name>
        <url>http://www.apache.org/licenses/LICENSE-2.0</url>
        <distribution>repo</distribution>
    </license>
</licenses>
<scm>
    <url>https://github.com/yusuke/twitter4j</url>

<connection>scm:git:https://github.com/yusuke/twitter4j.git</connection>
    <developerConnection>scm:git:https://github.com/yusuke/twitter4j.git
    </developerConnection>

<tag>4.0.7</tag>
  </scm>
    <developers>
    <developer>
        <id>yusuke</id>
        <name>Yusuke</name>
        <email>yusuke@mac.com</email>
        <url>http://samuraism.jp/</url>
        <roles>
            <role>lead</role>
            <role>architect</role>
            <role>developer</role>
        </roles>
        <timezone>9</timezone>
    </developer>
</developers>
<issueManagement>
    <system>YouTrack</system>
    <url>http://issue.twitter4j.org/youtrack/issues? 
   q=project%3A+Twitter4J</url>
    </issueManagement>
    <mailingLists>
    <mailingList>
        <name>Twitter4J</name>
        <subscribe>twitter4j-subscribe@googlegroups.com</subscribe>
        <unsubscribe>twitter4j-unsubscribe@googlegroups.com</unsubscribe>
        <post>twitter4j@googlegroups.com</post>
        <archive>http://groups.google.com/group/twitter4j</archive>
    </mailingList>
</mailingLists>
<distributionManagement>
    <repository>
        <id>sonatype-nexus-staging</id>
        <name>Nexus Release Repository</name>
        <uniqueVersion>false</uniqueVersion>
        <url>file:/Users/yusuke/maven2/
        </url>
    </repository>
    <snapshotRepository>
        <id>org.twitter4j</id>
        <name>twitter4j.org Repository</name>
        <uniqueVersion>false</uniqueVersion>
        <url>file:${user.home}/maven2/</url>
    </snapshotRepository>
</distributionManagement>
<profiles>
    <profile>
        <id>release-sign-artifacts</id>
        <activation>
            <property>
                <name>performRelease</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-gpg-plugin</artifactId>
                    <version>1.4</version>
                    <executions>
                        <execution>
                            <id>sign-artifacts</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>sign</goal>
                            </goals>
                        </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    <modules>
        <module>twitter4j-async</module>
        <module>twitter4j-core</module>
        <module>twitter4j-examples</module>
        <module>twitter4j-appengine</module>
        <module>twitter4j-http2-support</module>
        <module>twitter4j-stream</module>
    </modules>
    <dependencies>
        <dependency>
            <groupId>org.twitter4j</groupId>
            <artifactId>twitter4j</artifactId>
            <version>4.0.7</version>
            <type>pom</type>
        </dependency>
    </dependencies>
</project>

1 个答案:

答案 0 :(得分:0)

不必使用twitter4j JAR工件中的POM文件。
Twitter4j网站说

  

您只需包含以下内容即可轻松集成最新的Twitter4J构建   pom.xml中的以下几行。

<dependencies>
   <dependency>
      <groupId>org.twitter4j</groupId>
      <artifactId>twitter4j-core</artifactId>
      <version>[4.0,)</version>
    </dependency>
</dependencies>

这意味着您项目的基本pom.xml文件看起来像

<?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>your.group</groupId>
    <artifactId>your-artifact</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.twitter4j</groupId>
            <artifactId>twitter4j-core</artifactId>
            <version>4.0.7</version>
        </dependency>
    </dependencies>
</project>

如果您需要其他依赖项,例如twitter4j-stream

<?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>your.group</groupId>
    <artifactId>your-artifact</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.twitter4j</groupId>
            <artifactId>twitter4j-core</artifactId>
            <version>4.0.7</version>
        </dependency>
        <dependency>
            <groupId>org.twitter4j</groupId>
            <artifactId>twitter4j-stream</artifactId>
            <version>4.0.7</version>
        </dependency>
    </dependencies>
</project>

以此类推。