Including all the jars in a directory within the Java classpath

前端 未结 24 3523
鱼传尺愫
鱼传尺愫 2020-11-21 04:25

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 no

相关标签:
24条回答
  • 2020-11-21 05:12

    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.

    0 讨论(0)
  • 2020-11-21 05:12

    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
    
    0 讨论(0)
  • 2020-11-21 05:13

    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.

    0 讨论(0)
  • 2020-11-21 05:14

    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
    
    0 讨论(0)
  • 2020-11-21 05:15

    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

    0 讨论(0)
  • 2020-11-21 05:16

    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)

    0 讨论(0)
提交回复
热议问题