用于创建 Java CLASSPATH 的 BAT 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/524081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:48:09  来源:igfitidea点击:

BAT file to create Java CLASSPATH

javawindowscommand-linebatch-file

提问by Binil Thomas

I want to distribute a command-line application written in Java on Windows.

我想在 Windows 上分发用 Java 编写的命令行应用程序。

My application is distributed as a zip file, which has a lib directory entry which has the .jar files needed for invoking my main class. Currently, for Unix environments, I have a shell script which invokes the java command with a CLASSPATH created by appending all files in lib directory.

我的应用程序作为一个 zip 文件分发,它有一个 lib 目录条目,其中包含调用我的主类所需的 .jar 文件。目前,对于 Unix 环境,我有一个 shell 脚本,它使用通过附加 lib 目录中的所有文件创建的 CLASSPATH 调用 java 命令。

How do I write a .BAT file with similar functionality? What is the equivalent of find Unix command in Windows world?

如何编写具有类似功能的 .BAT 文件?Windows 世界中 find Unix 命令的等价物是什么?

采纳答案by Henry B

You want to use the for loop in Batch script

您想在批处理脚本中使用 for 循环

 @echo off
 setLocal EnableDelayedExpansion
 set CLASSPATH="
 for /R ./lib %%a in (*.jar) do (
   set CLASSPATH=!CLASSPATH!;%%a
 )
 set CLASSPATH=!CLASSPATH!"
 echo !CLASSPATH!

This really helped me when I was looking for a batch script to iterate through all the files in a directory, it's about deleting files but it's very useful.

当我正在寻找一个批处理脚本来遍历目录中的所有文件时,这真的帮助了我,它是关于删除文件,但它非常有用。

One-line batch script to delete empty directories

一行删除空目录的批处理脚本

To be honest, use Jon's answer though, far better if all the files are in one directory, this might help you out at another time though.

老实说,尽管使用 Jon 的回答,如果所有文件都在一个目录中会更好,但这可能会在其他时间帮助您。

回答by Jon Skeet

Why would you use find? Presumably you know all the libraries your jar file needs ahead of time, so why not just list them?

为什么要使用查找?想必您已经提前知道您的 jar 文件需要的所有库,那么为什么不列出它们呢?

Alternatively, you could always use -Djava.ext.dirs=liband let it pick up everything that way.

或者,您可以始终使用-Djava.ext.dirs=lib并让它以这种方式接收所有内容。

回答by duffymo

Sounds like an executable JARcould work for you. If you're distributing a ZIP with all the JARs your main routine needs, and you really execute it in a command shell, perhaps you could create an executable JAR with the Main-Class and Classpath defined in the manifest. All your users have to do is double click on the JAR and Bob's your uncle.

听起来像一个可执行的 JAR可以为你工作。如果您要分发包含主要例程所需的所有 JAR 的 ZIP,并且您确实在命令 shell 中执行它,那么也许您可以使用清单中定义的 Main-Class 和 Classpath 创建一个可执行 JAR。您的用户所要做的就是双击 JAR,Bob 就是您的叔叔。

回答by duffymo

What I do when I release command-line JARs for Windows/Linux is to embed all the JAR libraries inside my JAR using the ANT command:

当我为 Windows/Linux 发布命令行 JAR 时,我所做的是使用 ANT 命令将所有 JAR 库嵌入到我的 JAR 中:

       <fileset dir="${module.classes.dir}"/>
       <zipfileset src="${endorsed.lib.dir}/myLibrary.jar"/>

In this way the libraries are melt together with your class files.

通过这种方式,库与您的类文件融合在一起。

It is not the best way of binary reusability, but if the libraries are not so heavy, you have your application executed by simply calling java -jar myApp.jar in any OS.

这不是二进制可重用性的最佳方式,但如果库不是那么重,您可以通过在任何操作系统中简单地调用 java -jar myApp.jar 来执行您的应用程序。

回答by basszero

Since 6.0, Java supports wildcard classpaths.

从 6.0 开始,Java 支持通配符类路径。

Java command-line classpath syntax

Java 命令行类路径语法

回答by Eric Sorensen

A variation if you want the CLASSPATH setting to hold outside of the script and you didn't start the shell with /V, you can do something like this:

如果您希望 CLASSPATH 设置保留在脚本之外并且您没有使用 /V 启动 shell,则可以执行以下操作:

@echo off
FOR /R ./lib %%a in (*.jar) DO CALL :AddToPath %%a
ECHO %CLASSPATH%
GOTO :EOF

:AddToPath
SET CLASSPATH=%1;%CLASSPATH%
GOTO :EOF

Useful for setting up your environment when you are just playing around on the command line.

当您只是在命令行上玩耍时,对于设置环境很有用。

回答by Andrii Khaisin

You may use a bit different way if you use Maven to build your project. Application Assembler Maven Plugin is intended for creating Java apps launchers. This plug-in generate bat file launcher for you and put dependencies in specified folder. Optionally you launcher is able to register you application as a service\demon.

如果您使用 Maven 构建您的项目,您可能会使用一些不同的方式。Application Assembler Maven Plugin 用于创建 Java 应用程序启动器。该插件为您生成bat文件启动器,并将依赖项放在指定文件夹中。或者,您的启动器可以将您的应用程序注册为服务\恶魔。

This is a topic about using it

这是一个关于使用它的主题

This plug-in helps keeping you CM parts of your project DRY.

此插件有助于保持项目的 CM 部分保持干燥。

回答by Shirin Joshi

SET CLASSPATH=""
FOR /R /lib %%a in (*.jar) DO CALL :AddToPath %%a
echo %CLASSPATH%

java -cp %CLASSPATH% com.a.b.MyClass

pause

:AddToPath
SET CLASSPATH=%1;%CLASSPATH%
GOTO :EOF

回答by shridutt kothari

Is there an easier way to do this?

有没有更简单的方法来做到这一点?

Yes;

是的;

Since version 6, you can use class path wildcards.

从版本 6 开始,您可以使用类路径通配符。

javac -cp .;../lib/* yourJavaCodeDir/YourJavaFile.java

See also this related Q&A.

另请参阅此相关问答。

回答by Mike Godin

An option to make things portable is to set the classpath relative to the location of the batch file itself.

使事情可移植的一个选项是设置相对于批处理文件本身位置的类路径。

In Windows (assuming Java 6+, classes in ./bin, jars in ./lib):

在 Windows 中(假设 Java 6+,./bin 中的类,./lib 中的 jars):

@java -classpath %~dp0/bin;%~dp0/lib/* ClassName %1