Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Systems programmers are the high priests of a low cult. -- R. S. Barton


devel / comp.lang.java.programmer / Re: Load jar into classpath and access resources in it at runtime

SubjectAuthor
* Load jar into classpath and access resources in it at runtimemike
+- Re: Load jar into classpath and access resources in it at runtimeArne Vajhøj
`* Re: Load jar into classpath and access resources in it at runtimeDaniele Futtorovic
 `* Re: Load jar into classpath and access resources in it at runtimee.d.pro...@gmail.com
  `* Re: Load jar into classpath and access resources in it at runtimeDaniele Futtorovic
   `- Re: Load jar into classpath and access resources in it at runtimee.d.pro...@gmail.com

1
Load jar into classpath and access resources in it at runtime

<16f09c2d-e244-4ada-8e55-c1014fde20c1n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=123&group=comp.lang.java.programmer#123

  copy link   Newsgroups: comp.lang.java.programmer
X-Received: by 2002:a05:6000:1c05:b0:21b:90e8:9542 with SMTP id ba5-20020a0560001c0500b0021b90e89542mr9580730wrb.392.1655825917079;
Tue, 21 Jun 2022 08:38:37 -0700 (PDT)
X-Received: by 2002:a05:6871:8a2:b0:fe:899:e540 with SMTP id
r34-20020a05687108a200b000fe0899e540mr21241723oaq.70.1655825916471; Tue, 21
Jun 2022 08:38:36 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!209.85.128.88.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.java.programmer
Date: Tue, 21 Jun 2022 08:38:36 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=83.253.144.66; posting-account=1c_fOgoAAADuOXlL0A4-T9PUmVHtMSYd
NNTP-Posting-Host: 83.253.144.66
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <16f09c2d-e244-4ada-8e55-c1014fde20c1n@googlegroups.com>
Subject: Load jar into classpath and access resources in it at runtime
From: mikaelpetterson@hotmail.com (mike)
Injection-Date: Tue, 21 Jun 2022 15:38:37 +0000
Content-Type: text/plain; charset="UTF-8"
 by: mike - Tue, 21 Jun 2022 15:38 UTC

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

Re: Load jar into classpath and access resources in it at runtime

<62b2095e$0$699$14726298@news.sunsite.dk>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=124&group=comp.lang.java.programmer#124

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!dotsrc.org!filter.dotsrc.org!news.dotsrc.org!not-for-mail
Date: Tue, 21 Jun 2022 14:09:34 -0400
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.10.0
Subject: Re: Load jar into classpath and access resources in it at runtime
Content-Language: en-US
Newsgroups: comp.lang.java.programmer
References: <16f09c2d-e244-4ada-8e55-c1014fde20c1n@googlegroups.com>
From: arne@vajhoej.dk (Arne Vajhøj)
In-Reply-To: <16f09c2d-e244-4ada-8e55-c1014fde20c1n@googlegroups.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 19
Message-ID: <62b2095e$0$699$14726298@news.sunsite.dk>
Organization: SunSITE.dk - Supporting Open source
NNTP-Posting-Host: fc36491d.news.sunsite.dk
X-Trace: 1655834974 news.sunsite.dk 699 arne@vajhoej.dk/68.9.63.232:64032
X-Complaints-To: staff@sunsite.dk
 by: Arne Vajhøj - Tue, 21 Jun 2022 18:09 UTC

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

Re: Load jar into classpath and access resources in it at runtime

<t91fet$jjk$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=125&group=comp.lang.java.programmer#125

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: da.futt.news@laposte-dot-net.invalid (Daniele Futtorovic)
Newsgroups: comp.lang.java.programmer
Subject: Re: Load jar into classpath and access resources in it at runtime
Date: Thu, 23 Jun 2022 12:31:56 +0200
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <t91fet$jjk$1@dont-email.me>
References: <16f09c2d-e244-4ada-8e55-c1014fde20c1n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 23 Jun 2022 10:31:57 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2c122b4ba43e0e43055c896a113304ec";
logging-data="20084"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+h+TTbzGUCSz6+ol2KgDPL"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.10.0
Cancel-Lock: sha1:mK6KjH0j71TI2HI8EcfeS6JlRoI=
In-Reply-To: <16f09c2d-e244-4ada-8e55-c1014fde20c1n@googlegroups.com>
Content-Language: en-US
 by: Daniele Futtorovic - Thu, 23 Jun 2022 10:31 UTC

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.

Re: Load jar into classpath and access resources in it at runtime

<5b0738be-8988-4610-9453-2619dc70a2bcn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=126&group=comp.lang.java.programmer#126

  copy link   Newsgroups: comp.lang.java.programmer
X-Received: by 2002:a05:6000:2a4:b0:218:77ba:988e with SMTP id l4-20020a05600002a400b0021877ba988emr7937542wry.459.1655986828054;
Thu, 23 Jun 2022 05:20:28 -0700 (PDT)
X-Received: by 2002:a05:6808:1896:b0:32f:6cec:af9f with SMTP id
bi22-20020a056808189600b0032f6cecaf9fmr2024148oib.223.1655986827712; Thu, 23
Jun 2022 05:20:27 -0700 (PDT)
Path: i2pn2.org!i2pn.org!aioe.org!news.mixmin.net!proxad.net!feeder1-2.proxad.net!209.85.128.87.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.java.programmer
Date: Thu, 23 Jun 2022 05:20:27 -0700 (PDT)
In-Reply-To: <t91fet$jjk$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=98.237.40.232; posting-account=2czF5goAAAD4GBMPIGV4KcD2K4PhoB_H
NNTP-Posting-Host: 98.237.40.232
References: <16f09c2d-e244-4ada-8e55-c1014fde20c1n@googlegroups.com> <t91fet$jjk$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5b0738be-8988-4610-9453-2619dc70a2bcn@googlegroups.com>
Subject: Re: Load jar into classpath and access resources in it at runtime
From: e.d.programmer@gmail.com (e.d.pro...@gmail.com)
Injection-Date: Thu, 23 Jun 2022 12:20:28 +0000
Content-Type: text/plain; charset="UTF-8"
 by: e.d.pro...@gmail.com - Thu, 23 Jun 2022 12:20 UTC

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).

Re: Load jar into classpath and access resources in it at runtime

<t91q2f$tu4$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=127&group=comp.lang.java.programmer#127

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: da.futt.news@laposte-dot-net.invalid (Daniele Futtorovic)
Newsgroups: comp.lang.java.programmer
Subject: Re: Load jar into classpath and access resources in it at runtime
Date: Thu, 23 Jun 2022 15:33:02 +0200
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <t91q2f$tu4$1@dont-email.me>
References: <16f09c2d-e244-4ada-8e55-c1014fde20c1n@googlegroups.com>
<t91fet$jjk$1@dont-email.me>
<5b0738be-8988-4610-9453-2619dc70a2bcn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 23 Jun 2022 13:33:03 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2c122b4ba43e0e43055c896a113304ec";
logging-data="30660"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/krHXtSaIOLE4B2I3H9boj"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.10.0
Cancel-Lock: sha1:bYBympSYTKmbKTE8AA+kV6lG6c4=
In-Reply-To: <5b0738be-8988-4610-9453-2619dc70a2bcn@googlegroups.com>
Content-Language: en-US
 by: Daniele Futtorovic - Thu, 23 Jun 2022 13:33 UTC

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.

Re: Load jar into classpath and access resources in it at runtime

<f5131ba2-e8af-4a79-b58b-c886e68640b1n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=128&group=comp.lang.java.programmer#128

  copy link   Newsgroups: comp.lang.java.programmer
X-Received: by 2002:adf:f147:0:b0:21b:9ff4:9e08 with SMTP id y7-20020adff147000000b0021b9ff49e08mr9335988wro.608.1656018107809;
Thu, 23 Jun 2022 14:01:47 -0700 (PDT)
X-Received: by 2002:a9d:6d90:0:b0:60c:757d:1e08 with SMTP id
x16-20020a9d6d90000000b0060c757d1e08mr4508788otp.224.1656018107032; Thu, 23
Jun 2022 14:01:47 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.128.87.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.java.programmer
Date: Thu, 23 Jun 2022 14:01:46 -0700 (PDT)
In-Reply-To: <t91q2f$tu4$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=98.237.40.232; posting-account=2czF5goAAAD4GBMPIGV4KcD2K4PhoB_H
NNTP-Posting-Host: 98.237.40.232
References: <16f09c2d-e244-4ada-8e55-c1014fde20c1n@googlegroups.com>
<t91fet$jjk$1@dont-email.me> <5b0738be-8988-4610-9453-2619dc70a2bcn@googlegroups.com>
<t91q2f$tu4$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <f5131ba2-e8af-4a79-b58b-c886e68640b1n@googlegroups.com>
Subject: Re: Load jar into classpath and access resources in it at runtime
From: e.d.programmer@gmail.com (e.d.pro...@gmail.com)
Injection-Date: Thu, 23 Jun 2022 21:01:47 +0000
Content-Type: text/plain; charset="UTF-8"
 by: e.d.pro...@gmail.com - Thu, 23 Jun 2022 21:01 UTC

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

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor