0

I am compiling this class with JavaCompiler and it gives me a compilation error if the String that I pass to compile is this:

public class className  extends classNeed{

}

But if I delete public it works. What could I do to make it compilable with:

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

String program = code;
Iterable<? extends JavaFileObject> fileObjects;
fileObjects = getJavaSourceFromString(program);

String[] options = new String[]{"-d",  contextClass.getPath()+"temp/"+pathJar+"/",
"-classpath",contextClass.getBasePath()+"WEBINF/lib/lib1.jar;
"+contextClass.getBasePath()+"WEB-INF/lib/lib2.jar"};



compiler.getTask(null, null, null, Arrays.asList(options), null, fileObjects).call();

and it still working being public?

EDIT: ERROR string:///code.java:16: class className is public, should be declared in a filed className.java

but the name is the same

user207421
  • 305,947
  • 44
  • 307
  • 483

1 Answers1

1

Public classes need to be declared in their own files?

I think the compiler doesn't feel that the input has come from a correctly-named .java file, as per the language specification. (Public classes must be defined in their own files).

Maybe you've got the paths wrong, the Java compiler appears to reject it based on Java file/ declared package and classname mismatch.

Thomas W
  • 13,940
  • 4
  • 58
  • 76