简体   繁体   中英

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. The code is part of distribution that supposedly works in Office 2007 (which I don't have).

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.

However, I suspected it couldn't find the java command. I verified that my PATH is correct by opening manually a CMD.exe window and running the ShellCommand (which I printed using Debug.Print ). 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.

So, still not trusting Shell from VBA, I hardcoded the ShellCommand as follows:

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.

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 ?

TL;DR: Remove Java 8

The inventory of my Java installs shows some complexity (!)

  • Java 7 Update 72
  • Java 7 Update 72 (64-bit)
  • Java SE Development Kit 7 Update 72 (64-bit)
  • 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:

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.

That leaves the javapath setting as follows (which is Java 7 64-bit):

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 .

My hypothesis that the PATH variable was being ignored was wrong.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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