46

I have the following in a java file (MyRtmpClient.java):

import org.apache.mina.common.ByteBuffer;

and ByteBuffer is inside a JAR file (with the proper directory structure of course). That jar file and others I need are in the same directory as the .java file.

Then I compile with the line:

javac -cp ".;*.jar" MyRtmpClient.java

But I get the error:

MyRtmpClient.java:3: package org.apache.mina.common does not exist
import org.apache.mina.common.ByteBuffer;

How can I include jar files in my project?

djangofan
  • 28,471
  • 61
  • 196
  • 289

9 Answers9

27

your command line is correct, but there are some considerations:

  • you must have javac >= 1.6, because only in that version the compiler parses the "*" as various JAR files.
  • you must be running Windows, because ";" is the path separator for that operating system only (it doesn't work on Unix, the path separator on Unix is ":").

I'm assuming that the JAR file has the proper directory structure as you stated.

Bobby
  • 18,217
  • 15
  • 74
  • 89
cd1
  • 15,908
  • 12
  • 46
  • 47
  • Really? I didn't know that it would parse the *. I guess it's been a while since I compiled anything needing a classpath from the command line. – Michael Myers Jun 30 '09 at 16:00
  • Yours is probably the answer to my dilemma. I have JDK 1.5 :( –  Jun 30 '09 at 16:00
  • this is the source: http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html#options – cd1 Jun 30 '09 at 16:24
16

javac does not understand *.jar in the classpath argument. You need to explicitly specify each jar. e.g.

javac -cp ".;mina.jar" MyRtmpClient.java
Mark
  • 28,783
  • 8
  • 63
  • 92
  • 1
    Yes, * is usually interpreted by the shell, but here it doesn't get a chance. – Michael Myers Jun 30 '09 at 15:57
  • I tried using *.jar on Linux before writing my answer and it didn't work. – Mark Jun 30 '09 at 16:07
  • Depends on JDK version; see above. – Alex Feinman Jun 30 '09 at 16:17
  • So it seems to understand -cp *.jar if I remove the quotes but it does not understand -cp .:*.jar – Mark Jul 01 '09 at 08:06
  • the class path foo/* is expanded into foo/a.jar;foo/b.jar;foo/c.jar – djangofan Dec 09 '12 at 19:10
  • I'm using JDK 1.12 on Ubuntu 12.04 LTS, and to compile a number of packages (and in my case I don't need to do anything with .java files in them) with a single .java file in the same directory, this is what I've used: `javac -cp ".:dropbox-core-sdk-1.7.5.jar:dropbox-core-sdk-1.7.5-javadoc.jar:dropbox-core-sdk-1.7.5-sources.jar:jackson-core-2.2.3.jar:javax.mail.jar" Main.java`. – Andrei Nov 19 '13 at 22:39
  • What does the "." in ".;mina.jar" do? – amey91 Jan 08 '15 at 21:53
  • 1
    I've found it does not support *.jar but it will support *. This is with both of them in quotes so that the command line doesn't alter them, – MikeKulls Nov 04 '15 at 21:34
11

In javac JDK 6 and above You could use (note lack of .jar):

javac -cp ".;*" MyRtmpClient.java

to quote javac - Java programming language compiler

As a special convenience, a class path element containing a basename of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR.

For example, if directory foo contains a.jar and b.JAR, then the class path element foo/* is expanded to A.jar;b.JAR, except that the order of jar files is unspecified. All jar files in the specified directory, even hidden ones, are included in the list. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

Community
  • 1
  • 1
metyl
  • 461
  • 4
  • 7
9

If you have utilities find and tr at your disposal (e.g. you're working Linux), you could do:

javac -cp .:`find * -name "*.jar" | tr "\n" ":"` MyRtmpClient.java

All jar files in the current directory and all it's sub-directories will be added (shell command lists all jar files and puts colons as separators between them).


Explanation:
  • pair of the backticks ( ` ) denote shell commands to be executed,
  • find * -name "*.jar" finds and lists all jar files in hierarchy whose root is current folder,
  • vertical bar ( | ) is pipe; connects output of find to the input of next command,
  • tr "\n" ":" replaces all newline characters with colon characters.
plesiv
  • 6,935
  • 3
  • 26
  • 34
8

In your case, I think JAVAC can not found jars file.

Please try:

PROJECT_PATH
- lib\a.jar
- src\package\b.java

cd @PROJECT_PATH

javac -classpath lib\a.jar src\package\b.java
Xavi
  • 20,111
  • 14
  • 72
  • 63
thanhduyspkt
  • 91
  • 1
  • 2
  • In this case use " javac -cp .;.\lib\\** src\package\b ". That rule will match jars under lib and also wildcard classes in the current directory. – djangofan Dec 09 '12 at 19:13
4

Probably below syntax will work on windows dos command

javac -cp ".;first.jar;second.jar;third.jar" MyRtmpClient.java
shashank
  • 61
  • 1
  • 5
2

In Java 8, the option ".;*" etc. which are mentioned above did not seem to work. I tried and found that : javac -cp '<location of jars>/*' MyRtmpClient.java

works:

<location of jar> can be /usr/local/classes/* or /home/developer/MyProject/*

Ankush
  • 499
  • 1
  • 7
  • 17
-2

try including the jar file in your command line so :

javac MyRtmpClient.java ByteBuffer.jar

blispr
  • 883
  • 5
  • 10
-2

You cannot use -cp with Javac. You have to use -classpath instead (assuming the other settings are correct).

Will
  • 4,585
  • 1
  • 26
  • 48
Rewinna
  • 11