如何从 USB 驱动器的 jar 文件中执行运行时命令?

How to execute a Runtime command in a jar file from a USB drive?

我的 Java 应用程序 运行 是这样的命令:

String Command="C:\A_Test\Dir_PC_Customizer\Chinese_English_Dictionary_PinItem.cmd";
Runtime.getRuntime().exec(Command);

我把它打包成一个 Jar 文件,运行从 C:/ 驱动器就可以了。 但是我把它复制到USB钥匙链驱动器后,它不会运行。

我的 Chinese_English_Dictionary_PinItem.cmd 文件是:

@echo off
set ITEM=
set TASKBAR=

set ITEM=/item:"C:\A_Test\Dir_Chinese_English_Dictionary\Chinese_English_Dictionary.exe"
set TASKBAR=/taskbar

echo on
:: Pin to Taskbar
cscript //nologo PinItem.wsf %ITEM% %TASKBAR%

当它是来自 C:/ 的 运行 时,输出如下所示:

Command = C:\A_Test\Dir_PC_Customizer\Chinese_English_Dictionary_PinItem.cmd
Result : 
Executing : C:\A_Test\Dir_PC_Customizer\Chinese_English_Dictionary_PinItem.cmd

C:\Dir_PC_Customizer>cscript //nologo PinItem.wsf /item:"C:\A_Test\Dir_Chinese_English_Dictionary\Chinese_English_Dictionary.exe" /taskbar 
Property item is now = C:\A_Test\Dir_Chinese_English_Dictionary\Chinese_English_Dictionary.exe
Property taskbar is now = 
Microsoft Deployment Toolkit version: 6.1.2373.0
------------ Initialization PinItem -------------
The /item switch specified with value: C:\A_Test\Dir_Chinese_English_Dictionary\Chinese_English_Dictionary.exe
The /taskbar switch was specified.
Function-GetSpecialFolder: Cannot determine special folder for CSIDL_PROFILES
Item "C:\A_Test\Dir_Chinese_English_Dictionary\Chinese_English_Dictionary.exe" pinned successfully
------------ Departing PinItem -------------
PinItem processing completed successfully.

但是当它是来自 USB 驱动器的 运行 时,输出如下所示:

Executing : C:\A_Test\Dir_PC_Customizer\Chinese_English_Dictionary_PinItem.cmd

F:\Dir_PC_Customizer\dist>cscript //nologo PinItem.wsf /item:"C:\A_Test\Dir_Chinese_English_Dictionary\Chinese_English_Dictionary.exe" /taskbar 
Input Error: Can not find script file "F:\Dir_PC_Customizer\dist\PinItem.wsf".

注意:他们正在执行同一个文件:

C:\A_Test\Dir_PC_Customizer\Chinese_English_Dictionary_PinItem.cmd

它们只是从 C:/ 或 F:/

执行

显而易见的是:当 运行 来自 C:/ 时,它是:C:\Dir_PC_Customizer>cscript

当 运行 来自 F:/ 时,它是 F:\Dir_PC_Customizer\dist>cscript

并且错误消息显示:"F:\Dir_PC_Customizer\dist\PinItem.wsf" 未找到,因为 PinItem.wsf 和其他几个文件位于:F:\Dir_PC_Customizer [ 和 C:\A_Test\Dir_PC_Customizer ] ,不在 dist.

我的问题是:如果我想让它能够从任何驱动器 运行:C:/、F:/ 或 G:/,我应该怎么做?

我猜一种方法是告诉它 "although I'm running in "F:\Dir_PC_Customizer\dist",但假装我来自 "C:\A_Test\Dir_PC_Customizer"运行ning,但是如何告诉它切换目录?这是关键。

我找到了答案,当应用程序运行时,将多余的几个文件从父目录复制到当前目录以便找到它们。