[testng-users] How to load TestNG config within an executable JAR?

2,434 views
Skip to first unread message

TERACytE

unread,
Dec 13, 2011, 1:32:25 PM12/13/11
to testng...@googlegroups.com

I have an shaded executable jar created by a Maven build. Within I have a
main class that executes and attempts to run a collection of TestNG tests.
It runs until it it attempts to load the xml config files for the tests.
The steps within the main class are:

suites.add("testng-A.xml");
suites.add("testng-B.xml");

testng.setTestSuites(suites);
testng.addListener(tla);
testng.run();

The problem is that I cannot get TestNG to successfully find the xml files.
I receive a "java.io.FileNotFoundException: testng-a.xml (No such file or
directory)" error. The location of the files in the JAR are:

/com/address/MyMainClass
/testng-a.xml
/testng-b.xml

I am running the jar with the following command:
java -jar uber.jar

Any ideas on how I can get this to work?

--
View this message in context: http://old.nabble.com/How-to-load-TestNG-config-within-an-executable-JAR--tp32969731p32969731.html
Sent from the testng-users mailing list archive at Nabble.com.

Cédric Beust ♔

unread,
Dec 13, 2011, 2:32:22 PM12/13/11
to testng...@googlegroups.com
Hi,

TestNG expects these to be files, so I'm not surprised this is not working. We would need to change this part of the API so it can accept input streams instead, but I don't see an easy way to retrofit this in the XML classes at the moment.

As a workaround, you could extract these files from the jar file in a temporary location and pass the paths to these files to your XmlSuite objects...

-- 
Cédric




--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.


TERACytE

unread,
Dec 13, 2011, 3:32:00 PM12/13/11
to testng...@googlegroups.com

Thank-you for the prompt reply. I was hopeful there was a way around this.
I tested my code with a hardcoded path outside of the JAR and it worked. I
now need to find a way to extract and reference them at runtime.

--
View this message in context: http://old.nabble.com/How-to-load-TestNG-config-within-an-executable-JAR--tp32969731p32970485.html

Marv

unread,
Dec 21, 2011, 10:19:41 PM12/21/11
to testng-users
I was doing the exact same thing. But instead of extracting the file
from the jar, I used getResourceAsStream and then write the stream out
to file in the working folder and clean up the file after tests run.

//Retrieve the suite file from the jar and then write it out
to filesystem for use with testNG
InputStream is = pScanner.class.getResourceAsStream("/
testng.xml");
File mysuite = new File("testng.xml");
try {
OutputStream os = new FileOutputStream(mysuite);

byte buf[]=new byte[1024];
int len;
try {
while ((len=is.read(buf))>0) {
os.write(buf,0,len);
os.close();
is.close();
System.out.println(mysuite.toPath().toString());
}
} catch (IOException e) {
e.printStackTrace(); //To change body of catch
statement use File | Settings | File Templates.
}
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch
statement use File | Settings | File Templates.
}

List<String> xmlFileList = new ArrayList();
xmlFileList.add("testng.xml");
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { pScanner.class });
testng.setTestSuites(xmlFileList);
testng.addListener(tla);
testng.run();

On Dec 13, 1:32 pm, TERACytE <terac...@gmail.com> wrote:
> Thank-you for the prompt reply.  I was hopeful there was a way around this.
> I tested my code with a hardcoded path outside of theJARand it worked.  I
> now need to find a way to extract and reference them at runtime.
>
>
>
>
>
>
>
>
>
> Cedric Beust wrote:
>
> > Hi,
>
> > TestNG expects these to be files, so I'm not surprised this is not
> > working.
> > We would need to change this part of the API so it can accept input
> > streams
> > instead, but I don't see an easy way to retrofit this in the XML classes
> > at
> > the moment.
>
> > As a workaround, you could extract these files from thejarfile in a
> > temporary location and pass the paths to these files to your XmlSuite
> > objects...
>
> > --
> > Cédric
>
> > On Tue, Dec 13, 2011 at 10:32 AM, TERACytE <terac...@gmail.com> wrote:
>
> >> I have an shaded executablejarcreated by a Maven build.  WithinI have
> >> a
> >> main class that executes and attempts to run a collection of TestNG
> >> tests.
> >> It runs until it it attempts to load the xml config files for the tests.
> >> The stepswithinthe main class are:
>
> >>        suites.add("testng-A.xml");
> >>        suites.add("testng-B.xml");
>
> >>        testng.setTestSuites(suites);
> >>        testng.addListener(tla);
> >>        testng.run();
>
> >> The problem is that I cannot get TestNG to successfully find the xml
> >> files.
> >> I receive a "java.io.FileNotFoundException: testng-a.xml (No such file or
> >> directory)" error.  The location of the files in theJARare:
>
> >>        /com/address/MyMainClass
> >>        /testng-a.xml
> >>        /testng-b.xml
>
> >> I am running thejarwith the following command:
> >>        java -jaruber.jar
>
> >> Any ideas on how I can get this to work?
>
> >> --
> >> View this message in context:
> >>http://old.nabble.com/How-to-load-TestNG-config-within-an-executable-...
> >> Sent from the testng-users mailing list archive at Nabble.com.
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "testng-users" group.
> >> To post to this group, send email to testng...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> testng-users...@googlegroups.com.
> >> For more options, visit this group at
> >>http://groups.google.com/group/testng-users?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "testng-users" group.
> > To post to this group, send email to testng...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > testng-users...@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/testng-users?hl=en.
>
> --
> View this message in context:http://old.nabble.com/How-to-load-TestNG-config-within-an-executable-...

Ashok Kumar

unread,
Oct 18, 2018, 5:21:58 AM10/18/18
to testng-users
Hi,
May I know whether this is resolved and support is available in the latest version of Test NG. 

Or still I need to extract the XML outside and add it to the suites list. 

If the support is available, can some I get some code snippet, which I can refer to .

Regards,
Ashok

⇜Krishnan Mahadevan⇝

unread,
Oct 18, 2018, 5:28:55 AM10/18/18
to testng...@googlegroups.com
What happens when you try it using the latest released version of TestNG ? 

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.
--

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

shailender g

unread,
Oct 18, 2018, 6:04:41 AM10/18/18
to testng...@googlegroups.com
I have tested jar exeution recently and it worked.
Thanks! 
Shailender

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.

Ashok Kumar

unread,
Oct 24, 2018, 5:24:41 AM10/24/18
to testng-users
Hi,
I have tested it with below code snippet, and it worked. 

testngObj.setTestJar("Test.jar");
testngObj.setXmlPathInJar(this.testNgXmlFilePath);
testngObj.run();

This works only if the XML is a suite XML with Tests in it. 

I am actually trying to run a XML which has <suite-files> content. 

Example: 
Test_Suites.xml
<suite name="Suites Test" parallel="none">
<suite-files>
<suite-file path="Test1.xml"></suite-file>
<suite-file path="Test2.xml"></suite-file>
<suite-file path="Test3.xml"></suite-file>
</suite-files>
</suite>

I have a separate maven project (Test) having all the XMLs inside the resources folder which i build it as a JAR

Folder structure inside resources
resources 
            Test XMLs
                     Test_Suites.xml
                      Test1.xml
                      Test2.xml
                      Test3.xml

Any help towards this problem will be much appreciated. 

Regards,
Ashok

⇜Krishnan Mahadevan⇝

unread,
Oct 24, 2018, 5:32:01 AM10/24/18
to testng...@googlegroups.com
Ashok,

>>>>  I am actually trying to run a XML which has <suite-files> content

What version of TestNG are you using ? Please try using the latest released version viz ., 7.0.0-beta1. If it doesn't work please write up a bug and include a sample ( full fledged simple one ) that can be used to easily reproduce the problem.  

Ashok Kumar

unread,
Oct 24, 2018, 6:35:05 AM10/24/18
to testng-users
Hi Krishnan,
I am using TesntNG Version 6.13.1.

BTW, did you try having the XMLs inside the JAR and tried to access it (as i have said in my previous post) directly, or you extracted the XMLs from the JAR locally and used local path reference. 

If it worked for you (accessing directly from the JAR), could you please share the code-snippet. 

The problem is I am getting an exception. I believe, Test NG picks up the XML in the JAR (main XML with sutite-files tag) set as part of the "testngObj.setXmlPathInJar(this.testNgXmlFilePath);" and it is not able to find the files given in the main XML file. 

[Wed Oct 24 16:01:12 GMT+05:30 2018] SEVERE: Exception : 
 
org.testng.TestNGException: java.io.IOException: Stream closed
at com.eci.raft.tests.infra.testngext.CustomSuiteXmlParser.parse(CustomSuiteXmlParser.java:20)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:9)
at org.testng.xml.Parser.parse(Parser.java:152)
at org.testng.xml.Parser.parse(Parser.java:237)
at org.testng.JarFileUtils.testngXmlExistsInJar(JarFileUtils.java:58)
at org.testng.JarFileUtils.extractSuitesFrom(JarFileUtils.java:39)
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:360)
at org.testng.TestNG.initializeEverything(TestNG.java:974)
at org.testng.TestNG.run(TestNG.java:988)
at com.eci.raft.tests.smoke.TestNgExecutorNew.start(TestNgExecutorNew.java:25)
at com.eci.raft.tests.smoke.TestExecution.main(TestExecution.java:34)
Caused by: java.io.IOException: Stream closed
at java.util.zip.InflaterInputStream.ensureOpen(InflaterInputStream.java:67)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:121)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(XMLEntityManager.java:2899)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:674)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:189)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:812)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:327)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:195)
at org.testng.xml.XMLParser.parse(XMLParser.java:38)
at com.eci.raft.tests.infra.testngext.CustomSuiteXmlParser.parse(CustomSuiteXmlParser.java:16)
... 10 more


Code Snippet:
public void start(String[] testInput) {
parseArgs(testInput);
TestNG testngObj = new TestNG();
testngObj.setOutputDirectory(this.testOutputDirPath);
testngObj.setTestJar(getJarPath("ApolloEmbTests-0.0.1-EnvTest-SNAPSHOT.jar")); ---> XMLs are available inside the JAR
testngObj.setXmlPathInJar(this.testNgXmlFilePath);
System.setProperty("inputFile", "input.xlsx");
try {
testngObj.run();
} catch (Exception e) {
Logger.logExceptionTrace(e);
}
}

public static String getJarPath(String jarName) {
String classPath = System.getProperty("java.class.path");
Logger.debugInfo("Separator:"+File.pathSeparator);
List<String> cpJarsList = Arrays.asList(classPath.split(File.pathSeparator));
Logger.debugInfo("Classpath jars list : " + cpJarsList);
for(String s : cpJarsList) {
if(s.contains(jarName)) {
return s;
}
}
return null;
}

Regards,
Ashok

⇜Krishnan Mahadevan⇝

unread,
Oct 24, 2018, 6:39:51 AM10/24/18
to testng...@googlegroups.com
Ashok,

No I haven't tried. 

I remember some fixes went in around this functionality. That is why I am asking that you please try using TestNG 7.0.0-beta1. If it still doesn't work please write up a bug and we will try and get to it. 

Ashok Kumar

unread,
Oct 25, 2018, 3:36:51 AM10/25/18
to testng-users
Hi Krishnan,
This worked on the new version "7.0.0-beta1"

Thanks.

Regards,
Ashok

shailender g

unread,
Oct 26, 2018, 6:09:36 AM10/26/18
to testng...@googlegroups.com
When I added jar feature to my project , I had to make sure all resources in my maven project are accessed as a resource stream. For example using getResourceAsStream instead InputStream or other File streams. Other than that I didn't have to do any significant changes. Hope this helps!

amod mahajan

unread,
Aug 16, 2020, 11:58:42 AM8/16/20
to testng-users
Hi,

I am into the same problem. I have my testng.xml inside src/test/resources/xmlFiles/testng.xml and I have a runner class where I am calling this testng,xml. 
Locally it is running absolutely fine. 

But when I import it as an executable jar file which is generated inside target folder. Every path it is taking within /target. 
I have tried all getResourceAsStream() and whatever solution is discussed. 

TestNG Version - 7.3.0
I am using maven-assembly-plugin to export as a JAR. 
Java - 1.8

Thanks
Amod Mahajan

Krishnan Mahadevan

unread,
Aug 16, 2020, 12:34:13 PM8/16/20
to testng...@googlegroups.com

Can you please add some context behind your project, what your code looks like, what your runner looks like, what version of TestNG are you on, etc.,

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.com/

ocean chow

unread,
Aug 20, 2020, 2:23:56 AM8/20/20
to testng-users
Hi amod,
you can see my issue "throw ”Cannot find class in classpath:*****.testclass” when execute test by jar mode", hope it can help you.
Reply all
Reply to author
Forward
0 new messages