Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

java - Need correct step for Bat file creation using (TestNG.xml + Maven)

I am wondering about to create correct Bat file ,

I am using Maven + Selenium(TestNG)

I have tried many different ways to Resolve exception, But its one of the most common exception: Could not find or load main class / java.lang.ClassNotFoundException

In my Maven project there is no bin or lib folder, And I am wondering for it. Whether to call library from .m2(Maven) installation.

To call class file, In my maven project .class file located something like : E:estBatchDemoargetclassesmytest

Here is sample of What I have tried so far,

set projectLocation=E:estBatchDemo
cd %projectLocation%   
set classpath=E:estBatchDemoargetclassesmytest; 
java -cp %classpath% C:UsersDesktop-pc.m2
epositoryorgestngestng6.14.3estng-6.14.3.jar; C:UsersDesktop-pc.m2
epositoryorgseleniumhqseleniumselenium-java3.12.0selenium-java-3.12.0.jar; org.testng.TestNG %projectLocation%estng.xml
pause

Anyone can please assist, Which library need to call or How can I achieve correct .class path.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As per your question description I think there are certain grey areas which we need to address beofre jumping into the solution.

  • I am wondering about to create correct batch (i.e. .bat) file: Fundamentally, Maven is a build tool which extensively works in-conjunction with pom.xml and doesn't requires any Windows batch file.
  • Moving forward if your usecase is to pass the Windows batch file to Jenkins for Continous Integration then you need to verify this step if the Windows batch file is able to kick-start the execution of your Test Suite.
  • However Jenkins can also kick-start the execution of your Test Suite through the pom.xml and Maven.

Now, your usecase must match to one of the above mentioned requirement as mentioned below:

  • Windows Batch file + TestNG
  • Jenkins + TestNG
  • Jenkins + Maven

As you have extensively spoken about the Windows batch file so here are the relevant details as per the first option Windows Batch file + TestNG:

  • Write a simple code block with @Test annotation of TestNG as follows:

    package demoJenkins;
    
    import org.testng.annotations.Test;
    
    public class DemoJenkinsJob 
    {
    
        @Test
        public void testJenkins()
        {
    
            System.out.println("Welcome to Jenkins World");
        }
    }
    
  • Run As TestNG Test

  • On Successful execution convert using TestNG -> Convert to TestNG and testng.xml gets created.
  • The testng.xml will look like:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite">
      <test name="Test">
        <classes>
          <class name="demoJenkins.DemoJenkinsJob"/>
        </classes>
      </test> <!-- Test -->
    </suite> <!-- Suite -->
    
  • Get the absolute Project Location from your IDE (i.e. Eclipse), browse to the sub-directory, create a directory lib

  • Copy all the relevant jars and libraries (Selenium and TestNG jars) in the lib directory.

    • selenium-server-standalone-3.13.0.jar
    • org.testng_6.14.2.r201802161450.jar
    • com.beust.jcommander_1.72.0.jar
    • org.apache-extras.beanshell.bsh_2.0.0.b6.jar
    • org.yaml.snakeyaml_1.17.0.jar
  • Through CLI browse to the Project Directory and provide the following classpath:

    >set classpath=<Project Directory>in;<Project Directory>lib*;
    
  • Through CLI execute testng.xml as follows:

    Project_Directory>java org.testng.TestNG testng.xml
    
  • On successful execution, within the Project Directory create a new text document and add the following code:

    java -cp bin;lib/* org.testng.TestNG testng.xml
    
  • Save the file as run.bat

  • Execute the Windows batch file run.bat and verify it executes the Test Suite as expected.

Update

As per your question and subsequent comment,

  • When you use TestNG and/or Maven, these framework/buildtool keeps all the relevant jars in their respective location. But when you intend the configure the buildpath manually you have to create the lib directory manually and keep a copy of the relevant jars.

  • Perhaps you don't need to create the bin directory manually as on successful compilation of your Java program your program will automatically create the bin and keeps the class file before execution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...