For our project we needed to compile few class files during run-time, we have started to use JavaCompiler
class. Everything works fine in Eclipse however when we tried to build (Maven) and deploy
we are getting "package does not exist
" error.
Code first:
Files.walk(Paths.get(inputTestcaseFolder))
.filter(Files::isRegularFile)
.filter(path -> path.toAbsolutePath().toString().contains(".java"))
.forEach(fileList::add);
for (Path p : fileList) {
System.out.println("Test Compiled : " + p.getFileName().toString());
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, p.toString());
}
Also tried referencing the class path still the same issue
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
List<String> files = new ArrayList<>();
files.add(p.toString());
Iterable<? extends JavaFileObject> compilationUnits1 =
fileManager.getJavaFileObjectsFromStrings(files);
optionList.addAll(Arrays.asList("-classpath",System.getProperty("java.class.path")));
compiler.getTask(null, fileManager, null, optionList, null, compilationUnits1).call();
fileManager.close();
Jenkins Job configuration:
Build Goal and Options:
-e -X clean compile dependency:copy-dependencies exec:java
-Dexec.mainClass="Performance.BaseFixture.Base" -DM2=D:\ -DM2_HOME=C:\apache-maven-3.2.3
-Dmaven.repo.local=D:\ -Dexec.includeProjectDependencies=true
Error:
[DEBUG] Adding project dependency artifact: httpclient to classpath
[DEBUG] Adding project dependency artifact: xmlbeans to classpath
[DEBUG] Adding project dependency artifact: stax-api to classpath
[DEBUG] Adding project dependency artifact: zip4j to classpath
[DEBUG] joining on thread Thread[Performance.BaseFixture.Base.main(),5,Performance.BaseFixture.Base]
[DEBUG] Setting accessibility to true in order to invoke main().
success
Test Compiled : CaseListFlow.java
src\test\java\TestCases\CaseListFlow.java:3: error: package HTTPClient does not exist
import HTTPClient.HTTPResponse;
^
src\test\java\TestCases\CaseListFlow.java:4: error: package HTTPClient does not exist
import HTTPClient.NVPair;
^