简体   繁体   English

如何在Jar文件中运行类?

[英]How to run classes inside Jar file?

I created a jar file that launches an application to allow the user to select which test they want to run. 我创建了一个jar文件,该文件将启动一个应用程序,以允许用户选择他们要运行的测试。 Each test has its own class and main method. 每个测试都有自己的类和主要方法。 The UI application passes parameters to the main method for the class that represents the test. UI应用程序将参数传递给表示测试的类的main方法。

I was able to create the executable JAR file for the UI application but nothing happens when I select the test I want to run. 我能够为UI应用程序创建可执行JAR文件,但是当我选择要运行的测试时没有任何反应。 I think it is that JAR file can only handle one main method. 我认为JAR文件只能处理一种主要方法。

Below is part of the code for the application that allows the user to select which test to run. 以下是应用程序代码的一部分,该代码允许用户选择要运行的测试。

public class UI extends JFrame {

    String[] args={Environment, Browser, TestingCoverage, DBLog, "UI"};

    if (chckbxEQ_CA_Home.isSelected())
    {   
        EQ_CA_Home.main(args);      
    }

    if (chckbxEQ_CA_Condo.isSelected())
    {               
        EQ_CA_Condo.main(args);
    }

    if (chckbxEQ_CA_Renter.isSelected())
    {   
        EQ_CA_Renter.main(args);
    }
}       

You can do that using Junit library. 您可以使用Junit库来实现。

JUnitCore junit = new JUnitCore();
Result res = junit.run(yourTestClass);

Don't invoke the main() method to avoid any possible interruptions, just let the JUnitcore find the appropriate test methods. 不要调用main()方法来避免任何可能的中断,只需让JUnitcore查找适当的测试方法即可。

And also don't forget to include Junit.jar in classpath (or) include it as part of your jar file (as a fat jar). 并且也不要忘记在类路径中包含Junit.jar (或将其作为jar文件的一部分(作为胖jar))。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM