简体   繁体   中英

Unresolved compilation problems:Twitter cannot be resolved to a type

I am building a maven project for auto-posting tweets using twitterj4. I have already added the twitterj4 dependency at pom.xml, however, there is still an error:

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)

My java version is:

JDK: 1.8.201
JRE: 1.8.201
Apache Maven: 3.6.0
I am using eclipse IDE 2018-2019.
I have already had all the twitterj4-...-4.0.7.jar in my lib, in the maven project.

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

}

This is my pom.xml file:

<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>

You don't have to use the POM file from the twitter4j JAR artifact.
The Twitter4j website says that

You can integrate the latest Twitter4J build easily by just including the following lines in your pom.xml.

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

Which means your project's basic pom.xml file will look like

<?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>

And if you need additional dependencies, eg 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>

And so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM