Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

throw ”Cannot find class in classpath:*****.testclass” when execute test by jar mode

196 views
Skip to first unread message

ocean chow

unread,
Aug 9, 2020, 1:27:21 PM8/9/20
to testng-users
Hi guys, 
I suffered this issue for a long time, I create maven project to write test cases, use below pom.xml and assembly.xml to build package, as you can see, the project isn't a executable jar. 
<plugin>
         
<artifactId>maven-assembly-plugin</artifactId>
           
<version>2.4</version>
           
<configuration>
               
<descriptor>src/test/resources/assembly.xml</descriptor>
           
</configuration>
           
<executions>
               
<execution>
                   
<id>make-assembly</id>
                   
<phase>package</phase>
                   
<goals>
                       
<goal>single</goal>
                   
</goals>
               
</execution>
           
</executions>
       
</plugin>

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
   
<id>fat-tests</id>
   
<formats>
       
<format>jar</format>
   
</formats>
   
<includeBaseDirectory>false</includeBaseDirectory>
   
<dependencySets>
       
<dependencySet>
           
<outputDirectory>/</outputDirectory>
           
<useProjectArtifact>true</useProjectArtifact>
           
<unpack>true</unpack>
       
</dependencySet>
   
</dependencySets>
   
<fileSets>
       
<fileSet>
           
<directory>${project.build.directory}/test-classes</directory>
           
<outputDirectory>/</outputDirectory>
           
<useDefaultExcludes>true</useDefaultExcludes>
       
</fileSet>
   
</fileSets>
</assembly>

then I use codes to debug 
TestNG testNG = new TestNG();
   
testNG.setTestJar("c:\testjar.jar");
   
testNG.setXmlPathInJar("suitefile\xx.xml");
   
testNG.run();

at last I will see error in the below, while I can locate the .class file by the path in the xx.xml,the suite file is so simple which just has one testsuite and one test and one test class. anyone can help me with that? thanks.
Cannot find class in classpath: tests.OceanTest.Test05Test
at org.testng.xml.XmlClass.loadClass(XmlClass.java:81)
at org.testng.xml.XmlClass.init(XmlClass.java:73)
at org.testng.xml.XmlClass.<init>(XmlClass.java:59)
at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:544)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(XMLDTDValidator.java:745)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1359)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2784)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
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:39)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:17)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:10)
at org.testng.xml.Parser.parse(Parser.java:168)
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:379)
at org.testng.TestNG.run(TestNG.java:1030)
at testproject.main(App.java:26)



⇜Krishnan Mahadevan⇝

unread,
Aug 9, 2020, 1:33:22 PM8/9/20
to testng-users
Can you please create a simple standalone project and share that so that it can be used to reproduce the problem ?
Make sure that you are using the latest released version of TestNG (7.3.0 as of today)

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 Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/62a1fb23-8d33-4a6b-b486-68b94520fbc3o%40googlegroups.com.
Message has been deleted

ocean chow

unread,
Aug 10, 2020, 6:52:38 AM8/10/20
to testng-users
Thanks Krishnan,
right now I use 7.3.0 to do the test, and not use a test project to call test in jar , just use "java -cp automation_test-1.0.0-fat-tests.jar -Dhub="http://vms.com:4444/wd/hub" -Dmode="" -DuseGrid="true" -DbrowserType="IE" -DbrowserVersion=11 -DappRunType=0 -Denv="Live" org.testng.TestNG  -testjar automation_test-1.0.0-fat-tests.jar     -xmlpathinjar suiteXmlFile/xx.xml" to do the test, so the error in the previous post has gone, but it will throw a nullpointerexception on the common method to read configuration file( config/customized xml)

1. Below is the structure of the jar file(automation_test-1.0.0-fat-tests.jar)
   config/customized xml
   suiteXmlFile/xx.xml
   com/aa/bb/common/....--->store class file for my common function for the test
   com/aa/bb/test/....---->store class files for my test cases
   .....                       ----->other class files for dependency lib from POM.xml

2. the common method for reading configuration is in the class com.aa.bb.myConfiguration.java, which will be built in com/aa/bb/common/myConfiguration.class 
InputStream is = myConfiguration.class.getResourceAsStream("/config/customized xml")

is this information enough?could you please help me with that, Thanks.

BR,

OC

在 2020年8月10日星期一 UTC+8上午1:33:22,Krishnan Mahadevan写道:
To unsubscribe from this group and stop receiving emails from it, send an email to testng...@googlegroups.com.

⇜Krishnan Mahadevan⇝

unread,
Aug 11, 2020, 2:39:20 AM8/11/20
to testng-users
The NPE that you are seeing perhaps indicates that maybe the xml is not available in your jar. Did you double check that ?

The command that you are using to run your tests seems a bit weird.

java -cp automation_test-1.0.0-fat-tests.jar \
-Dhub="http://vms.com:4444/wd/hub" -Dmode="" \
-DuseGrid="true" -DbrowserType="IE" -DbrowserVersion=11 \
-DappRunType=0 -Denv="Live" org.testng.TestNG  \
-testjar automation_test-1.0.0-fat-tests.jar \
-xmlpathinjar suiteXmlFile/xx.xml
You should be able to skip giving the "testjar" and just do something like this no ?

java -cp automation_test-1.0.0-fat-tests.jar \
-Dhub="http://vms.com:4444/wd/hub
" -Dmode="" \
-DuseGrid="true" -DbrowserType="IE" -DbrowserVersion=11 \
-DappRunType=0 -Denv="Live" org.testng.TestNG  \
-xmlpathinjar suiteXmlFile/xx.xml

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 Scribblings @ https://rationaleemotions.com/

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/1e6bd48e-ed7c-43b9-a618-d7e9caebb276o%40googlegroups.com.
Message has been deleted

ocean chow

unread,
Aug 11, 2020, 3:43:33 AM8/11/20
to testng-users
Thanks Krishnan for your quick response.
 
Yes, I'm sure the xml exist in the jar file, like I described in last post about the structure of Jar file
  config/customized.xml  ----------------------------------------->>>>>I use InputStream is = myConfiguration.class.getResourceAsStream("/config/customized.xml") to get file but throw NPE:(
   suiteXmlFile/xx.xml
   com/aa/bb/common/....--->store class file for my common function for the test
   com/aa/bb/test/....---->store class files for my test cases
   .....                       ----->other class files for dependency lib from POM.xml

and I followed some steps in here --->https://niharikawhatsnext.wordpress.com/2015/03/11/running-maven-testng-tests-as-a-jar, you can ignore these "-D" para, which are used for my test case for my web project. if you means it's weird on the "-testjar automation_test-1.0.0-fat-tests.jar"? after I removed this parameter, I saw an error " you need to specify at least one testng.xml, one class or one method"

ocean chow

unread,
Aug 11, 2020, 11:00:42 AM8/11/20
to testng-users
found the reason now, cause the File.separator!!!
in my code, I use the /config"+File.separator+ AnyFileName to locate file, actually, when run by Right clicking @test method and choose runas testng***, that's no problem, but when execute jar, seems can't recognize by JVM, it will translate it to "/config\***.xml",so weird.

At last, still thanks @Krishnan. 
Reply all
Reply to author
Forward
0 new messages