有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Selenium脚本在eclipse中运行,但不是作为jar运行

我已经编写了这个selenium脚本,它在eclipse中运行良好,但当我将其打包为jar时,它就没有运行。 代码就像

     package rcm.Selenium.Test;
    import java.text.*;
    import java.util.*;
    public class Calculations 
    {   private Long difference;
    public Long timeDifference(String weboutput)
    {   try 
        {       Calendar calendar = GregorianCalendar.getInstance(); 
                Calendar today = new GregorianCalendar();               
                Date inputTime;
                if (weboutput.length() <= 11)
                {   // for data fetched for current date.
                    DateFormat formatter = new SimpleDateFormat("hh:mm:ss a", Locale.US);
                    inputTime = formatter.parse(weboutput);
                    calendar.setTime(inputTime);
                    int hour = calendar.get(Calendar.HOUR);
                    int minute = calendar.get(Calendar.MINUTE);
                    int second = calendar.get(Calendar.SECOND);
                    today.setTime(new Date());
                    today.set(Calendar.HOUR, hour);
                    today.set(Calendar.MINUTE, minute);
                    today.set(Calendar.SECOND, second);
                }
                else 
                {   if (weboutput.length() <= 15)
                    {   // for data for earlier date in same year or month.
                        DateFormat formatter = new SimpleDateFormat("MMM dd hh:mm a" , Locale.US);
                        inputTime = formatter.parse(weboutput);
                        calendar.setTime(inputTime);
                        int hour = calendar.get(Calendar.HOUR);
                        int minute = calendar.get(Calendar.MINUTE);
                        int month = calendar.get(Calendar.MONTH);
                        int date = calendar.get(Calendar.DATE);
                        today.setTime(new Date());
                        today.set(Calendar.HOUR, hour);
                        today.set(Calendar.MINUTE, minute);
                        today.set(Calendar.MONTH, month);
                        today.set(Calendar.DATE, date);
                    }
                    else
                    {   // for data with different year.
                        DateFormat formatter = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a" , Locale.US);
                        inputTime = formatter.parse(weboutput);
                        calendar.setTime(inputTime);
                        int hour = calendar.get(Calendar.HOUR);
                        int minute = calendar.get(Calendar.MINUTE);
                        int second = calendar.get(Calendar.SECOND);
                        int month = calendar.get(Calendar.MONTH);
                        int date = calendar.get(Calendar.DATE);
                        int year = calendar.get(Calendar.YEAR);
                        today.setTime(new Date());
                        today.set(Calendar.HOUR, hour);
                        today.set(Calendar.MINUTE, minute);
                        today.set(Calendar.SECOND, second);
                        today.set(Calendar.YEAR, year);
                        today.set(Calendar.MONTH, month);
                        today.set(Calendar.DATE, date);                 
                    }
                }               
                Date retrivedDate = today.getTime();
                Calendar cal = Calendar.getInstance();
                Date currentDate = cal.getTime();
                difference = currentDate.getTime() - retrivedDate.getTime(); 
                System.out.println(retrivedDate);
                System.out.println(currentDate);
                System.out.println(difference); 

        } 
        catch (ParseException e) 
        {       e.printStackTrace();
        }
        return difference; 
    }
    public boolean alarmValue(Long alarmTime)
    {   if (alarmTime <= 1800000 )  // change this value for Alarm duration, currently 30 min = 30* 60 s = 1800 * 1000 ms = 1800000 ms.
            return false;
        else
            return true;        
    }
     }

And the main class is :




         package rcm.Selenium.Test;
        import java.io.File;
     import java.util.concurrent.TimeUnit;
     import org.openqa.selenium.By;
     import org.openqa.selenium.WebDriver;
     import org.openqa.selenium.ie.InternetExplorerDriver;
         public class RcmSeleniumTest 
     {  public static boolean alarm;
        public static int myInt;
        public static void main(String[] args) 
        {   //Specify location of IE driver here, below is my IE driver location. 
            File file = new File("resources\\IEDriverServer.exe");
        System.setProperty("webdriver.ie.driver", file.getAbsolutePath());

        // Create a new instance of the ie driver.
        WebDriver driver = new InternetExplorerDriver();
        try 
        {   // Navigate to URL, Specify RCM URL here.
            driver.get("URL HERE");

            // Maximize the window.
            driver.manage().window().maximize();

            // To be used in lower environments with no single signon.
                TimeUnit.SECONDS.sleep(5);
                /*driver.findElement(By.id("loginForm:workspace_login_user_name")).clear();
                driver.findElement(By.id("loginForm:workspace_login_user_name")).sendKeys("*****");
                driver.findElement(By.id("loginForm:workspace_login_password")).clear();
                driver.findElement(By.id("loginForm:workspace_login_password")).sendKeys("******");
                driver.findElement(By.id("loginForm:submitbutton")).click();*/

            //Wait to let workspace open properly
            TimeUnit.SECONDS.sleep(10);
            System.out.println("Sorting");

            // First click on creation time.
            driver.findElement(By.id("portletComponentWorkList_viewNormalModeWorkList_viewPanel_instanceListTableWorkList_Header10")).click();
            // wait for click change to reflect.
            System.out.println("First click on Creation Time");
            TimeUnit.SECONDS.sleep(10);



            // Second Click on creation time, for required sorting.
            driver.findElement(By.id("portletComponentWorkList_viewNormalModeWorkList_viewPanel_instanceListTableWorkList_Header10")).click();
            System.out.println("Second Click on Creation Time");
            //wait for click change to reflect.
            TimeUnit.SECONDS.sleep(10);         

            System.out.println("Sorting Done"); 
            //driver.findElement(By.xpath("//table[@id='portletComponentWorkList_viewNormalModeWorkList_viewPanel_instanceListTableWorkList']/tbody/tr/td[11]")).click();
            //TimeUnit.SECONDS.sleep(5);
            //System.out.println("First Value Selected");

            // Get the time from RCM Workspace.
            String creationTime = driver.findElement(By.xpath("//table[@id='portletComponentWorkList_viewNormalModeWorkList_viewPanel_instanceListTableWorkList']/tbody/tr/td[11]")).getText();
            TimeUnit.SECONDS.sleep(3);
            System.out.println(creationTime);

            // Logout is must
            driver.findElement(By.id("_id9_signOutLinkLink")).click();  
            System.out.println("RCM User Logged Out");

            // Close the web page.
            driver.close(); 
            System.out.println("RCM Web Page Closed");


            // Calculate time difference and check alarm.
            System.out.println("Calculating the time difference");
            Calculations calOb = new Calculations();
            Long alarmTime = calOb.timeDifference(creationTime);
            alarm = calOb.alarmValue(alarmTime);
            myInt = (alarm) ? 1 : 0;
            System.out.println("alarm is :" + myInt);
        }
        catch (InterruptedException e1) 
        {   e1.printStackTrace();
            driver.close();
        }
    }
     }

当我运行这个jar文件时,出现以下错误

C:\Users\pna105\terminal test rcm>java -cp .:selenium-server-standalone-2.20.0.j
ar -Xmx1024M -jar RCMSELENIUM.jar -o true
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/W
ebDriver
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2570)
        at java.lang.Class.getMethod0(Class.java:2813)
        at java.lang.Class.getMethod(Class.java:1663)
        at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)

Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 6 more

我不知道为什么它不是作为jar运行的,在此之前它有google代码错误,包括guava。来自google代码项目的jar文件


共 (1) 个答案

  1. # 1 楼答案

    在运行终端时遇到以下问题,但现在它正在工作,请注意以供参考

    Encoding error : add "-encoding utf-8" while compiling the code.
    classpath error : add (-cp "your jar folder"\*)though it is found in many places that wild card doesn't work, but it worked in my case.
    no class def found error while running code : include "your jar folder" which now contains all class files needed in your classpath in java command or make one classpath entry for this.
    

    我的问题解决了,如果有任何疑问,请告诉我