playwright-java icon indicating copy to clipboard operation
playwright-java copied to clipboard

[Question] how to resolve java spring boot app throws "Failed to create driver"

Open tony-playwright opened this issue 3 years ago • 2 comments

Hi there,

I am using playwright java implementation. I understand this is class loader issue. the driver.jar and driver-bundle.jar dependencies are in maven pom.xml and I can see they are in the generated /lib dir of the war file. If I deploy to standard tomcat server, I "Failed to create driver" error. I have to explicitly add these 2 jars in the system class path even they are in the lib dir of the war. the issue looks like they are unable to create playwright-java-3892430927773655149/playwright.sh and related node package on temp folder. It looks to me class loader found the jars other wise I should get unable to find driver error. My question is how to make that playwright working on spring boot with embedded tomcat?

Another question is from the code private static Driver createDriver() throws Exception { String pathFromProperty = System.getProperty("playwright.cli.dir"); if (pathFromProperty != null) { return new PreinstalledDriver(Paths.get(pathFromProperty)); }

Class<?> jarDriver = Class.forName("com.microsoft.playwright.impl.DriverJar");
return (Driver) jarDriver.getDeclaredConstructor().newInstance();

}

How to define "playwright.cli.dir"? From the doc, it is node application and there is no separate cli installation.

Thanks

tony-playwright avatar Feb 18 '22 14:02 tony-playwright

Can you attach the stack trace and perhaps a repro that we could run locally? We have a test that checks playwright with spring boot loader: https://github.com/microsoft/playwright-java/tree/main/tools/test-spring-boot-starter

. the issue looks like they are unable to create playwright-java-3892430927773655149/playwright.sh and related node package on temp folder.

Does the app have proper access to a temp dir?

How to define "playwright.cli.dir"? From the doc, it is node application and there is no separate cli installation.

This is for separate driver installation, we don't officially support this option, you shouldn't need it.

yury-s avatar Feb 18 '22 17:02 yury-s

Hi Yury,

I created a sprint boot repo for you to review. It exposed as Restful API call. the spring boot loader you mentioned is standalone application and I have no issue to run it as standalone. I am guessing on J2ee server, playwright has issue to fork process to run the node js package because of web class loader.

here is repo that you can reproduce. please refer to readme file with my 3 test cases. I even tested on wildfly 11 without luck.

https://github.com/tony-playwright/spring-boot-playwright-pdf

To your question 1)"Does the app have proper access to a temp dir?". yes. I am running on my local mac machine and I have admin access to temp dir.

  1. stack trace exception java.lang.RuntimeException: Failed to create driver at com.microsoft.playwright.impl.Driver.ensureDriverInstalled(Driver.java:54) at com.microsoft.playwright.impl.PlaywrightImpl.create(PlaywrightImpl.java:40) at com.microsoft.playwright.Playwright.create(Playwright.java:96) at com.microsoft.playwright.Playwright.create(Playwright.java:100) at spring.boot.playwright.pdf.controller.ApplicationController.convertHtmlToPdf(ApplicationController.java:48) at spring.boot.playwright.pdf.controller.ApplicationController.generatePdfByPlaywright(ApplicationController.java:40) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) .................. at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.NullPointerException at com.microsoft.playwright.impl.DriverJar.extractDriverToTempDir(DriverJar.java:85) at com.microsoft.playwright.impl.DriverJar.initialize(DriverJar.java:44) at com.microsoft.playwright.impl.Driver.ensureDriverInstalled(Driver.java:52) ... 55 more 2022-02-22 15:25:20.511 ERROR 2677 --- [nio-1234-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.xml.ws.WebServiceException: 500 INTERNAL_SERVER_ERROR] with root cause java.lang.NullPointerException: null at com.microsoft.playwright.impl.DriverJar.extractDriverToTempDir(DriverJar.java:85) ~[driver-bundle-1.18.0.jar!/:1.18.0] at com.microsoft.playwright.impl.DriverJar.initialize(DriverJar.java:44) ~[driver-bundle- 1.18.0.jar!/:1.18.0] at com.microsoft.playwright.impl.Driver.ensureDriverInstalled(Driver.java:52) ~[driver-1.18.0.jar!/:1.18.0] at com.microsoft.playwright.impl.PlaywrightImpl.create(PlaywrightImpl.java:40) ~[playwright-1.18.0.jar!/:1.18.0] at com.microsoft.playwright.Playwright.create(Playwright.java:96) ~[playwright-1.18.0.jar!/:1.18.0] at com.microsoft.playwright.Playwright.create(Playwright.java:100) ~[playwright-1.18.0.jar!/:1.18.0] at spring.boot.playwright.pdf.controller.ApplicationController.convertHtmlToPdf(ApplicationController.java:48) ~. [classes!/:0.0.1-SNAPSHOT] at spring.boot.playwright.pdf.controller.ApplicationController.generatePdfByPlaywright(ApplicationController.java:40) ~[classes!/:0.0.1-SNAPSHOT]

As you can see, these 3 jars are in the classpath.

tony-playwright avatar Feb 22 '22 21:02 tony-playwright

Closing this issue as part of our triage process - looks like this issue felt under our radar, sorry!

If you still encounter issues, please re-file in this repository.

mxschmitt avatar Feb 01 '23 14:02 mxschmitt

Hi @tony-playwright were you able to resolve the issue?

yuanyal avatar Jun 17 '23 21:06 yuanyal