简体   繁体   English

将jar文件添加到类路径

[英]Add jar files to class path

I am making a build script as a batch file (don't ask me why or suggest alternatives. You won't be helping). 我正在将构建脚本制作为批处理文件(不要问我为什么或建议替代方法。您将无济于事)。 I have a variable called CLASSPATH that I use with the java compiler. 我有一个与Java编译器一起使用的名为CLASSPATH的变量。 CLASSPATH contains paths to numerous directories and jar files. CLASSPATH包含许多目录和jar文件的路径。 In addition to those, I'd like to add every jar file in the .[some-long-path]\\lib\\ directory 除了这些,我还要在。[some-long-path] \\ lib \\目录中添加每个jar文件。

It looks something like this: 看起来像这样:

SET /p dummy=%CLASSPATH%>classpath.tmp~<nul 
SET WAR_LIB_PATH=war\WEB-INF\lib
DIR %WAR_LIB_PATH% /B | findstr /L ".jar" > jars.tmp~
REM Have to put it into an external file
FOR /f %%j in (jars.tmp~) do (
    SET /p dummy=;%WAR_LIB_PATH%\%%j>>classpath.tmp~<nul 
)
SET /P CLASSPATH=<classpath.tmp~
ECHO %CLASSPATH%

This ALMOST works. 此ALMOST有效。 There are just two problems: 只有两个问题:

  • Somehow a space appears between entries, which ruins the classpath. 条目之间会以某种方式出现空格,这会破坏类路径。
  • It abruptly ends after 1024 characters. 它在1024个字符后突然结束。

Can someone help me with this? 有人可以帮我弄这个吗?

If you use java6, it's enough to write dir/* to include all jars in the dir 如果使用java6,则只需编写dir/*就可以在dir中包含所有jar

http://download.oracle.com/javase/6/docs/technotes/tools/windows/javac.html http://download.oracle.com/javase/6/docs/technotes/tools/windows/javac.html

For example, if directory foo contains a.jar and b.JAR, then the class path element foo/* is expanded to A.jar;b.JAR 例如,如果目录foo包含a.jar和b.JAR,则类路径元素foo / *将扩展为A.jar; b.JAR

If you are running javac , then try using the -classpath command-line argument instead of the environment variable, since those variables are size-limited on different operating systems. 如果您正在运行javac ,请尝试使用-classpath命令行参数而不是环境变量,因为这些变量在不同的操作系统上受大小限制。

And purely as a side note, if you are running a program from a JAR (ex java -jar app.jar ), you can add metadata do the JAR file that accomplishes the same thing. 只是作为一个旁注,如果您正在从JAR运行程序(例如java -jar app.jar ),则可以添加元数据来完成相同的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM