gpt4 book ai didi

java - 如何创建批处理文件来运行系统任何位置的 jar 文件

转载 作者:行者123 更新时间:2023-12-01 12:03:23 24 4
gpt4 key购买 nike

我有一个 MyFile.jar 文件。我需要一个批处理文件来搜索 jar 文件并触发它。

我找到了以下解决方案。但对于该批处理文件和 jar 文件必须位于同一文件夹中。

java -jar %~dp0MyFile.jar %*

我们不知道客户端会将 jar 文件放置在系统中的何处。但他们会将批处理文件放在启动文件夹中。

编辑:我现在设法编写了这个批处理文件。

`ECHO
ECHO Locating the Jar

IF EXIST java -jar MyFile.jar ELSE (GOTO

SearchJar)

:SearchJar
SET the_path=D: & CD\
DIR /S/B File.jar > installer_location.txt
SET /P the_path=< installer_location.txt
if defined the_path goto :FoundIt

E: & CD\
DIR /S/B MyFile.jar > installer_location.txt
SET /P the_path=< installer_location.txt
if defined the_path goto :FoundIt

C: & CD\
DIR /S/B MyFile.jar > installer_location.txt
SET /P the_path=< installer_location.txt
if defined the_path goto :FoundIt

G: & CD\
DIR /S/B MyFile.jar > installer_location.txt
SET /P the_path=< installer_location.txt
if defined the_path goto :FoundIt

H: & CD\
DIR /S/B MyFile.jar > installer_location.txt
SET /P the_path=< installer_location.txt
if defined the_path goto :FoundIt

I: & CD\
DIR /S/B MyFile.jar > installer_location.txt
SET /P the_path=< installer_location.txt
if defined the_path goto :FoundIt
ECHO Jar not found.

:FoundIt
ECHO Jar file found at %the_path%

CALL java -jar %the_path%`

这现在在我的系统中运行。但我不确定是否有更简单的方法来一次检查所有驱动器。

最佳答案

正如 gknicker 和我在上面评论的那样,这就是应该完成的方式。如果您使用安装程序(如 NSIS 或类似程序)来部署 .jar 文件和 .bat 脚本,请让安装程序写入注册表值,然后进行搜索或文件浏览可能永远都不需要。如果安装后移动.jar文件,会弹出 GUI file chooser并让用户浏览到他保存 MyFile.jar 的位置。将更新的位置保存到注册表中。如果再次移动文件则重新提示。

@echo off
setlocal enabledelayedexpansion

set "jarfile=MyFile.jar"
set "regkey=HKLM\Software\Company Name"
set "regvalue=jarfile"

set jarpath=
for /f "tokens=2*" %%I in ('reg query "%regkey%" /v "%regvalue%" 2^>NUL') do (
set "jarpath=%%~J"
)

if not exist "%jarpath%" (
call :chooser jarpath "%jarfile%" "%jarfile% (*.jar)|*.jar"
)

if exist "%jarpath%" (
reg add "%regkey%" /v "%regvalue%" /t REG_SZ /d "%jarpath%" /f 2>&1>NUL
) else (
<NUL set /P "=Unable to locate %jarfile%. This thread will self-destruct in 5 seconds... "
ping -n 6 0.0.0.0 >NUL
echo bang.
exit /b 1
)

java -jar "%jarpath%"

goto :EOF

:chooser <var_to_set> <filename> <filter>
:: based on https://stackoverflow.com/questions/15885132

setlocal enabledelayedexpansion
:: Does powershell.exe exist within %PATH%?
for %%I in (powershell.exe) do if "%%~$PATH:I" neq "" (
set chooser=powershell "Add-Type -AssemblyName System.windows.forms|Out-Null;$f=New-Object System.Windows.Forms.OpenFileDialog;$f.InitialDirectory='%cd%';$f.Filter='%~3';$f.showHelp=$true;$f.Title='Locate %~2';$f.ShowDialog()|Out-Null;$f.FileName"
) else (
rem :: If not, compose and link C# application to open file browser dialog
set chooser=%temp%\chooser.exe
>"%temp%\c.cs" echo using System;using System.Windows.Forms;
>>"%temp%\c.cs" echo class dummy{
>>"%temp%\c.cs" echo public static void Main^(^){
>>"%temp%\c.cs" echo OpenFileDialog f=new OpenFileDialog^(^);
>>"%temp%\c.cs" echo f.InitialDirectory=Environment.CurrentDirectory;
>>"%temp%\c.cs" echo f.Filter="%~3";
>>"%temp%\c.cs" echo f.ShowHelp=true;
>>"%temp%\c.cs" echo f.Title="Locate %~2";
>>"%temp%\c.cs" echo f.ShowDialog^(^);
>>"%temp%\c.cs" echo Console.Write^(f.FileName^);}}
for /f "delims=" %%I in ('dir /b /s "%windir%\microsoft.net\*csc.exe"') do (
if not exist "!chooser!" "%%I" /nologo /out:"!chooser!" "%temp%\c.cs" 2>NUL
)
del "%temp%\c.cs"
if not exist "!chooser!" (
echo Error: Please install .NET 2.0 or newer, or install PowerShell.
goto :EOF
)
)

:: capture choice to a variable
endlocal && for /f "delims=" %%I in ('%chooser%') do set "%~1=%%I"

:: Clean up the mess
del "%temp%\chooser.exe" 2>NUL
goto :EOF
<小时/>

如果您坚持要在所有本地驱动器中搜索MyFile.jar,可以使用wmic Logicaldisk查询驱动器列表。通过添加 where "notdrivetype=4" 排除网络驱动器。您仍然应该将结果保存到注册表中,以防止每次运行出现几分钟的延迟。

@echo off
setlocal

set "jarfile=MyFile.jar"
set "regkey=HKLM\Software\Company Name"
set "regvalue=jarfile"

set jarpath=
for /f "tokens=2*" %%I in ('reg query "%regkey%" /v "%regvalue%" 2^>NUL') do (
set "jarpath=%%~J"
)

if not exist "%jarpath%" (
call :finder jarpath "%jarfile%"
)

if exist "%jarpath%" (
reg add "%regkey%" /v "%regvalue%" /t REG_SZ /d "%jarpath%" /f 2>&1>NUL
) else (
<NUL set /P "=Unable to locate %jarfile%. This thread will self-destruct in 5 seconds... "
ping -n 6 0.0.0.0 >NUL
echo bang.
exit /b 1
)

java -jar "%jarpath%"

goto :EOF

:finder <var_to_set> <file_to_find>
setlocal
:: in wmic, drivetype=4 is network drive
for /f "tokens=2 delims=:=" %%I in (
'wmic logicaldisk where "not drivetype=4" get DeviceID /format:list ^| find "="'
) do (
<NUL set /P "=Searching %%I: for %~2... "
for /f "delims=" %%a in ('dir /s /b "%%I:\*%~2"') do (
echo %%~dpa
endlocal & set "%~1=%%a"
goto :EOF
)
echo Not found.
)
goto :EOF

关于java - 如何创建批处理文件来运行系统任何位置的 jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27849493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com