简体   繁体   English

无法从命令行运行Java单元测试代码

[英]Unable to run Java unit test code from the command line

I'm a Java newbie. 我是Java新手。 I want to run a Java Unit test on the command line and eventually run it from a PHP script. 我想在命令行上运行Java单元测试,并最终从PHP脚本运行它。 A snippet of my code follows 我的代码片段如下

package com.src.test;

import java.io.File;
import java.io.FileWriter;
import java.util.concurrent.TimeUnit;
import org.junit.*;

import static org.junit.Assert.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.thoughtworks.selenium.Selenium;

public class First {
......
}

The full path to the above file on Windows is D:\\Users\\XXX\\workspace\\SeleniumTest\\bin\\com\\src\\test Windows上上述文件的完整路径为D:\\Users\\XXX\\workspace\\SeleniumTest\\bin\\com\\src\\test

When I change directories into bin and run java com.src.test.First I get an error message: 当我将目录更改为bin并运行java com.src.test.First收到错误消息:

Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
        at java.net.URLClassLoader$1.run(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)
        ... 6 more

What am I doing wrong? 我究竟做错了什么? I've tried running java com.src.test.First.class and I get an error Error: Could not find or load main class com.src.test.First.class 我尝试运行java com.src.test.First.class ,但出现错误Error: Could not find or load main class com.src.test.First.class

You should compile your code using the -d option, to specify where to place the compiled files. 您应该使用-d选项来编译代码,以指定放置编译文件的位置。

If you had already done that, you need to be behind .\\com . 如果您已经这样做了,那么您需要落后于.\\com If you are not, you should run with the -cp option, showing where are your files. 如果不是,则应使用-cp选项,显示文件在哪里。 If you are behind .\\bin it should look like this: 如果您位于.\\bin后面,则它应如下所示:

java -cp .\bin com.src.test.First

Also, you should not write the .class after your class name. 同样,您不应在类名之后编写.class


Edit : 编辑

When I say you need to be behind .\\something , I mean you should be in the directory that contains the folder "something". 当我说您需要位于.\\something ,我的意思是您应该位于包含“ something”文件夹的目录中。 Also, you only need to specify extra class paths with -cp when the directory is not in the java classpath. 此外,仅当目录不在Java类路径中时,才需要使用-cp指定额外的类路径。 You could always add it to the environment variable CLASSPATH . 您总是可以将其添加到环境变量CLASSPATH More info on this subject here 有关此主题的更多信息,请点击此处


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

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