0

I am trying to create a runnable jar from this java library:

https://github.com/Jefferson-Henrique/GetOldTweets-java

Once created, the jar should be able to run with inputs from the command line.

The main file is this Exporter class:

package me.jhenrique.main;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;

import me.jhenrique.manager.TweetManager;
import me.jhenrique.manager.TwitterCriteria;
import me.jhenrique.model.Tweet;

public class Exporter {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

    public static void main(String[] args) {
        if (args == null || args.length == 0) {
            System.err.println("You must pass some parameters. Use \"-h\" to help.");
            System.exit(0);
        } 

        if (args.length == 1 && args[0].equals("-h")) {
            System.out.println("\nTo use this jar, you can pass the folowing attributes:");
            System.out.println("   username: Username of a specific twitter account (without @)");
            System.out.println("      since: The lower bound date (yyyy-mm-aa)");
            System.out.println("      until: The upper bound date (yyyy-mm-aa)");
            System.out.println("querysearch: A query text to be matched");
            System.out.println("  maxtweets: The maximum number of tweets to retrieve");

            System.out.println("\nExamples:");
            System.out.println("# Example 1 - Get tweets by username [barackobama]");
            System.out.println("java -jar got.jar username=barackobama maxtweets=1\n");

            System.out.println("# Example 2 - Get tweets by query search [europe refugees]");
            System.out.println("java -jar got.jar querysearch=\"europe refugees\" maxtweets=1\n");

            System.out.println("# Example 3 - Get tweets by username and bound dates [barackobama, '2015-09-10', '2015-09-12']");
            System.out.println("java -jar got.jar username=barackobama since=2015-09-10 until=2015-09-12 maxtweets=1");
        } else {
            TwitterCriteria criteria = TwitterCriteria.create();

            for (String parameter : args) {
                String[] parameterSplit = parameter.split("=");

                if (parameterSplit[0].equals("username")) {
                    criteria.setUsername(parameterSplit[1]);
                } else if (parameterSplit[0].equals("since")) {
                    criteria.setSince(parameterSplit[1]);
                } else if (parameterSplit[0].equals("until")) {
                    criteria.setUntil(parameterSplit[1]);
                } else if (parameterSplit[0].equals("querysearch")) {
                    criteria.setQuerySearch(parameterSplit[1]);
                } else if (parameterSplit[0].equals("maxtweets")) {
                    criteria.setMaxTweets(Integer.valueOf(parameterSplit[1]));
                }
            }

            try {
                BufferedWriter bw = new BufferedWriter(new FileWriter("output_got.csv"));
                bw.write("username;date;retweets;favorites;text;geo;mentions;hashtags;id;permalink");
                bw.newLine();

                System.out.println("Searching... \n");
                for (Tweet t : TweetManager.getTweets(criteria)) {
                    bw.write(String.format("%s;%s;%d;%d;\"%s\";%s;%s;%s;\"%s\";%s", t.getUsername(), sdf.format(t.getDate()), t.getRetweets(), t.getFavorites(), t.getText(), t.getGeo(), t.getMentions(), t.getHashtags(), t.getId(), t.getPermalink()));
                    bw.newLine();
                }

                bw.close();

                System.out.println("Done. Output file generated \"output_got.csv\".");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

There is also a TweetManager.java file, TwitterCriteria.java file, and Tweet.java file in the src folder. (The Main.java file just shows examples of how code should be used.) In addition there are two jars in the libs folder for this project, "json-20140107.jar" and "jsoup-1.8.1.jar".

I'm making a small change in the java code and I want to recreate the got.jar file with this change included. I'm using Intellij IDEA. I don't have any java experience, so this process has been pretty guess-and-check so far.

The problem (I think) is that when I run the jar I created, the jar is unable to find the two libs jars "json-20140107.jar" and "jsoup-1.8.1.jar". The output I get from terminal is this:

main_jar$ java -jar main.jar querysearch="europe refugees" maxtweets=1
Searching... 

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient
    at me.jhenrique.main.Exporter.main(Exporter.java:64)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.HttpClient
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

In making the runnable jar in Intellij, I selected "jar" in the Artifacts tab, checked the "Build on make" box, have "Exporter" as my main class, and made sure my class path pointed to the folder containing json and jsoup folders. I also have the json and jsoup libraries listed under "Dependencies" in the Modules tab. (These were all suggestions from previous answers related to this problem.)

enter image description here

enter image description here

enter image description here

I've tried all other suggestions I could find on this topic and nothing has worked yet for avoiding this error. What am I doing wrong?

Edit: Solved - Problem was I needed to use Maven for additional dependency downloads I realized the problem was that I had additional dependencies (besides the json and jsoup jars) that needed to be downloaded automatically by Maven, which wasn't happening because I didn't import the project as a Maven project into Intellij. I didn't know what Maven was at first, but when I looked at the pom.xml file in the code I was using I saw that the apache HttpClient library was referenced in addition to the json and jsoup libraries, and I did not have a jar for the HttpClient library.

Once I imported the project as a Maven project the additional dependencies were automatically downloaded and the exported jar file worked as expected. Thank you to Will for a very clear explanation that assured me the problem wasn't how I was importing the libraries I already had!

I had one final small problem with Maven and Intellij whose solution I found here, in case anyone else gets the "no main manifest attribute" error with Maven and Intellij: Wrong Manifest.mf in IntelliJ IDEA created .jar

dimachidy
  • 110
  • 1
  • 4
  • 9

2 Answers2

0

You should either include the dependencies in the jar or keep the jar with the libraries it depends on. Unfortunately I have too little experience with IDEA (none) to tell you how to make it include include dependencies in a jar.

  • Thank you for the tip Olav - I ended up including the dependencies in the jar once I figured out my problem (which was me not using Maven to automatically download additional dependencies I needed). – dimachidy Jun 27 '16 at 03:55
0

I had a quick go at creating a Jar with a dependency on another Jar.

I first added the library to my Project using IntelliJ

IntelliJ Libraries

I then created the Jar by going to Artifacts and selecting 'From modules with dependencies' From modules with dependencies

I then set the main class for the Jar Set main class for jar

My completed jar setup looked like this.

completed jar setup

Then go to the Build menu and select Build Artifacts and select the build action.

Build jar

You should now have a jar

completed jar

I you open the jar up you should see your programs class files and the class files for any dependencies.

exploeded jar

Hope that helps.

Community
  • 1
  • 1
Will Humphreys
  • 2,677
  • 3
  • 25
  • 26
  • Thank you for the clear explanation Will - after doing exactly as you described I still got the error, which pointed me to the real problem: I wasn't properly using Maven to automatically download the additional dependencies I was missing. I appreciate the screen shots, they were very helpful for sanity checking what I was doing. – dimachidy Jun 27 '16 at 03:32