How to wildcard include JAR files when compiling?

后端 未结 9 659
面向向阳花
面向向阳花 2020-12-01 05:30

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

import org.apache.mina.common.ByteBuffer;

and ByteBuffer is inside a

相关标签:
9条回答
  • 2020-12-01 05:30

    try including the jar file in your command line so :

    javac MyRtmpClient.java ByteBuffer.jar

    0 讨论(0)
  • 2020-12-01 05:37

    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
    
    0 讨论(0)
  • 2020-12-01 05:38

    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.

    0 讨论(0)
  • 2020-12-01 05:39

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

    0 讨论(0)
  • 2020-12-01 05:42

    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.

    0 讨论(0)
  • 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.
    0 讨论(0)
提交回复
热议问题