1

I have condition on web application where I am upload a .java source file and compiling this source based on the dependencies with jar files resides in WEB-INF/lib folder.

I am able to load this jar file by using this code and my file compile successfully and deploy .class in proper package inside WEB-INF/classes. with the help of this thread. How to set classpath when I use javax.tools.JavaCompiler compile the source?.

File[] javaFiles = new File[]{new File(fileToCompile)};
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);

Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(javaFiles));

List<String> optionList = new ArrayList<String>();
optionList.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path")));
fileManager.setLocation(StandardLocation.CLASS_OUTPUT,
                Arrays.asList(new File(servletContextPath + WEB_CLASSES_PATH)));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList, null, compilationUnits);

the only problem I having here when I upload a .java source file which actually have an association with my current application java file this won't find the reference of this source and complaining the error.

Class A implements com.saif.WebDriverEventListener

package com.saif does not exist.

Any help would be appreciated. Thanks,

Community
  • 1
  • 1
Muhammad Saifuddin
  • 1,284
  • 11
  • 29

0 Answers0