Search code examples
javacommand-lineclasspathtwitter4j

Unable to run application with Twitter4j from command line


I have included the Twitter4j .jar files in my classpath like so:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path=""/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
    <classpathentry kind="lib" path="C:/twitter4j-3.0.5/lib/twitter4j-core-3.0.5.jar"/>
    <classpathentry kind="lib" path="C:/twitter4j-3.0.5/lib/twitter4j-stream-3.0.5.jar"/>
    <classpathentry kind="output" path=""/>
</classpath>

My application runs fine through my IDE, but when I attempt to run it through the command complaining about not being able to find the StatusListener class which should be inside of the twitter4j-stream-3.0.5.jar:

>java MyTwitterApp
Exception in thread "main" java.lang.NoClassDefFoundError: twitter4j/StatusListener

Is there anything else that I need to do in order to run an application with twitter4j from the command line?


Solution

  • I found the answer here: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

    Specifically, the part that says:

    The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.

    Seems I overlooked this when setting my CLASSPATH Windows Environment Variable. I added the . to my classpath like so:

    .;C:/twitter4j-3.0.5/lib/twitter4j-core-3.0.5.jar;C:/twitter4j-3.0.5/lib/twitter4j-stream-3.0.5.jar
    

    Using a wildcard to simplify this further, it becomes:

    .;C:/twitter4j-3.0.5/lib/*
    

    which allows me to use the java MyTwitterApp syntax correctly. It looks for the associated .jar files that I am using for my project and the . allows me to use the current directory as my classpath for all other Java applications, thus not breaking any compatibility.