Я использую PhantomJS, чтобы сделать безгласное тестирование веб-сайта. Поскольку exe
будет включен в файл jar
, я решил прочитать его и записать его во временный файл, чтобы я мог нормально обращаться к нему через абсолютный путь.Выполнение PhantomJS изнутри 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)
Если я не пытаюсь читать PhantomJS
из jar
, следовательно, используя относительный путь, он отлично работает. Вопрос в том, как я могу читать и выполнять PhantomJS
из файла jar или, по крайней мере, получить обходной путь при чтении и записи нового (временного) файла для работы.
Почему в проекте фантомного exe в файле jar в первую очередь? –
@ArtjomB. Чтобы пользователи не заботились о исполняемых файлах webdriver – BullyWiiPlaza
Вы читаете файл phantomjs.exe в строку? –