0

我是从 Windows 命令行运行 Java 的新手,无法运行我的程序。总而言之,这正是我想要做的:

运行两个 .java 文件,TestCase1.java 和 UniversalVariables.java
合并多个外部 jar 文件
从命令行执行所有这些操作

TestCase1.java 包含所有功能代码,而 UniversalVariables.java 几乎只有一些数据,所以不要让其中的所有内容让您感到困惑。

应该注意的是,我正在使用 Selenium 并通过 Eclipse 运行它。

这是TestCase1.java:

package NSSR;

import com.thoughtworks.selenium.*;<br>
import org.junit.After;<br>
import org.junit.Before;<br>
import org.junit.Test;<br>

//Create new NSSR

public class TestCase1 extends SeleneseTestCase {

    public static void main(String[] args) {
        TestCase1 tc1 = new TestCase1();
        try {
            tc1.setUp();
            tc1.testTestCase1();
            tc1.tearDown();
        } catch (Exception e) {

        }
    }

    @Before
    public void setUp() throws Exception {
        selenium = new DefaultSelenium("localhost", 4444, "*firefox",
                UniversalVariables.baseAddress);
        selenium.start();
    }

    @Test
    public void testTestCase1() throws Exception {
        selenium.open(UniversalVariables.loginPage);
        selenium.type("name=USERNAME", UniversalVariables.username);
        selenium.type("name=PASSWORD", UniversalVariables.password);
        selenium.click("link=Submit");
        selenium.waitForPageToLoad("60000");
        selenium.open("/itg/dashboard/app/portal/PageView.jsp?IS_WINDOID=N");
        selenium.click("link=Create");
        selenium.click("link=Request");
        selenium.waitForPageToLoad("30000");
        selenium.select("name=REQUEST_TYPE_CREATE",
                "label=Non-Standard Service");
        selenium.click("css=span.secBtn > a");
        selenium.waitForPageToLoad("30000");
        String title = UniversalVariables.getRandomTitle();
        String req = UniversalVariables.Requestor;
        String dept = UniversalVariables.reqDept;
        String desc = UniversalVariables.getRandomDescription();
        String date = UniversalVariables.getRandomDate();
        System.out.println(title);
        System.out.println(req);
        System.out.println(dept);
        System.out.println(desc);
        System.out.println(date);
        selenium.type("id=REQ.DESCRIPTION", title);
        selenium.type("id=REQ.P.REQUESTORAC_TF", req);
        selenium.select("id=REQ.P.REQUESTOR_DEPT", dept);
        selenium.type("id=REQ.P.DETAIL_DESC", desc);
        selenium.type("REQ.P.REQ_COMPL_DATE", date);
        selenium.click("link=Submit");
        selenium.waitForPageToLoad("30000");
        selenium.refresh();
    }

    @After
    public void tearDown() throws Exception {
        selenium.stop();
    }
}

这是 UniversalVariables.java:

package NSSR;

import java.util.*;

public class UniversalVariables {

    public static String baseAddress = "******";
    public static String loginPage = "******";
    public static String username = "******";
    public static String password = "******";
    public static ArrayList<String> NSSR_Titles = new ArrayList<String>(NSSR_Titles());
    public static String Requestor = "******";
    public static String reqDept = "Shared Services";
    public static ArrayList<String> NSSR_Descriptions = new ArrayList<String>(NSSR_Descriptions());
    public static ArrayList<String> NSSR_Dates = new ArrayList<String>(NSSR_Dates());
    public static Random randy = new Random();
    public static int num;

    public static String getRandomTitle() {
        num = randy.nextInt(NSSR_Titles.size());
        return NSSR_Titles.get(num);
    }

    public static String getRandomDescription() {
        num = randy.nextInt(NSSR_Descriptions.size());
        return NSSR_Descriptions.get(num);
    }

    public static String getRandomDate() {
        num = randy.nextInt(NSSR_Dates.size());
        return NSSR_Dates.get(num);
    }

    public static ArrayList<String> NSSR_Titles() {
        ArrayList<String> list = new ArrayList<String>();
        for(int i = 1; i < 11; i++)
            list.add("Title " + i);
        return list;
    }

    public static ArrayList<String> NSSR_Descriptions() {
        ArrayList<String> list = new ArrayList<String>();
        for(int i = 1; i < 11; i++)
            list.add("Description " + i);
        return list;
    }

    public static ArrayList<String> NSSR_Dates() {
        ArrayList<String> list = new ArrayList<String>();
        for(int i = 1; i < 13; i++)
            for(int j = 1; j < 29; j++)
                for(int k = 13; k < 100; k++) {
                    String str = i + "/" + j + "/20" + k;
                    list.add(str);
                }
        return list;
    }
}

这是我在命令行中输入的(这些是我使用的所有 jar 文件。我知道它不漂亮......请耐心等待。):

>javac -classpath C:\JAR\junit-4.0.jar;C:\JAR\junit-4.0-src.jar;C:\JAR\selenium-java-c
lient-driver.jar;C:\JAR\selenium-java-client-driver-sources.jar;C:\JAR\selenium-
java-client-driver-tests.jar;C:\JAR\selenium-java-client-driver-test-sources.jar
;C:\JAR\selenium-server.jar;C:\JAR\selenium-server-coreless.jar;C:\JAR\selenium-
server-sources.jar;C:\JAR\selenium-server-standalone-2.24.1.jar; NSSR\TestCase1.
java NSSR\UniversalVariables.java

这编译得很好。然后我把它放到命令行中:

>java NSSR.TestCase1

然后是错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/thoughtworks/sele
nium/SeleneseTestCase
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.thoughtworks.selenium.SeleneseT
estCase
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 12 more
Could not find the main class: NSSR.TestCase1.  Program will exit.

仅供参考,这是我的 CLASSPATH:

.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\WINDOWS\system32\java.exe\lib;C:\Program Files\IBM\RationalSDLC\ClearQuest\cqjni.jar;C :\JAR*.jar

任何帮助是极大的赞赏!

4

2 回答 2

3

您还需要将罐子作为参数放入java,就像您传递的一样javac

于 2012-07-09T15:16:09.120 回答
1
java -classpath "C:\JAR\junit-4.0.jar;C:\JAR\junit-4.0-src.jar;C:\JAR\selenium-java-client-driver.jar;C:\JAR\selenium-java-client-driver-sources.jar;C:\JAR\selenium-java-client-driver-tests.jar;C:\JAR\selenium-java-client-driver-test-sources.jar;C:\JAR\selenium-server.jar;C\JAR\selenium-server-coreless.jar;C:\JAR\selenium-server-sources.jar;C:\JAR\selenium-server-standalone-2.24.1.jar" NSSR.TestCase1

应该做你想做的。java还需要知道在哪里可以找到库。它不存储在引用类文件中(也不应该,因为它只适合您自己的系统)

于 2012-07-09T15:23:40.253 回答