我正在尝试从 PHP 脚本中运行一个小型 Java 类。它在 Linux 上按预期工作,但是当在 Windows 上执行相同的脚本时,我收到:
Could not find or load main class JDBCProxy
这是相关的 PHP:
$classpath = join(PATH_SEPARATOR, array(
dirname(__FILE__).DIRECTORY_SEPARATOR.'JDBCProxy',
dirname(__FILE__).DIRECTORY_SEPARATOR.'JDBCProxy'.DIRECTORY_SEPARATOR.'libs'.DIRECTORY_SEPARATOR.'json-simple-1.1.1.jar',
));
$cmd = sprintf("java -cp '%s' JDBCProxy", $classpath);
...
$process = proc_open($cmd, $descriptorspec, $pipes);
sprintf的输出是
java -cp 'C:\worker\lib\DB\JDBCProxy;C:\worker\lib\DB\JDBCProxy\libs\json-simple-1.1.1.jar' JDBCProxy
当直接从命令行运行时,这将按预期工作。 为什么 Java 在 PHP 上下文中运行时无法找到该类?
最佳答案
对于 Windows,请使用双引号 ("
) 而不是单引号 ('
)。
参见Including all the jars in a directory within the Java classpath对于 Java 细节和 this answer用于 Windows 对单引号的处理。
关于通过 PHP 执行时 Java 无法找到类,相同的命令在 CLI 中有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47084432/