What do you not like about this?
# announcements
s
What do you not like about this?
g
for one getClass is ambiguous as heck, no idea what to import.... second, it isn't exactly intuitive, maybe you can clarify what's happening here? third... I could have swarn I was programming in a language recently that let me do something like resource("path from resources root") and I thought it was kotlin... forth, if they are going to strongly recommend organizing a project like this, thought they would make it easy! I don't know, lol
Why do I need to get file after getting resource... does getResource get tacos too? lol
s
well.
I presume the resource does not need to be provided as a file
right?
I remember a syntax like this:
g
Copy code
clientDocker.buildImageCmd().pathToDockerfile()
path
image.png
s
Copy code
val file = MyClass::class.java.getResource("database.properties").getFile()
you like that better?
g
is MyClass just this?
s
It’s the class name
but you could also do this:
Copy code
this.javaClass.getResource("database.properties").getFile()
I’ve right now no time to answer your question about the internals but if you will find a tone of good explanations via google
g
I just need the path... I could do ..\..\resources\blah blah... but if I move stuff around later that would suck.
Copy code
val clientDocker = DockerClass().dockerClient
    val result = clientDocker.infoCmd().exec()
    val file = DockerClass::class.java.getResource("database.properties").file
This works, now I just need the path of it.
s
try . absolutePath
g
nope, getResource is returning null, what should the path be? "dockerfiles/svrDB" isn't workin
s
Is your problem related to retrieving the resource (Which seems not to work) or you are wondering about the syntax being used?
g
it isn't working... I assumed because my pathing syntax was wrong
s
so the techniques used are based on reflections
g
DockerClass::class.java.getResource("svrDB") must not be null at com.newsworthy.build.containers.DockerClass.main(Docker.kt:31) at com.newsworthy.build.containers.DockerKt.main(Docker.kt:40) at com.newsworthy.build.containers.DockerKt.main(Docker.kt) Process finished with exit code 1
s
should read about classloaders and their purpose
similiar in Java too
or C#
g
ok\
s
maybe somebody else is available to help you out right now
will come back to you alter
later
g
no worries, break time anyway
a
If you're using this in a jar, getting a resource as a file is unsupported, see: https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar
If not, may be an issue between using the resources in main vs. test
g
I'm so confused dudes... So what do I do here, I need a path to the svrDB file posted a pic of above... what type of path am I supposed to use in this modern framework?
Copy code
fun main(){
    val clientDocker = DockerClass().dockerClient
    val result = clientDocker.infoCmd().exec()
    DockerClass().main()
    //clientDocker.buildImageCmd().pathToDockerfile("?????")

}
a
is this code in BuildApp.kt?
g
its in Docker.kt, the file I wanna reference is svrDB
Full code in that file
Copy code
package com.newsworthy.build.containers

import com.github.dockerjava.core.DefaultDockerClientConfig
import com.github.dockerjava.core.DockerClientConfig
import com.github.dockerjava.core.DockerClientImpl
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient
import com.github.dockerjava.transport.DockerHttpClient
import java.io.File

const val projectName: String = "srcBuild"

class DockerClass {
    private val config: DockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder()
        .withDockerHost("<tcp://localhost:2375>")
        //.withDockerTlsVerify(true)
        //.withDockerCertPath("/home/user/.docker")
        //.withRegistryUsername("admin")
        //.withRegistryPassword("password")
        //.withRegistryEmail("<mailto:gunslingor@gmail.com|gunslingor@gmail.com>")
        //.withRegistryUrl("<https://index.docker.io/v1/>")
        .build()

    val httpClient: DockerHttpClient = ApacheDockerHttpClient.Builder()
        .dockerHost(config.dockerHost)
        .sslConfig(config.sslConfig)
        .build()

    val dockerClient = DockerClientImpl.getInstance(config, httpClient)!!

    fun main(){
        //val filePath = DockerClass::class.java.getResource("svrDB").file
        //val filePath = (this::class).java.getResource("dockerfiles\\svrDb")
        //print(filePath)


    }
}

fun main(){
    val clientDocker = DockerClass().dockerClient
    val result = clientDocker.infoCmd().exec()
    DockerClass().main()
    //clientDocker.buildImageCmd().pathToDockerfile()

}
a
can you move the
svrDb
file to main/resources instead of test/resources?
g
yes, that is probablyt in error, lol... but if I need to test a resource what happens, I have to put it both?
maybe that was my problem, lol
a
yeah, by default, test classes/resources are not accessible in main
you could probably play around with
sourceSets
in gradle to make it work, but wouldn’t recommend necessarily
hard to say without knowing the context, but yeah, could put it in both resources folder or refactor how the file is passed into the DockerClass
g
I love sources sets, I do see the resources setting and I have used it before... maybe that's the way I recall doing it so easy before... even so, gradle just sets it and it's in the default local so it is already set correctly... I checked the gradle docs and nothing actually tells me how to access em right though.
Copy code
DockerClass::class.java.getResource("dockerfile/svrDB")
still comes back null
a
i see the folder is called
dockerfiles
in your screenshot
g
fixed, no change... also concerned, even if I get this working it's going to change if I compile to a jar?
a
yeah, in a jar, you can only get access to an
InputStream
to it using
getResourceAsStream
if you need it in a literal file, you’d need to write that InputStream to a temporary file and use that
g
I mean, this function takes a path not a file... so your telling me the path is going to change depending on whether I use a jar or not?
a
DockerClass::class.java.classLoader.getResource("dockerfiles/svrDB")
g
thanks, that did seem to give the path.. in my output folder, interesting
a
a jar is basically a zip so there doesn’t exist a straightforward path to your resource
g
right... not really sure what java does to run it
So I really need a method that will work like this and in a fat jar... this is great for programming, but prod uses fat people jars
thanks though... much appreciated
👍 1
a
yeah, to make it work in jars, i recommend trying
getResourceAsStream
, write that to a File (maybe using
File.createTempFile
), then you can pass that path to your function and it’ll work in both dev IDE/prod jar scenarios
g
weird. Is that really what folks do, isn't copying a file a waste of resources? Guess I'll try it, no worries.
a
don’t claim to be an expert with the manipulation of resources like this, but it’s what i’ve found so far in my research
g
hmmm..... check this out, I'm using ktor and klaxon in my srcWeb project and I noticed this... so there I just have to add this like and ktor seems to be serving up the files via HTTP without issue, whether Jar/War/Normal-whatever... class files?
Copy code
static("/static") {
    files("build/processedResources/jvm/main")
}
wonder what it's really doing... oh well, I get stuck trying to understand things... time to move on, lol
a
hard to say what it’s doing under the hood without looking under the hood lol
g
no worries, thanks again.
👍 1