【问题标题】:include php file in final Jar so that method can call php file在最终 Jar 中包含 php 文件,以便方法可以调用 php 文件
【发布时间】:2016-09-06 19:51:45
【问题描述】:

我正在使用来自 java 代码的 php 文件,例如

Class JavaPhpTest{
    public void method1(){
Process p = Runtime.getRuntime().exec("/usr/local/php/bin/php " + "test.php")
    }
}

我在pom.xml 中包含了test.php(文件位于/src/main/resources)文件作为这个java 项目的资源,并且在构建test.php 之后在Jar 中。

现在,当有人在他们的pom.xml 中使用JavaPhpTest-jar 作为依赖项并像调用method1() 一样

class javaCall{
public void testing(){
    new JavaPhpTest().method1();
}

问题是现在当我打电话给testing() 时,它给出了test.php 找不到的错误。

我看到JavaPhpTest-jar 在 javaCall 的类路径中,但没有 php。 谁能给建议我们如何解决这个依赖问题?

我应该也为 php 创建一个 pom 项目并包含它吗?如果是这样,有人可以为我提供工作链接来做到这一点。

【问题讨论】:

  • 请澄清您的问题,因为我不确定您想要什么:从 JAR 中读取 text.php 文件或将整个 PHP 安装归档到 JAR 文件中
  • 两种方式都可以。如果我们可以从 JAR 中读取 php 那将是理想的

标签: java php maven jar classpath


【解决方案1】:

如果您想执行test.php,您必须从 Jar 中读取文件内容并将其保存为常规文件,在临时文件夹中。在这里您可以找到示例:getResourceAsStream() doesn't see resource

然后使用临时文件夹中的文件路径调用Runtime.getRuntime().exec(..)

更新

我看到了你的解决方案,可能会简化:

InputStream phpStream = JavaPhpTest.class.getResourceAsStream("/test.php");
java.nio.file.Files.copy(phpStream, Paths.get("test.php"));

【讨论】:

  • 感谢它运行良好。以下是使用的代码:final String destDir = JavaPhpTest.class.getProtectionDomain().getCodeSource().getLocation().getFile(); JarFile file = new JarFile(destDir); InputStream ipStreamtoPHPFile = file.getInputStream(file.getEntry("test.php")); File newfile = new File(destDir + File.separator + "test.php"); FileOutputStream opStreamPHPfile = new FileOutputStream(newfile); while (ipStreamtoPHPFile.available() > 0) opStreamPHPfile.write(ipStreamtoPHPFile.read());
猜你喜欢
  • 2011-07-20
  • 2010-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多