我一直在努力将我的代码移植到一个可运行的JAR文件中,但我似乎无法让PhantomJS工作。我的代码如下所示:
procedure TForm1.Chromium1PreKeyEvent(Sender: TObject;
const browser: ICefBrowser; const event: PCefKeyEvent; osEvent: PMsg;
out isKeyboardShortcut, Result: Boolean);
begin
if event.focus_on_editable_field=0 then
begin
if event.windows_key_code=vk_back then
begin
result:=true;
end;
end
else
begin
result:=false;
end;
end;
基本上,它将phantomjs.exe(从我的资源包中)复制到外部目录并启动它。这在我的IDE中工作正常,但出于某种原因,当我尝试使用private void initDriver()
{
// Prevent Log Messages from showing up in console, login
try
{
File f = new File ("./phantomjs.exe");
if (!f.exists())
{
wait(2.0);
InputStream in = getClass().getClassLoader().getResourceAsStream("/js/scrapertool/resources/phantomjs.exe");
OutputStream out = new FileOutputStream("./phantomjs.exe");
IOUtils.copy(in, out);
}
System.setProperty("phantomjs.binary.path","./phantomjs.exe");
}
catch (Exception e)
{
try
{
e.printStackTrace(new PrintStream("output.txt"));
System.exit(0);
}
catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
}
DesiredCapabilities dcap = new DesiredCapabilities();
String[] phantomArgs = new String[] { "--webdriver-loglevel=NONE" };
dcap.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomArgs);
//Initialize driver, try 3 times before crashing
int i=0;
while(i<3)
{
try
{
driver=new PhantomJSDriver(dcap);
break;
}
catch(Exception ex)
{
if(i>2)
{
try
{
ex.printStackTrace(new PrintStream("output.txt"));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
//after debugging I know it gets here eventually
System.exit(0);
}
i++;
}
}
}
时出现此错误:
initDriver()
调试后我知道它至少找到了phantomjs.exe服务器文件,那我该怎么办?
更新:
今天尝试了这个并且有效。也许我只需要重置我的电脑。