1077

Is there a way to include all the jar files within a directory in the classpath?

I'm trying java -classpath lib/*.jar:. my.package.Program and it is not able to find class files that are certainly in those jars. Do I need to add each jar file to the classpath separately?

Maarten Bodewes
  • 80,169
  • 13
  • 121
  • 225
Chris Serra
  • 12,256
  • 3
  • 23
  • 25
  • 3
    Sorry I've never accepted this. It should be a community wiki. Never used one of the provided answers. I believe I created a shell script that just scanned the lib/ directory and created the classpath from parsing file names. – Chris Serra Mar 28 '11 at 15:02
  • There's some kind of bug in this new Java feature, because it does not work as described. I gave up and used Ant to work around it, as described in one of the answers. – Alex R Apr 03 '11 at 00:17
  • 1
    There is issue with wildcard processing in Windows. http://stackoverflow.com/questions/11607873/escape-wildcard-processing-in-java-classpath/11608165#comment15368871_11608165 – Mike Jul 23 '12 at 08:44
  • At first I thought the `.` after `jar:` is put by mistake but..... The standard symbol for `current directory' is a single period (.) in both Unix and Windows systems. – KNU Nov 20 '14 at 11:21
  • 7
    Short answer: (1) drop the `.jar` part, (2) must have **at least 2 parts, separated by a `;` on Windows** (which is usually `:` elsewhere). For example: `java -classpath ".;lib/*" Program` – Evgeni Sergeev Dec 12 '14 at 01:38

25 Answers25

1223

Using Java 6 or later, the classpath option supports wildcards. Note the following:

  • Use straight quotes (")
  • Use *, not *.jar

Windows

java -cp "Test.jar;lib/*" my.package.MainClass

Unix

java -cp "Test.jar:lib/*" my.package.MainClass

This is similar to Windows, but uses : instead of ;. If you cannot use wildcards, bash allows the following syntax (where lib is the directory containing all the Java archive files):

java -cp "$(printf %s: lib/*.jar)"

(Note that using a classpath is incompatible with the -jar option. See also: Execute jar file with multiple classpath libraries from command prompt)

Understanding Wildcards

From the Classpath document:

Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/* specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

A class path entry that contains * will not match class files. To match both classes and JAR files in a single directory foo, use either foo;foo/* or foo/*;foo. The order chosen determines whether the classes and resources in foo are loaded before JAR files in foo, or vice versa.

Subdirectories are not searched recursively. For example, foo/* looks for JAR files only in foo, not in foo/bar, foo/baz, etc.

The order in which the JAR files in a directory are enumerated in the expanded class path is not specified and may vary from platform to platform and even from moment to moment on the same machine. A well-constructed application should not depend upon any particular order. If a specific order is required then the JAR files can be enumerated explicitly in the class path.

Expansion of wildcards is done early, prior to the invocation of a program's main method, rather than late, during the class-loading process itself. Each element of the input class path containing a wildcard is replaced by the (possibly empty) sequence of elements generated by enumerating the JAR files in the named directory. For example, if the directory foo contains a.jar, b.jar, and c.jar, then the class path foo/* is expanded into foo/a.jar;foo/b.jar;foo/c.jar, and that string would be the value of the system property java.class.path.

The CLASSPATH environment variable is not treated any differently from the -classpath (or -cp) command-line option. That is, wildcards are honored in all these cases. However, class path wildcards are not honored in the Class-Path jar-manifest header.

Note: due to a known bug in java 8, the windows examples must use a backslash preceding entries with a trailing asterisk: https://bugs.openjdk.java.net/browse/JDK-8131329

Socowi
  • 17,678
  • 2
  • 21
  • 39
basszero
  • 28,508
  • 9
  • 50
  • 76
  • We're actually using Java 5 at the moment. But that is good to know for the future, thanks for the tip! – Chris Serra Oct 21 '08 at 04:25
  • Awesome. Something like this works for me:java -cp target/classes;target/lib/* de.byteconsult.Main – Tom Nov 19 '10 at 11:58
  • 2
    The feature is poorly documented, and seems to require some less-than-obvious pre-conditions to be satisfied in order to work as intended. – Alex R Apr 03 '11 at 00:20
  • Lord Torgamus below provides an example and explanation. – Ellen Spertus Jun 12 '12 at 18:54
  • You can also use find: `cp=$(find /path/to/std/jars /path/to/your/jar -not -type d -printf "%p:")` – JohnnyLambada Aug 15 '12 at 21:01
  • 1
    +1 for the last bash/tr trick. Java/JamVM here doesn't like wildcards for paths outside the working directory, but explicitly referencing each JAR using shell wildcard + `tr` works! – Supr May 15 '13 at 13:13
  • 2
    I have a command `java -classpath /jars/*:/anotherJarsDir/* com.test.MyClass` without any quotes and it works fine. I'm wondering why shell isn't expanding it and erroring out? – yellavon Mar 03 '14 at 14:01
  • Mind that `java -cp "Test.jar;lib/*" my.package.MainClass` works but if the classpath parameter is given as second argument, it does not work (`java my.package.MainClass -cp "Test.jar;lib/*"`). – AndiDog Mar 25 '15 at 21:56
  • 3
    Also don't use `~` in the -cp – Sohail Si Aug 10 '15 at 20:01
  • Following is not valid as well java -cp = " " like you use in set classpath=" " – manifold Jul 18 '16 at 08:36
  • Single quotes should be used in bash – Konstantin Pelepelin May 05 '18 at 12:11
  • 1
    Your windows example doesn't work with java 8 or earlier, but would with this classpath: Test.jar;lib\\* ... forward slash is okay except when preceding an asterisk and a few others ... see https://bugs.openjdk.java.net/browse/JDK-8131329 – philwalk Jul 18 '18 at 16:32
  • In Cygwin we need to use semicolon (`;`) separator instead of colon (`:`) – Peter Jun 19 '19 at 15:34
  • @basszero With double quotes, It worked on MacOS 10.15.2 Catalina. Thanks :) +1 for this. – Anish B. Jan 04 '20 at 17:17
  • what about older version than java 6 @basszero – Aman Mar 21 '20 at 11:31
  • java -cp "Test.jar;lib/*" my.package.MainClass will this not look for children folders under lib? Eg my jar is in directory lib/org/abc.jar – Hardik Rana Aug 20 '20 at 06:46
241

Under Windows this works:

java -cp "Test.jar;lib/*" my.package.MainClass

and this does not work:

java -cp "Test.jar;lib/*.jar" my.package.MainClass

Notice the *.jar, so the * wildcard should be used alone.


On Linux, the following works:

java -cp "Test.jar:lib/*" my.package.MainClass

The separators are colons instead of semicolons.

informatik01
  • 15,174
  • 9
  • 67
  • 100
davorp
  • 3,676
  • 3
  • 24
  • 33
  • 19
    The perfect answer. 2 important things to notice: 1) Use quotes and 2) Use * only, not *.jar – Wim Deblauwe Dec 03 '12 at 10:20
  • 4
    A year and 8 months later, the edit I made to include the UNIX version saved me yet again. :) Funny how it wouldn't recognize my jar files with `*.jar` but only with `*`. – jmort253 Sep 29 '13 at 00:12
  • I found that the order of classpaths is important (but I don't know why). I was getting errors until I switched order of classpaths. – user13107 Mar 19 '14 at 07:27
  • @jmort253, the thing is, this is not the shell * expanding, but the wildcard is java parsing the classpath, seeing the * and filling in the wildcard – Sebastian May 08 '14 at 19:29
  • 1
    @SebastianGodelet - Yeh, that's just me getting confused between Regex wildcards and this notation, which isn't the same I guess. Mostly, what saved me is knowing the difference between `:` on one platform and `;` on the other. :) I compile with Java from the command line about once per year, just enough not to remember how yet often enough to be annoying. – jmort253 May 08 '14 at 19:33
  • java -cp "Test.jar;lib/*" my.package.MainClass will this not look for children folders under lib? Eg my jar is in directory lib/org/abc.ja – Hardik Rana Aug 20 '20 at 07:14
69

We get around this problem by deploying a main jar file myapp.jar which contains a manifest (Manifest.mf) file specifying a classpath with the other required jars, which are then deployed alongside it. In this case, you only need to declare java -jar myapp.jar when running the code.

So if you deploy the main jar into some directory, and then put the dependent jars into a lib folder beneath that, the manifest looks like:

Manifest-Version: 1.0
Implementation-Title: myapp
Implementation-Version: 1.0.1
Class-Path: lib/dep1.jar lib/dep2.jar

NB: this is platform-independent - we can use the same jars to launch on a UNIX server or on a Windows PC.

bourbert
  • 198
  • 13
oxbow_lakes
  • 129,207
  • 53
  • 306
  • 443
  • This seems to work for a lot of people, however, Java seems to plainly ignore the Class-Path entries in the manifest file here. We cannot run the application without manually adding "lib/*" to the classpath using -cp. Any ideas? – Raku Oct 12 '11 at 11:36
  • 6
    oxbow_lakes's answer isn't entirely correct; the Class-Path thing is honored (and ONLY that is honored; -cp/-classpath is ignored!) if you start this jar with java -jar myapp.jar. I presume oxbow_lakes meant to write that when he wrote 'java -classpath myapp.jar'. – rzwitserloot Jan 08 '13 at 13:00
51

My solution on Ubuntu 10.04 using java-sun 1.6.0_24 having all jars in "lib" directory:

java -cp .:lib/* my.main.Class

If this fails, the following command should work (prints out all *.jars in lib directory to the classpath param)

java -cp $(for i in lib/*.jar ; do echo -n $i: ; done). my.main.Class
Habi
  • 519
  • 4
  • 3
  • 4
    a funny note. java -cp lib/* my.main.Class will fail always because shell glob expansion of lib/*, while java -cp .:lib/* my.main.Class will not because .:lib/* is not a valid glob path. Take a while to note that – albfan Mar 22 '12 at 22:03
  • 1
    This does not work; linux will expand the *. you can try: java -cp '.:lib/*' and that works fine (note the single quotes! It won't work with double quotes!). Actually, .:lib/* might work if that's not a legit glob due to the colon, but it feels a bit iffy. I'd add the quotes. The single quotes tell bash to not touch any part of the contents. – rzwitserloot Jan 08 '13 at 13:01
  • It does not matter (in this context) if you use single or double quotes. You want to prevent the shell from expanding (globbing) the *, that is all. And pass the text "lib/\*" litterally to the JVM, so the VM recognices this as a "special pattern" and searches by itself for jar files. – Angel O'Sphere May 30 '16 at 13:05
42

Short answer: java -classpath lib/*:. my.package.Program

Oracle provides documentation on using wildcards in classpaths here for Java 6 and here for Java 7, under the section heading Understanding class path wildcards. (As I write this, the two pages contain the same information.) Here's a summary of the highlights:

  • In general, to include all of the JARs in a given directory, you can use the wildcard * (not *.jar).

  • The wildcard only matches JARs, not class files; to get all classes in a directory, just end the classpath entry at the directory name.

  • The above two options can be combined to include all JAR and class files in a directory, and the usual classpath precedence rules apply. E.g. -cp /classes;/jars/*

  • The wildcard will not search for JARs in subdirectories.

  • The above bullet points are true if you use the CLASSPATH system property or the -cp or -classpath command line flags. However, if you use the Class-Path JAR manifest header (as you might do with an ant build file), wildcards will not be honored.

Yes, my first link is the same one provided in the top-scoring answer (which I have no hope of overtaking), but that answer doesn't provide much explanation beyond the link. Since that sort of behavior is discouraged on Stack Overflow these days, I thought I'd expand on it.

Community
  • 1
  • 1
Pops
  • 28,257
  • 34
  • 127
  • 149
  • my problem was with lib/*.jar rather than lib/*. Thanks a lot this fixed it. I noticed that there's a difference between : and ; but that could be my testing-many-changes-at-the-same-time kind of thing. – Eyad Ebrahim Sep 19 '12 at 08:05
  • Thanks for the emphasising the difference between * and *.jar – burak Aug 10 '18 at 11:56
  • I am not sure why having the additional classpath entry for "." fixed my problem, but it works now. "." is the same directory as the directory I specified with the wildcard. I can't see why this would matter, but it did. Thanks! – Datbates Jul 20 '20 at 16:24
40

Windows:

 java -cp file.jar;dir/* my.app.ClassName

Linux:

 java -cp file.jar:dir/* my.app.ClassName

Remind:
- Windows path separator is ;
- Linux path separator is :
- In Windows if cp argument does not contains white space, the "quotes" is optional

Corey
  • 977
  • 3
  • 17
  • 28
Wender
  • 885
  • 12
  • 20
  • windows example doesn't work for java 8 and earlier: see https://bugs.openjdk.java.net/browse/JDK-8131329 – philwalk Jul 18 '18 at 16:34
  • Maybe does not work for open JDK, I will test this and I will talk about here – Wender Jul 18 '18 at 18:31
  • Sorry, I did test with HotSpot and I did think that works with openjdk. – Wender Sep 17 '18 at 18:15
  • Oracle java under windows requires a backslash prior to the asterisk rather than a forward slash, although I haven't re-tested the most recent or alternate java versions. – philwalk Sep 21 '18 at 15:37
  • java -cp "Test.jar;lib/*" my.package.MainClass will this not look for children folders under lib? Eg my jar is in directory lib/org/abc.ja – Hardik Rana Aug 20 '20 at 07:14
39

For me this works in windows .

java -cp "/lib/*;" sample

For linux

java -cp "/lib/*:" sample

I am using Java 6

SANN3
  • 7,856
  • 4
  • 54
  • 84
31

You can try java -Djava.ext.dirs=jarDirectory http://docs.oracle.com/javase/6/docs/technotes/guides/extensions/spec.html

Directory for external jars when running java

Afshin Moazami
  • 2,003
  • 5
  • 32
  • 54
Navi
  • 7,648
  • 4
  • 32
  • 31
  • 4
    This works, but watch out, pass the `-Djava.ext.dirs=` BEFORE `-jar` – Giovanni Funchal Jun 04 '10 at 13:31
  • 6
    java.ext.dirs will work very different from a normal jar in classpath. It has higher priority and permission which will able to somehow override classes in bootstamp(rt.jar) – Dennis C Nov 30 '10 at 01:56
  • Thanks. On 'java version "1.8.0_221" Java(TM) SE Runtime Environment (build 1.8.0_221-b27) Java HotSpot(TM) 64-Bit Server VM (build 25.221-b27, mixed mode)', only this -D version of passing in the classpath worked. The traditional form did not. – Matt Campbell Nov 05 '19 at 22:29
26

Correct:

java -classpath "lib/*:." my.package.Program

Incorrect:

java -classpath "lib/a*.jar:." my.package.Program
java -classpath "lib/a*:."     my.package.Program
java -classpath "lib/*.jar:."  my.package.Program
java -classpath  lib/*:.       my.package.Program
rvernica
  • 1,153
  • 10
  • 11
11

If you are using Java 6, then you can use wildcards in the classpath.

Now it is possible to use wildcards in classpath definition:

javac -cp libs/* -verbose -encoding UTF-8 src/mypackage/*.java  -d build/classes

Ref: http://www.rekk.de/bloggy/2008/add-all-jars-in-a-directory-to-classpath-with-java-se-6-using-wildcards/

SANN3
  • 7,856
  • 4
  • 54
  • 84
prakash
  • 136
  • 1
  • 3
9

Please note that wildcard expansion is broken for Java 7 on Windows.

Check out this StackOverflow issue for more information.

The workaround is to put a semicolon right after the wildcard. java -cp "somewhere/*;"

Community
  • 1
  • 1
Jake Toronto
  • 3,254
  • 2
  • 21
  • 25
9

If you really need to specify all the .jar files dynamically you could use shell scripts, or Apache Ant. There's a commons project called Commons Launcher which basically lets you specify your startup script as an ant build file (if you see what I mean).

Then, you can specify something like:

<path id="base.class.path">
    <pathelement path="${resources.dir}"/>
    <fileset dir="${extensions.dir}" includes="*.jar" />
    <fileset dir="${lib.dir}" includes="*.jar"/>
</path>

In your launch build file, which will launch your application with the correct classpath.

8

To whom it may concern,

I found this strange behaviour on Windows under an MSYS/MinGW shell.

Works:

$ javac -cp '.;c:\Programs\COMSOL44\plugins\*' Reclaim.java

Doesn't work:

$ javac -cp 'c:\Programs\COMSOL44\plugins\*' Reclaim.java
javac: invalid flag: c:\Programs\COMSOL44\plugins\com.comsol.aco_1.0.0.jar
Usage: javac <options> <source files>
use -help for a list of possible options

I am quite sure that the wildcard is not expanded by the shell, because e.g.

$ echo './*'
./*

(Tried it with another program too, rather than the built-in echo, with the same result.)

I believe that it's javac which is trying to expand it, and it behaves differently whether there is a semicolon in the argument or not. First, it may be trying to expand all arguments that look like paths. And only then it would parse them, with -cp taking only the following token. (Note that com.comsol.aco_1.0.0.jar is the second JAR in that directory.) That's all a guess.

This is

$ javac -version
javac 1.7.0
Evgeni Sergeev
  • 18,558
  • 15
  • 94
  • 112
5

All the above solutions work great if you develop and run the Java application outside any IDE like Eclipse or Netbeans.

If you are on Windows 7 and used Eclipse IDE for Development in Java, you might run into issues if using Command Prompt to run the class files built inside Eclipse.

E.g. Your source code in Eclipse is having the following package hierarchy: edu.sjsu.myapp.Main.java

You have json.jar as an external dependency for the Main.java

When you try running Main.java from within Eclipse, it will run without any issues.

But when you try running this using Command Prompt after compiling Main.java in Eclipse, it will shoot some weird errors saying "ClassNotDef Error blah blah".

I assume you are in the working directory of your source code !!

Use the following syntax to run it from command prompt:

  1. javac -cp ".;json.jar" Main.java

  2. java -cp ".;json.jar" edu.sjsu.myapp.Main

    [Don't miss the . above]

This is because you have placed the Main.java inside the package edu.sjsu.myapp and java.exe will look for the exact pattern.

Hope it helps !!

realPK
  • 1,850
  • 24
  • 20
4

Short Form: If your main is within a jar, you'll probably need an additional '-jar pathTo/yourJar/YourJarsName.jar ' explicitly declared to get it working (even though 'YourJarsName.jar' was on the classpath) (or, expressed to answer the original question that was asked 5 years ago: you don't need to redeclare each jar explicitly, but does seem, even with java6 you need to redeclare your own jar ...)


Long Form: (I've made this explicit to the point that I hope even interlopers to java can make use of this)

Like many here I'm using eclipse to export jars: (File->Export-->'Runnable JAR File'). There are three options on 'Library handling' eclipse (Juno) offers:

opt1: "Extract required libraries into generated JAR"
opt2: "Package required libraries into generated JAR"
opt3: "Copy required libraries into a sub-folder next to the generated JAR"

Typically I'd use opt2 (and opt1 was definitely breaking), however native code in one of the jars I'm using I discovered breaks with the handy "jarinjar" trick that eclipse leverages when you choose that option. Even after realizing I needed opt3, and then finding this StackOverflow entry, it still took me some time to figure it out how to launch my main outside of eclipse, so here's what worked for me, as it's useful for others...


If you named your jar: "fooBarTheJarFile.jar" and all is set to export to the dir: "/theFully/qualifiedPath/toYourChosenDir".

(meaning the 'Export destination' field will read: '/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar' )

After you hit finish, you'll find eclipse then puts all the libraries into a folder named 'fooBarTheJarFile_lib' within that export directory, giving you something like:

/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar01.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar02.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar03.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar04.jar

You can then launch from anywhere on your system with:

java -classpath "/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/*" -jar  /theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar   package.path_to.the_class_with.your_main.TheClassWithYourMain

(For Java Newbies: 'package.path_to.the_class_with.your_main' is the declared package-path that you'll find at the top of the 'TheClassWithYourMain.java' file that contains the 'main(String[] args){...}' that you wish to run from outside java)


The pitfall to notice: is that having 'fooBarTheJarFile.jar' within the list of jars on your declared classpath is not enough. You need to explicitly declare '-jar', and redeclare the location of that jar.

e.g. this breaks:

 java -classpath "/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar;/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/*"  somepackages.inside.yourJar.leadingToTheMain.TheClassWithYourMain

restated with relative paths:

cd /theFully/qualifiedPath/toYourChosenDir/;
BREAKS:  java -cp "fooBarTheJarFile_lib/*"                                package.path_to.the_class_with.your_main.TheClassWithYourMain    
BREAKS:  java -cp ".;fooBarTheJarFile_lib/*"                              package.path_to.the_class_with.your_main.TheClassWithYourMain   
BREAKS:  java -cp ".;fooBarTheJarFile_lib/*"   -jar                       package.path_to.the_class_with.your_main.TheClassWithYourMain   
WORKS:   java -cp ".;fooBarTheJarFile_lib/*"   -jar  fooBarTheJarFile.jar package.path_to.the_class_with.your_main.TheClassWithYourMain   

(using java version "1.6.0_27"; via OpenJDK 64-Bit Server VM on ubuntu 12.04)

Sid
  • 4,510
  • 14
  • 51
  • 100
Matt S.
  • 818
  • 10
  • 21
4

For windows quotes are required and ; should be used as separator. e.g.:

java -cp "target\\*;target\\dependency\\*" my.package.Main
om-nom-nom
  • 60,231
  • 11
  • 174
  • 223
Alexey
  • 8,424
  • 4
  • 55
  • 75
4

macOS, current folder

For Java 13 on macOS Mojave

If all your .jar files are in the same folder, use cd to make that your current working directory. Verify with pwd.

For the -classpath you must first list the JAR file for your app. Using a colon character : as a delimiter, append an asterisk * to get all other JAR files within the same folder. Lastly, pass the full package name of the class with your main method.

For example, for an app in a JAR file named my_app.jar with a main method in a class named App in a package named com.example, alongside some needed jars in the same folder:

java -classpath my_app.jar:* com.example.App
Basil Bourque
  • 218,480
  • 72
  • 657
  • 915
3

class from wepapp:

  > mvn clean install

  > java -cp "webapp/target/webapp-1.17.0-SNAPSHOT/WEB-INF/lib/tool-jar-1.17.0-SNAPSHOT.jar;webapp/target/webapp-1.17.0-SNAPSHOT/WEB-INF/lib/*" com.xx.xx.util.EncryptorUtils param1 param2
Mindaugas K.
  • 282
  • 3
  • 9
3

The only way I know how is to do it individually, for example:

setenv CLASSPATH /User/username/newfolder/jarfile.jar:jarfile2.jar:jarfile3.jar:.

Hope that helps!

om-nom-nom
  • 60,231
  • 11
  • 174
  • 223
ParseTheData
  • 147
  • 2
  • 4
  • 8
2

You need to add them all separately. Alternatively, if you really need to just specify a directory, you can unjar everything into one dir and add that to your classpath. I don't recommend this approach however as you risk bizarre problems in classpath versioning and unmanagability.

Daniel Spiewak
  • 52,267
  • 12
  • 104
  • 120
2

Not a direct solution to being able to set /* to -cp but I hope you could use the following script to ease the situation a bit for dynamic class-paths and lib directories.

 libDir2Scan4jars="../test";cp=""; for j in `ls ${libDir2Scan4jars}/*.jar`; do if [ "$j" != "" ]; then cp=$cp:$j; fi; done; echo $cp| cut -c2-${#cp} > .tmpCP.tmp; export tmpCLASSPATH=`cat .tmpCP.tmp`; if [ "$tmpCLASSPATH" != "" ]; then echo .; echo "classpath set, you can now use  ~>         java -cp \$tmpCLASSPATH"; echo .; else echo .; echo "Error please check libDir2Scan4jars path"; echo .; fi; 

Scripted for Linux, could have a similar one for windows too. If proper directory is provided as input to the "libDir2Scan4jars"; the script will scan all the jars and create a classpath string and export it to a env variable "tmpCLASSPATH".

Sreesankar
  • 301
  • 2
  • 6
1

Think of a jar file as the root of a directory structure. Yes, you need to add them all separately.

1

Set the classpath in a way suitable multiple jars and current directory's class files.

CLASSPATH=${ORACLE_HOME}/jdbc/lib/ojdbc6.jar:${ORACLE_HOME}/jdbc/lib/ojdbc14.jar:${ORACLE_HOME}/jdbc/lib/nls_charset12.jar; 
CLASSPATH=$CLASSPATH:/export/home/gs806e/tops/jconn2.jar:.;
export CLASSPATH
Girdhar Singh Rathore
  • 3,743
  • 4
  • 43
  • 61
0

I have multiple jars in a folder. The below command worked for me in JDK1.8 to include all jars present in the folder. Please note that to include in quotes if you have a space in the classpath

Windows

Compiling: javac -classpath "C:\My Jars\sdk\lib\*" c:\programs\MyProgram.java

Running: java -classpath "C:\My Jars\sdk\lib\*;c:\programs" MyProgram

Linux

Compiling: javac -classpath "/home/guestuser/My Jars/sdk/lib/*" MyProgram.java

Running: java -classpath "/home/guestuser/My Jars/sdk/lib/*:/home/guestuser/programs" MyProgram

vkrams
  • 6,227
  • 17
  • 67
  • 113
0

Order of arguments to java command is also important:

c:\projects\CloudMirror>java Javaside -cp "jna-5.6.0.jar;.\"
Error: Unable to initialize main class Javaside
Caused by: java.lang.NoClassDefFoundError: com/sun/jna/Callback

versus

c:\projects\CloudMirror>java -cp "jna-5.6.0.jar;.\" Javaside
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable
kellogs
  • 2,645
  • 2
  • 33
  • 46