sattu
sattu

Reputation: 648

How to run a java code from command prompt which is using API?

I have written code that uses classes in jar files for Twitter4J API. I want to run my code through a command prompt. How would I do that? I tried:

$ javac -cp [path-to-twitter4j-jars] MyCode.java

It got compiled successfully, but when i run it by

$ java MyCode

But it does not find the Twitter4J classes. I am creating an instance of that class in my code.

Could anybody please help me on this issue?

Upvotes: 2

Views: 1490

Answers (4)

lichengwu
lichengwu

Reputation: 4307

Add jars to classpath:

java -cp .:path/to/twitter4j/jars MyCode

Docs:

where options include:
-d32      use a 32-bit data model if available
-d64      use a 64-bit data model if available
-server   to select the "server" VM
              The default VM is server,
              because you are running on a server-class machine.


-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
              A : separated list of directories, JAR archives,
              and ZIP archives to search for class files.
-D<name>=<value>
              set a system property
-verbose:[class|gc|jni]
              enable verbose output
-version      print product version and exit
-version:<value>
              require the specified version to run
-showversion  print product version and continue
-jre-restrict-search | -no-jre-restrict-search
              include/exclude user private JREs in the version search
-? -help      print this help message
-X            print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
              enable assertions with specified granularity
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
              disable assertions with specified granularity
-esa | -enablesystemassertions
              enable system assertions
-dsa | -disablesystemassertions
              disable system assertions
-agentlib:<libname>[=<options>]
              load native agent library <libname>, e.g. -agentlib:hprof
              see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
              load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
              load Java programming language agent, see java.lang.instrument
-splash:<imagepath>
              show splash screen with specified image

Upvotes: 2

rgettman
rgettman

Reputation: 178353

When running your Java application, the JVM needs to know about the jars also.

java -cp [path-to-twitter4j-jars] MyCode

Here's a link to java command-line options.

Upvotes: 2

Steven Bakhtiari
Steven Bakhtiari

Reputation: 3266

The JAR files still need to be on the classpath when running your application, not just when compiling.

Upvotes: 2

SLaks
SLaks

Reputation: 888283

The classpath is where Java searches for actual .class files in fodlers by package.
Java does not look at JARs that are inside classpath folders.

You need to put the JAR file itself in the classpath; not the folder that contains it.

(alternatively, you can include folder/*.jar to put every JAR in that folder in the classpath)

Upvotes: 3

Related Questions