dbrumann
dbrumann

Reputation: 17166

Executing java from CLI via exec() under Windows

I have a PHP-script originally developed on Ubuntu, which now has to run on a Windows machine, executing a java program like this:

exec("java -jar {$filename}");
// Process output

This does not work as expected on Windows. I already found out, that although I can use java -version from the command prompt I can't use it in exec(), i.e. the problem is java can not be found.

I have a workaround in place, pointing to java.exe using the complete path to C:\Program Files\Java\...\java.exe if the script runs on Windows. Unfortunately though this is hardcoded to the path on the current machine, which might change or vary on a different system, e.g. when installing Java to a different location or a different version (JRE/JDK/6/7) is installed.

How do I call Java on Windows without having to refer to the exact location of java.exe?

Upvotes: 1

Views: 810

Answers (2)

kantholy
kantholy

Reputation: 994

Even if this Question is a little older, I ran into the same problem and I found a pretty neat solution for it without the PATH requirement.

There is a symlinks to all java executabled located in this folder:

C:\ProgramData\Oracle\Java\javapath

for example: just call

C:\ProgramData\Oracle\Java\javapath\java.exe -jar XYZ.jar

Upvotes: 0

Aurimas Ličkus
Aurimas Ličkus

Reputation: 10074

You need to set enviroment variable on windows, to be able access java without path

http://www.java.com/en/download/help/path.xml

Upvotes: 2

Related Questions