BAT file to create Java CLASSPATH

2019-01-16 05:45发布

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

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.

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

10条回答
Root(大扎)
2楼-- · 2019-01-16 06:39

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

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

查看更多
对你真心纯属浪费
3楼-- · 2019-01-16 06:41
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
查看更多
劳资没心,怎么记你
4楼-- · 2019-01-16 06:47

Since 6.0, Java supports wildcard classpaths.

Java command-line classpath syntax

查看更多
我命由我不由天
5楼-- · 2019-01-16 06:47

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:

@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.

查看更多
登录 后发表回答