简体   繁体   English

从通过VBA启动的.bat文件运行时找不到java.exe

[英]Cannot find java.exe when run from a .bat file launched via VBA

I have a script.bat which looks like this: 我有一个script.bat

c:\windows\system32\java.exe -version
pause

If I run the script from a command window, it works fine, and outputs the Java version information: 如果我从命令窗口运行脚本,它将正常工作,并输出Java版本信息:

java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)

However, I also have a VBA macro in Excel that calls the same script: 但是,我在Excel中也有一个VBA宏,它调用相同的脚本:

Shell "C:\users\Matt\script.bat", vbNormalFocus

The .bat file executes, but it has a problem finding Java: .bat文件已执行,但是在查找Java时遇到了问题:

C:\Users\Matt\Documents>c:\windows\system32\java.exe -version
'c:\windows\system32\java.exe' is not recognized as an internal or external command,
operable program or batch file.

Any ideas on what's going wrong? 有什么问题的想法吗? Oddly, this works fine on a number of other machines, just fails on this one laptop (64-bit Windows 7, using Excel 2010). 奇怪的是,这在其他许多计算机上也能正常工作,而在这台笔记本电脑上(使用Excel 2010的64位Windows 7)则失败。

It seems to be the same problem as described here: 似乎是与此处描述的问题相同的问题:

http://betterlogic.com/roger/2011/04/cannotunable-to-run-java-exe-from-cwindowssystem32/ http://betterlogic.com/roger/2011/04/cannotunable-to-run-java-exe-from-cwindowssystem32/

Namely, Windows was trying to do some 64-bit compatibility magic to redirect C:\\Windows\\System32\\java.exe to C:\\Windows\\SysWOW64\\java.exe , but Java didn't install a suitable exe in that location. 也就是说,Windows试图做一些64位兼容性魔术来将C:\\Windows\\System32\\java.exe重定向到C:\\Windows\\SysWOW64\\java.exe ,但是Java没有在该位置安装合适的exe。

I solved this by copying the exe's of my java1.7 64 bit installation from C:\\Windows\\system32 and pasting them into C:\\Windows\\SysWOW64. 我通过从C:\\ Windows \\ system32复制我的Java1.7 64位安装程序的exe并将其粘贴到C:\\ Windows \\ SysWOW64来解决此问题。 I don't know if this will work for other versions of java and it shouldn't need to be done with the 32 bit java installation. 我不知道这是否适用于其他版本的Java,并且不需要使用32位Java安装来完成。 Using this method the path environment variables do not need to be updated or maintained. 使用这种方法,路径环境变量不需要更新或维护。

I think it is because you must have installed java by unzipping the binary. 我认为这是因为您必须通过解压缩二进制文件来安装Java。

The path is set correctly because it runs on command line but not when you give full path. 路径设置正确,因为它在命令行上运行,但在您提供完整路径时却没有。 Run the below command to get the java.exe directory. 运行以下命令以获取java.exe目录。

c:\> for %i in (java.exe) do @echo.   %~$PATH:i

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

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