microsoft / playwright-java

Java version of the Playwright testing and automation library
https://playwright.dev/java/
Apache License 2.0
1.15k stars 207 forks source link

[Question] NoSuchFileException in jar file #263

Closed arkasandr closed 3 years ago

arkasandr commented 3 years ago

I didn't find any decision yet for this problem. I cannot execute jar file with my spring-boot service using playwright.

java.lang.IllegalStateException: Failed to execute CommandLineRunner
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:782)
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:763)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:318)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
        at ru.cnd.Application.main(Application.java:14)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
Caused by: java.lang.RuntimeException: Failed to create driver
        at com.microsoft.playwright.impl.Driver.ensureDriverInstalled(Driver.java:46)
        at com.microsoft.playwright.impl.PlaywrightImpl.create(PlaywrightImpl.java:40)
        at com.microsoft.playwright.Playwright.create(Playwright.java:50)
        at ru.cnd.config.BrowserInitialize.launchBrowser(BrowserInitialize.java:12)
        at ru.cnd.config.ServiceInitialize.lambda$configAuthentication$0(ServiceInitialize.java:35)
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779)
        ... 13 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
        at com.microsoft.playwright.impl.Driver.createDriver(Driver.java:65)
        at com.microsoft.playwright.impl.Driver.ensureDriverInstalled(Driver.java:44)
        ... 18 common frames omitted
Caused by: java.nio.file.NoSuchFileException: /BOOT-INF/lib/driver-bundle-0.180.0.jar!/driver/linux
        at jdk.zipfs/jdk.nio.zipfs.ZipPath.getAttributes(ZipPath.java:742)

I found that getResource() causes this exception, but I'm not sure that it is my case. This method in DriverJar.class has such code

private void extractDriverToTempDir() throws URISyntaxException, IOException { ClassLoader classloader = Thread.currentThread().getContextClassLoader(); URI uri = classloader.getResource("driver/" + platformDir()).toURI();

Please, how can I run my jar file correctly or find the reason of such exception?

yury-s commented 3 years ago

The error suggests that com.microsoft.playwright.impl.DriverJar which is provided by driver-bundle module is not on the class path. If you manually configure which jar files are passed to java vm you need to make sure to include all playwright dependencies from pom.xml.

arkasandr commented 3 years ago

" If you manually configure which jar files are passed to java vm you need to make sure to include all playwright dependencies from pom.xml." no manually config. i use spring-boot-maven-plugin to for install

arkasandr commented 3 years ago

i think it fails when calling (Driver)jarDriver.getDeclaredConstructor().newInstance(); and try to get DriverJar instance

yury-s commented 3 years ago

i think it fails when calling (Driver)jarDriver.getDeclaredConstructor().newInstance(); and try to get DriverJar instance

Correct, and it fails because the DriverJar is not in the classpath (most likely you didn't inlcude driver-bundle.jar into the class path).

arkasandr commented 3 years ago

@yury-s thanks! i add manually to project /lib and use ENTRYPOINT exec java -cp target/myApp-1.0.jar -Dloader.path=lib/ org.springframework.boot.loader.PropertiesLauncher to execute jar file. Now I try to build dockerfile and have another problem Caused by: com.microsoft.playwright.PlaywrightException: Error: Host system is missing dependencies! with list of missing dependencies.... what can be wrong now?

yury-s commented 3 years ago

Now I try to build dockerfile and have another problem Caused by: com.microsoft.playwright.PlaywrightException: Error: Host system is missing dependencies! with list of missing dependencies.... what can be wrong now?

The message should contain detailed explanation. It's hard to tell without seeing the exact error but my hunch is that this has something to do with browser dependencies. Please use our docker file or the one that published on DockerHub to ensure the deps are preinstalled.

arkasandr commented 3 years ago

@yury-s thanks for the help. I only add some code to start my spring-boot application at the end of this Dockerfile. May be I mixed directories and my app not find these libraries. This is my Dockerfile

`FROM ubuntu:focal WORKDIR /usr/app

Install WebKit dependencies

RUN apt-get update && apt-get install -y --no-install-recommends \ libwoff1 \ libopus0 \ libwebp6 \ libwebpdemux2 \ libenchant1c2a \ libgudev-1.0-0 \ libsecret-1-0 \ libhyphen0 \ libgdk-pixbuf2.0-0 \ libegl1 \ libnotify4 \ libxslt1.1 \ libevent-2.1-7 \ libgles2 \ libxcomposite1 \ libatk1.0-0 \ libatk-bridge2.0-0 \ libepoxy0 \ libgtk-3-0 \ libharfbuzz-icu0

Install gstreamer and plugins to support video playback in WebKit.

RUN apt-get update && apt-get install -y --no-install-recommends \ libgstreamer-gl1.0-0 \ libgstreamer-plugins-bad1.0-0 \ gstreamer1.0-plugins-good \ gstreamer1.0-libav

Install Chromium dependencies

RUN apt-get update && apt-get install -y --no-install-recommends \ libnss3 \ libxss1 \ libasound2 \ fonts-noto-color-emoji \ libxtst6

Install Firefox dependencies

RUN apt-get update && apt-get install -y --no-install-recommends \ libdbus-glib-1-2 \ libxt6

Install ffmpeg to bring in audio and video codecs necessary for playing videos in Firefox.

RUN apt-get update && apt-get install -y --no-install-recommends \ ffmpeg

(Optional) Install XVFB if there's a need to run browsers in headful mode

RUN apt-get update && apt-get install -y --no-install-recommends \ xvfb

=== INSTALL JDK and Maven ===

RUN apt-get update && apt-get install -y --no-install-recommends \ openjdk-8-jdk maven

Install utilities required for downloading driver

RUN apt-get update && apt-get install -y --no-install-recommends \ curl unzip

RUN mkdir /tmp/pw-java COPY . /tmp/pw-java RUN cd /tmp/pw-java && chmod 755 ./scripts/download_driver_for_all_platforms.sh && \ mvn install -D skipTests --no-transfer-progress && \ rm -rf /tmp/pw-java

Install my app

FROM openjdk:11 as backend RUN apt-get update && apt-get install maven -y WORKDIR /backend COPY / . ADD /lib /lib RUN mvn install

ENTRYPOINT exec java -cp target/mosPropBot-1.0.jar -Duser.timezone=UTC -Dloader.path=lib/ org.springframework.boot.loader.PropertiesLauncher

`

arkasandr commented 3 years ago

And now I have this stack trace

`mos-prop-bot | 2021-02-09 15:31:47.641 ERROR 1 --- [ main] o.s.boot.SpringApplication : Application run failed

mos-prop-bot | mos-prop-bot | java.lang.IllegalStateException: Failed to execute CommandLineRunner mos-prop-bot | at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:782) mos-prop-bot | at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:763) mos-prop-bot | at org.springframework.boot.SpringApplication.run(SpringApplication.java:318) mos-prop-bot | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) mos-prop-bot | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) mos-prop-bot | at ru.cnd.Application.main(Application.java:10) mos-prop-bot | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) mos-prop-bot | at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) mos-prop-bot | at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) mos-prop-bot | at java.base/java.lang.reflect.Method.invoke(Method.java:566) mos-prop-bot | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47) mos-prop-bot | at org.springframework.boot.loader.Launcher.launch(Launcher.java:86) mos-prop-bot | at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) mos-prop-bot | at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:577) mos-prop-bot | Caused by: com.microsoft.playwright.PlaywrightException: Error: Host system is missing dependencies! mos-prop-bot | mos-prop-bot | Missing libraries are: mos-prop-bot | libgobject-2.0.so.0 mos-prop-bot | libglib-2.0.so.0 mos-prop-bot | libatk-1.0.so.0 mos-prop-bot | libatk-bridge-2.0.so.0 mos-prop-bot | libgio-2.0.so.0 mos-prop-bot | libdrm.so.2 mos-prop-bot | libxkbcommon.so.0 mos-prop-bot | libXcomposite.so.1 mos-prop-bot | libXdamage.so.1 mos-prop-bot | libXfixes.so.3 mos-prop-bot | libXrandr.so.2 mos-prop-bot | libgbm.so.1 mos-prop-bot | libgtk-3.so.0 mos-prop-bot | libgdk-3.so.0 mos-prop-bot | libpango-1.0.so.0 mos-prop-bot | libcairo.so.2 mos-prop-bot | libgdk_pixbuf-2.0.so.0 mos-prop-bot | libatspi.so.0 mos-prop-bot | libxshmfence.so.1 mos-prop-bot | mos-prop-bot | Note: use DEBUG=pw:api environment variable and rerun to capture Playwright logs. mos-prop-bot | at com.microsoft.playwright.impl.WaitableResult.get(WaitableResult.java:50) mos-prop-bot | at com.microsoft.playwright.impl.ChannelOwner.runUntil(ChannelOwner.java:88) mos-prop-bot | at com.microsoft.playwright.impl.Connection.sendMessage(Connection.java:74) mos-prop-bot | at com.microsoft.playwright.impl.ChannelOwner.sendMessage(ChannelOwner.java:79) mos-prop-bot | at com.microsoft.playwright.impl.BrowserTypeImpl.launchImpl(BrowserTypeImpl.java:44) mos-prop-bot | at com.microsoft.playwright.impl.BrowserTypeImpl.lambda$launch$0(BrowserTypeImpl.java:36) mos-prop-bot | at com.microsoft.playwright.impl.LoggingSupport.withLogging(LoggingSupport.java:51) mos-prop-bot | at com.microsoft.playwright.impl.BrowserTypeImpl.launch(BrowserTypeImpl.java:36) mos-prop-bot | at com.microsoft.playwright.impl.BrowserTypeImpl.launch(BrowserTypeImpl.java:29) mos-prop-bot | at ru.cnd.config.BrowserInitialize.launchBrowser(BrowserInitialize.java:15) mos-prop-bot | at ru.cnd.config.ServiceInitialize.lambda$configAuthentication$0(ServiceInitialize.java:35) mos-prop-bot | at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779) mos-prop-bot | ... 13 common frames omitted `