micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.06k stars 1.06k forks source link

ResourceLoader is broken with JAR #8296

Closed ThraaxSession closed 1 year ago

ThraaxSession commented 1 year ago

Expected Behavior

When the JAR is directly used, the ResourceLoader can load any resource from Resources

Actual Behaviour

I get a call stack, when I want to get a resource from JAR. When I run from Idea, it works fine.

In Kotlin constructor: private val resourceLoader: ResourceLoader Actual call: val logsScript = resourceLoader.getResource("commands/getLogs.sh")

image

com.jcraft.jsch.SftpException: java.io.FileNotFoundException: /mnt/d/Projekte/Uberspace-Projekte/SpaceUp-Server/file:/mnt/d/Projekte/Uberspace-Projekte/SpaceUp-Server/build/libs/SpaceUp-0.26.0-SNAPSHOT-all.jar!/commands/getLogs.sh (No such file or directory)
        at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:485)
        at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:367)
        at technology.iatlas.spaceup.services.SshService.upload$suspendImpl(SshService.kt:240)
        at technology.iatlas.spaceup.services.SshService.upload(SshService.kt)

Steps To Reproduce

Environment Information

GraalVM 22 / Java 11 Micronaut 3.7.3

Example Application

Here is the service which crashes

Version

3.7.3

yawkat commented 1 year ago

This is an issue in your code, you are incorrectly converting the URL returned by getResource into a file here: https://github.com/SpaceUp-Cloud/SpaceUp-Server/blob/0ce749634ec7c4cbc129a7e829dbdd305073b0a9/src/main/kotlin/technology/iatlas/spaceup/services/SshService.kt#L236-L241

When the application is packaged as a jar, the resources are entries in that jar file, they are not individual files on the file system. They have a proper URL (the one returned by getResource), but those URLs do not have an associated java.io.File, because they're inside the jar. You can probably call URL.openStream and pass the stream directly, though.

ThraaxSession commented 1 year ago

@yawkat oh yes, face palm to me. I changed that 2 weeks ago. My bad.