Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #39783 > unrolled thread

Load jar into classpath and access resources in it at runtime

Started bymike <mikaelpetterson@hotmail.com>
First post2022-06-21 08:38 -0700
Last post2022-06-23 14:01 -0700
Articles 6 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Load jar into classpath and access resources in it at runtime mike <mikaelpetterson@hotmail.com> - 2022-06-21 08:38 -0700
    Re: Load jar into classpath and access resources in it at runtime Arne Vajhøj <arne@vajhoej.dk> - 2022-06-21 14:09 -0400
    Re: Load jar into classpath and access resources in it at runtime Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2022-06-23 12:31 +0200
      Re: Load jar into classpath and access resources in it at runtime "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2022-06-23 05:20 -0700
        Re: Load jar into classpath and access resources in it at runtime Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2022-06-23 15:33 +0200
          Re: Load jar into classpath and access resources in it at runtime "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2022-06-23 14:01 -0700

#39783 — Load jar into classpath and access resources in it at runtime

Frommike <mikaelpetterson@hotmail.com>
Date2022-06-21 08:38 -0700
SubjectLoad jar into classpath and access resources in it at runtime
Message-ID<16f09c2d-e244-4ada-8e55-c1014fde20c1n@googlegroups.com>
Hi,

I have created a jar file, test-example.jar, and added under src/test/resources
and now I want to make sure that when I run the testcase
this jar file is loaded so I can access files in it. I am using java 11.

Q1: How to load jar, in testcase, at runtime?
Q2: How to access resource in jar. Resource resides in jar with the following path suites/tools/device/nc/testng2.xml

br,

//mike

[toc] | [next] | [standalone]


#39784

FromArne Vajhøj <arne@vajhoej.dk>
Date2022-06-21 14:09 -0400
Message-ID<62b2095e$0$699$14726298@news.sunsite.dk>
In reply to#39783
On 6/21/2022 11:38 AM, mike wrote:
> I have created a jar file, test-example.jar, and added under src/test/resources
> and now I want to make sure that when I run the testcase
> this jar file is loaded so I can access files in it. I am using java 11.
> 
> Q1: How to load jar, in testcase, at runtime?

Standard technique is to instantiate a URLClassLoader for the
jar file and use that to load files from the jar file.

> Q2: How to access resource in jar. Resource resides in jar with the following path suites/tools/device/nc/testng2.xml

Maybe:

theclassloader.getResourceAsStream("suites/tools/device/nc/testng2.xml"));

Arne

[toc] | [prev] | [next] | [standalone]


#39785

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2022-06-23 12:31 +0200
Message-ID<t91fet$jjk$1@dont-email.me>
In reply to#39783
On 21/06/2022 17:38, mike wrote:
> Hi,
> 
> I have created a jar file, test-example.jar, and added under src/test/resources
> and now I want to make sure that when I run the testcase
> this jar file is loaded so I can access files in it. I am using java 11.
> 
> Q1: How to load jar, in testcase, at runtime?
> Q2: How to access resource in jar. Resource resides in jar with the following path suites/tools/device/nc/testng2.xml
> 
> br,
> 
> //mike

Normally, what you put under /src is what you want packaged (in a JAR, 
WAR, etc.).
If you put a JAR there, and your sources are packaged as a JAR, you'll 
end up with JAR inside your JAR. Which I would say is definitely 
technically workable. I'm not sure whether it's a recommended practice 
or not.

To load such a JAR file, you'd have to load it as a resource
 
Thread.getThread().getContextClassLoader().getResource(NAME_OF_THE_JAR_FILE)

, then create a new ClassLoader for that resource -- which will be 
binary, so not a URLCLassLoader... Then load the resource/class you need 
from that classloader.

Perhaps instead of the above, one could simply create a URLClassLoader 
with a "classpath:/NAME_OF_THE_JAR_FILE" URL. I mention it in case, but 
IIRC the "classpath:/" resource prefix is something that Spring code 
brings, it's not out of the box. Not sure.

Alternatives to all of the above include:
- at or before build time, extracting the files you need from the JAR 
and putting them in your /src tree flat;
- putting the JAR file alongside your source tree (in a lib directory) 
and making sure it's added to the classpath when the runtime is 
launched, in which case you could access classes and resources from it 
as though they were in your /src tree.

-- 
  DF.

[toc] | [prev] | [next] | [standalone]


#39786

From"e.d.pro...@gmail.com" <e.d.programmer@gmail.com>
Date2022-06-23 05:20 -0700
Message-ID<5b0738be-8988-4610-9453-2619dc70a2bcn@googlegroups.com>
In reply to#39785
On Thursday, June 23, 2022 at 6:32:11 AM UTC-4, Daniele Futtorovic wrote:
> On 21/06/2022 17:38, mike wrote: 
> > Hi, 
> > 
> > I have created a jar file, test-example.jar, and added under src/test/resources 
> > and now I want to make sure that when I run the testcase 
> > this jar file is loaded so I can access files in it. I am using java 11. 
> > 
> > Q1: How to load jar, in testcase, at runtime? 
> > Q2: How to access resource in jar. Resource resides in jar with the following path suites/tools/device/nc/testng2.xml 
> > 
> > br, 
> > 
> > //mike
> Normally, what you put under /src is what you want packaged (in a JAR, 
> WAR, etc.). 
> If you put a JAR there, and your sources are packaged as a JAR, you'll 
> end up with JAR inside your JAR. Which I would say is definitely 
> technically workable. I'm not sure whether it's a recommended practice 
> or not. 
> 
> To load such a JAR file, you'd have to load it as a resource 
> 
> Thread.getThread().getContextClassLoader().getResource(NAME_OF_THE_JAR_FILE) 
> 
op didn't say if they're trying to package a jar inside another jar or just build a war.
That Thread command is definitely the way to read a resource, at least in a maven war project from the src/main/resources folder.
If you're building a war, dependent jars should end up in WEB-INF/lib. 
If the jar is provided on the classpath, the context class loader should work. Normally files loaded as resources are not packaged inside a file (jar) wrapped in another file (jar/war).

[toc] | [prev] | [next] | [standalone]


#39787

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2022-06-23 15:33 +0200
Message-ID<t91q2f$tu4$1@dont-email.me>
In reply to#39786
On 23/06/2022 14:20, e.d.pro...@gmail.com wrote:

> op didn't say if they're trying to package a jar inside another jar or just build a war.

On 21/06/2022 17:38, mike wrote:
 > Hi,
 >
 > I have created a jar file, test-example.jar, and added under 
src/test/resources

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#39788

From"e.d.pro...@gmail.com" <e.d.programmer@gmail.com>
Date2022-06-23 14:01 -0700
Message-ID<f5131ba2-e8af-4a79-b58b-c886e68640b1n@googlegroups.com>
In reply to#39787
On Thursday, June 23, 2022 at 9:33:17 AM UTC-4, Daniele Futtorovic wrote:
> > op didn't say if they're trying to package a jar inside another jar or just build a war.
> On 21/06/2022 17:38, mike wrote: 
> > Hi, 
> > 
> > I have created a jar file, test-example.jar, and added under 
> src/test/resources
> -- 
> DF.
sounds like test-example.jar is a jar file in the src/test/resources path, not what they're trying to build

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web