onejar - Using resource in kotlin func - does not work with fat jar (one jar) -


i have following piece of code:

fun main(args: array<string>) {     val urlforcsr: url = classloader.getsystemclassloader().getresource("merchant.id")     // tried classloader.getsystemresource("merchant.id")     ... 

the following when run intellij works fine , finds resource. when run using bundled jar gives nullpointerexception.

  • path resource: /src/main/resources/merchant.id
  • path code: /src/main/java/route.kt

following maven config snippet:

    ...     <!-- make jar executable -->         <plugin>             <groupid>org.apache.maven.plugins</groupid>             <artifactid>maven-jar-plugin</artifactid>             <configuration>                 <archive>                     <manifest>                         <mainclass>routekt</mainclass> <!-- class generated (for above main func - named route.kt) -->                     </manifest>                 </archive>             </configuration>         </plugin>          <!-- includes runtime dependencies -->         <plugin>             <groupid>org.dstovall</groupid>             <artifactid>onejar-maven-plugin</artifactid>             <version>1.4.4</version>             <executions>                 <execution>                     <goals>                         <goal>one-jar</goal>                     </goals>                 </execution>             </executions>         </plugin>     </plugins> 

is there other war url above resource work one-jar or other way of making fat jar.

jar content:

jar tf target/route-1.0-snapshot.jar meta-inf/ meta-inf/manifest.mf merchant.id routekt$main$1.class routekt.class meta-inf/maven/ meta-inf/maven/groupid/ meta-inf/maven/groupid/tokengenerator/ meta-inf/maven/groupid/tokengenerator/pom.xml meta-inf/maven/groupid/tokengenerator/pom.properties 

one-jar content:

meta-inf/manifest.mf main/route-1.0-snapshot.jar lib/kotlin-stdlib-0.1-snapshot.jar lib/kotlin-runtime-0.1-snapshot.jar lib/spark-core-2.3.jar lib/slf4j-api-1.7.12.jar lib/slf4j-simple-1.7.12.jar lib/jetty-server-9.3.2.v20150730.jar ... 

onejar packages of code jar file contains other jar files in lib/ directory or reference them in other dirs outside jar (depending on onejar system using). uses custom class loader make work. therefore defeats use of system class loader.

one way defeat this use class same jar resource want load, , therefore class loader setup correctly jar or nested jar or magic location in narnia of resource:

val stream = myclass::class.java.getresourceasstream("/merchant.id") 

where myclass class same jar merchant.id, , path resource must absolute leading /

be sure stream , not resource url might not usable since isn't understandable url system. produce url jar within jar rest of java wouldn't understand, example file:/path/to/jarfile/bot.jar!/config/file.txt (or worse!). work, i'm not sure when jar within jar file in it.

a secondary option:

val stream = thread.currentthread().contextclassloader.getresourceasstream("/merchant.id") 

check resource names:

in question read resource merchant.id show jar contents having merchant.id.soft.applepaydev-v1.csr ... make sure these names match if trying load exact resource.


resource loading in onejar must customized, ugly. in link 1 resource talks resource loading onejar:

there number of ways java supports finding , opening resources:

  • url findresource(string resource): returns resource, or null if not found.
  • enumeration findresources(string resource): returns enumeration of resource url's match given string
  • url.openconnection(): opens connection resource url.

one-jar supports of these mechanisms within context of one-jar file, may contain jar files have duplicate resources.

this problem referenced these other stack overflow posts, therefore pretty duplicate question:

googling turns many more resources.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -