selenium—如何在运行时从动态添加的java类中引用打包的jar的类路径内容

pprl5pva  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(232)

我正在处理一个桌面项目,在这个项目中,我动态地向打包的jar中添加一个java类,并在编译后执行相同的java类。我的java类有一些selenium导入,我在项目中添加了selenium作为maven依赖项,但是在运行时编译java文件时,无法识别导入。请帮助我怎样才能使它工作。
以下是用户生成的java类的内容:

  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4. import java.lang.*;
  5. import org.openqa.selenium.*;
  6. public class CustomClass{
  7. public boolean WebElementVisible(WebDriver driver, WebElement zipCode) {
  8. if (zipCode.isEnabled()) {
  9. return true;
  10. }
  11. return false;
  12. }
  13. }

这就是我如何将生成的java文件添加并编译到打包jar的类路径中。

  1. DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
  2. JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  3. StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
  4. List<String> optionList = new ArrayList<String>();
  5. optionList.add("-classpath");
  6. optionList.add(System.getProperty("java.class.path") + File.pathSeparator +);
  7. Iterable<? extends JavaFileObject> compilationUnit = fileManager
  8. .getJavaFileObjectsFromFiles(Arrays.asList(tempCustomFile));
  9. // file manager in the below is the path of my dynamically added java class
  10. JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList, null,
  11. compilationUnit);
  12. if (task.call()) {
  13. URLClassLoader classLoader = new URLClassLoader(
  14. new URL[] { new File(operationalFolderPath).toURI().toURL() });
  15. Class<?> loadedClass = classLoader.loadClass(className);
  16. classLoader.close();
  17. try {
  18. Method[] methods = loadedClass.getMethods();
  19. /*
  20. * Check whether setManyValues() method contains int parameter or not and print
  21. * no of string parameter it contains
  22. */
  23. Method method = null;
  24. for (Method methodInClass : methods) {
  25. if (methodInClass.getName().equals(utilityEditorManager.getMethodName())) {
  26. method = methodInClass;
  27. }
  28. }
  29. Class<?>[] parameterTypes = method.getParameterTypes();
  30. for (Class paramClassType : parameterTypes) {
  31. if(paramClassType.equals(WebDriver.class) || paramClassType.equals(WebElement.class)) {
  32. isWebType=true;
  33. isExecute=false;
  34. }
  35. }
  36. Object[] objList = new Object[parameterTypes.length];
  37. // setting the value of the parameters to be passed before execution
  38. if (value != null) {
  39. objList = value;
  40. } else {
  41. int index = 0;
  42. // getting default value of parameters if it is not backend execution
  43. for (Class paramClassType : parameterTypes) {
  44. objList[index] = getDefaultValue(paramClassType);
  45. index++;
  46. }
  47. }
  48. try {
  49. Object obj = loadedClass.newInstance();
  50. Object o = method.invoke(obj, objList);
  51. } catch (IllegalArgumentException e) {
  52. log.debug("The parameter's passed to invoke the method utility are incorrect");
  53. } catch (InvocationTargetException e) {
  54. log.debug("The invoked method Utility is facing runtime exceptions");
  55. }
  56. catch(Exception e) {
  57. log.debug("The method utility execution failed due to : " + e.getMessage());
  58. return result;
  59. }
  60. } catch (SecurityException e) {
  61. log.debug("A security exception occurred while executing method utility");
  62. log.error(e);
  63. }
  64. } else {
  65. String compilationError = "";
  66. for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
  67. compilationError = compilationError.concat(diagnostic.getKind() + "! \n" + diagnostic.getMessage(null) + "\n");
  68. log.error("An error occurred while compiling the method utility.\n" + compilationError);
  69. }
  70. }
  71. fileManager.close();

当我通过eclipse运行相同的代码时,它工作得很好,我可以使用selenium,但当我将项目打包为jar时,它就不工作了。表示无法识别包org.openqa.selenium.*;

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题