简体   繁体   English

VBA中的Shell命令不使用PATH变量(从VBA执行.jar)

[英]Shell command in VBA not using the PATH variable (executing .jar from VBA)

Under Windows 7 (64-bit) and Office 2010 in a Word VBA macro I'm making a Shell call to java to execute a .jar file. 在Windows 7(64位)和Office 2010在Word VBA宏我正在做一个Shell调用java的执行.jar文件。 The code is part of distribution that supposedly works in Office 2007 (which I don't have). 该代码是分发的一部分,据称可以在Office 2007中使用(我没有)。

ShellCommand = "java -classpath ..."
TaskID = Shell(ShellCommand, ShellWindowState)

Anyway, the Shell command returns a process ID, but the process terminates immediately with no Err.Number I'm not actually able to get much info about why. 无论如何, Shell命令返回一个进程ID,但是该进程立即终止,没有Err.Number我实际上无法获得有关原因的更多信息。

However, I suspected it couldn't find the java command. 但是,我怀疑它找不到java命令。 I verified that my PATH is correct by opening manually a CMD.exe window and running the ShellCommand (which I printed using Debug.Print ). 我通过手动打开CMD.exe窗口并运行ShellCommand (我使用Debug.Print打印)验证了我的PATH是否正确。 Indeed, my PATH variable is set to point to where the latest JRE is installed, which is also evidenced by typing java -version and seeing the proper output. 实际上,我的PATH变量设置为指向安装最新JRE的位置,这也可以通过键入java -version并查看正确的输出来证明。

So, still not trusting Shell from VBA, I hardcoded the ShellCommand as follows: 所以,仍然不相信来自VBA的Shell ,我将ShellCommand硬编码如下:

ShellCommand = "c:\Program Files\Java\jdk1.7.0_72\jre\bin\java -classpath ..."

To my (un)suprise, the Shell command works and my .jar was executed as it should. 对于我(非)的惊讶,Shell命令工作,我的.jar按原样执行。

So, who can tell me why the system PATH variable is ignored in ShellCommand or, better yet, how I can code my VBA program so that whenever I update my JRE on Windows 7 ( java.exe is a moving target every couple of weeks it seems), it will find the java.exe ? 那么,谁能告诉我为什么在ShellCommand忽略系统PATH变量,或者更好的是,我如何编写我的VBA程序以便每当我在Windows 7上更新我的JRE时( java.exe每隔几周java.exe成为一个移动目标)好像),它会找到java.exe吗?

TL;DR: Remove Java 8 TL; DR:删除Java 8

The inventory of my Java installs shows some complexity (!) 我的Java安装库存显示出一些复杂性(!)

  • Java 7 Update 72 Java 7 Update 72
  • Java 7 Update 72 (64-bit) Java 7 Update 72(64位)
  • Java SE Development Kit 7 Update 72 (64-bit) Java SE开发工具包7更新72(64位)
  • Java 8 Update 25 Java 8 Update 25

The first part of the PATH that I displayed with Environ("PATH") shows C:\\ProgramData\\Oracle\\Java\\javapath which is a set of SYMLINK s as follows: 我在Environ("PATH")显示的PATH的第一部分显示了C:\\ProgramData\\Oracle\\Java\\javapath ,它是一组SYMLINK ,如下所示:

C:\ProgramData\Oracle\Java\javapath>dir

 Directory of C:\ProgramData\Oracle\Java\javapath

2014-10-16  10:32    <DIR>          .
2014-10-16  10:32    <DIR>          ..
2014-10-16  10:32    <SYMLINK>      java.exe [C:\Program Files (x86)\Java\jre1.8.0_25\bin\java.exe]
2014-10-16  10:32    <SYMLINK>      javaw.exe [C:\Program Files (x86)\Java\jre1.8.0_25\bin\javaw.exe]
2014-10-16  10:32    <SYMLINK>      javaws.exe [C:\Program Files (x86)\Java\jre1.8.0_25\bin\javaws.exe]

As an experiment, I removed Java 8. 作为实验,我删除了Java 8。

That leaves the javapath setting as follows (which is Java 7 64-bit): 这留下了javapath设置如下(这是Java 7 64位):

C:\ProgramData\Oracle\Java\javapath>dir

 Directory of C:\ProgramData\Oracle\Java\javapath

2014-10-28  21:14    <DIR>          .
2014-10-28  21:14    <DIR>          ..
2014-10-28  21:14    <SYMLINK>      java.exe [C:\Program Files\Java\jre7\bin\java.exe]
2014-10-28  21:14    <SYMLINK>      javaw.exe [C:\Program Files\Java\jre7\bin\javaw.exe]
2014-10-28  21:14    <SYMLINK>      javaws.exe [C:\Program Files\Java\jre7\bin\javaws.exe]

Running my VBA application now works without a hard-coded path to java.exe . 现在运行我的VBA应用程序没有到java.exe的硬编码路径。

My hypothesis that the PATH variable was being ignored was wrong. 我的假设是PATH变量被忽略是错误的。

My new hypothesis is that Java 8 (32-bit) was not working properly when called from Shell , since it was executing the .jar file properly within a cmd.exe window (with the same path). 我的新假设是,从Shell调用时,Java 8(32位)无法正常工作,因为它在cmd.exe窗口中正确执行.jar文件(具有相同的路径)。

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

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