我正在处理一个桌面项目,在这个项目中,我动态地向打包的jar中添加一个java类,并在编译后执行相同的java类。我的java类有一些selenium导入,我在项目中添加了selenium作为maven依赖项,但是在运行时编译java文件时,无法识别导入。请帮助我怎样才能使它工作。
以下是用户生成的java类的内容:
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
import org.openqa.selenium.*;
public class CustomClass{
public boolean WebElementVisible(WebDriver driver, WebElement zipCode) {
if (zipCode.isEnabled()) {
return true;
}
return false;
}
}
这就是我如何将生成的java文件添加并编译到打包jar的类路径中。
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
List<String> optionList = new ArrayList<String>();
optionList.add("-classpath");
optionList.add(System.getProperty("java.class.path") + File.pathSeparator +);
Iterable<? extends JavaFileObject> compilationUnit = fileManager
.getJavaFileObjectsFromFiles(Arrays.asList(tempCustomFile));
// file manager in the below is the path of my dynamically added java class
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList, null,
compilationUnit);
if (task.call()) {
URLClassLoader classLoader = new URLClassLoader(
new URL[] { new File(operationalFolderPath).toURI().toURL() });
Class<?> loadedClass = classLoader.loadClass(className);
classLoader.close();
try {
Method[] methods = loadedClass.getMethods();
/*
* Check whether setManyValues() method contains int parameter or not and print
* no of string parameter it contains
*/
Method method = null;
for (Method methodInClass : methods) {
if (methodInClass.getName().equals(utilityEditorManager.getMethodName())) {
method = methodInClass;
}
}
Class<?>[] parameterTypes = method.getParameterTypes();
for (Class paramClassType : parameterTypes) {
if(paramClassType.equals(WebDriver.class) || paramClassType.equals(WebElement.class)) {
isWebType=true;
isExecute=false;
}
}
Object[] objList = new Object[parameterTypes.length];
// setting the value of the parameters to be passed before execution
if (value != null) {
objList = value;
} else {
int index = 0;
// getting default value of parameters if it is not backend execution
for (Class paramClassType : parameterTypes) {
objList[index] = getDefaultValue(paramClassType);
index++;
}
}
try {
Object obj = loadedClass.newInstance();
Object o = method.invoke(obj, objList);
} catch (IllegalArgumentException e) {
log.debug("The parameter's passed to invoke the method utility are incorrect");
} catch (InvocationTargetException e) {
log.debug("The invoked method Utility is facing runtime exceptions");
}
catch(Exception e) {
log.debug("The method utility execution failed due to : " + e.getMessage());
return result;
}
} catch (SecurityException e) {
log.debug("A security exception occurred while executing method utility");
log.error(e);
}
} else {
String compilationError = "";
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
compilationError = compilationError.concat(diagnostic.getKind() + "! \n" + diagnostic.getMessage(null) + "\n");
log.error("An error occurred while compiling the method utility.\n" + compilationError);
}
}
fileManager.close();
当我通过eclipse运行相同的代码时,它工作得很好,我可以使用selenium,但当我将项目打包为jar时,它就不工作了。表示无法识别包org.openqa.selenium.*;
暂无答案!
目前还没有任何答案,快来回答吧!