【问题标题】:Executing PhantomJS From Inside Jar从 Jar 内部执行 PhantomJS
【发布时间】:2015-06-17 05:47:42
【问题描述】:

我正在使用PhantomJS 对网站进行无头测试。由于exe 将捆绑在jar 文件中,因此我决定读取它并将其写入临时文件,以便我可以通过绝对路径正常访问它。

以下是将 InputStream 转换为 String 的代码,引用新的临时文件:

public String getFilePath(InputStream inputStream, String fileName)
        throws IOException
{
    String fileContents = readFileToString(inputStream);
    File file = createTemporaryFile(fileName);
    String filePath = file.getAbsolutePath();
    writeStringToFile(fileContents, filePath);

    return file.getAbsolutePath();
}

private void writeStringToFile(String text, String filePath)
        throws FileNotFoundException
{
    PrintWriter fileWriter = new PrintWriter(filePath);

    fileWriter.print(text);
    fileWriter.close();
}

private File createTemporaryFile(String fileName)
{
    String tempoaryFileDirectory = System.getProperty("java.io.tmpdir");
    File temporaryFile = new File(tempoaryFileDirectory + File.separator
            + fileName);

    return temporaryFile;
}

private String readFileToString(InputStream inputStream)
        throws UnsupportedEncodingException, IOException
{
    StringBuilder inputStringBuilder = new StringBuilder();
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(inputStream, "UTF-8"));

    String line;
    while ((line = bufferedReader.readLine()) != null)
    {
        inputStringBuilder.append(line);
        inputStringBuilder.append(System.lineSeparator());
    }

    String fileContents = inputStringBuilder.toString();

    return fileContents;
}

这可行,但是当我尝试启动 PhantomJS 时,它会给我一个 ExecuteException

SERVERE: org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "C:\Users\%USERPROFILE%\AppData\Local\Temp\phantomjs.exe" (in directory "."): CreateProcess error=216, the version of %1 is not compatible with this Windows version. Check the system information of your computer and talk to the distributor of this software)

如果我不尝试从jar 中读取PhantomJS,因此使用相对路径可以正常工作。问题是我如何从 jar 文件中读取和执行PhantomJS,或者至少获得读取和写入新(临时)文件的解决方法。

【问题讨论】:

  • jar文件中为什么会出现PhantomJS exe?
  • @ArtjomB。让用户无需关心 webdriver 可执行文件
  • 您正在将 phantomjs.exe 读入字符串?

标签: java phantomjs windows-8.1


【解决方案1】:

您无法执行 JAR 条目,因为 JAR 是一个 zip 文件,而操作系统不支持从 zip 文件中运行可执行文件。他们原则上可以,但归结为“从 zip 中复制 exe 然后运行它”。

exe 已损坏,因为您将其存储在字符串中。字符串不是二进制数据,它们是 UTF-16,这就是为什么您不能直接从 InputStream 读取到字符串的原因——需要进行编码转换。您的代码将 exe 读取为 UTF-8,将其转换为 UTF-16,然后使用默认字符集将其写回。即使您机器上的默认字符集恰好是 UTF-8,这也会导致数据损坏,因为 exe 不是有效的 UTF-8。

试穿它的大小。 Java 7 引入了 NIO.2,它(除其他外)为常见的文件操作提供了很多方便的方法。包括将 InputStream 放入文件中!我也在使用临时文件 API,如果您的应用程序的多个实例同时运行,它将防止冲突。

public String getFilePath(InputStream inputStream, String prefix, String suffix)
        throws IOException
{
    java.nio.file.Path p = java.nio.file.Files.createTempFile(prefix, suffix);
    p.toFile().deleteOnExit();
    java.nio.file.Files.copy(inputStream, p, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
    return p.toAbsolutePath().toString();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 2013-12-10
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 2020-04-17
    • 2015-03-30
    • 1970-01-01
    相关资源
    最近更新 更多