-1

In the project, I dynamically generate a POJO class. After generating this POJO class, I need to use this class in other places, but there will be an error "symbol not found". For example, when I generate this class with the name of "User" and use "User" in the code generated in another section, there will be an error of symbol not found.

public void run(SourceContext<User> sourceContext) throws Exception

In this automatically generated code, the prompt cannot find the symbol "User",How can I solve this problem?

  • More context please. You are generating a source file (and compiling it) in the same program that then tries to use the code? And you made sure the path of the resulting class in on the classpath? – kutschkem May 24 '22 at 13:24
  • In the first step of a piece of code, I created the POJO source code and compiled it. Then I created other source codes, but I need to use the source code created in the first step. The above problems arise when compiling this new code.II don't particularly understand classpath – Maria Isla May 24 '22 at 13:36
  • Classpath is where the ClassLoader looks for classes when it encounters a reference to an unloaded class. https://stackoverflow.com/questions/2396493/what-is-a-classpath-and-how-do-i-set-it – kutschkem May 24 '22 at 13:39
  • Thank you very much, but the POJO code and compiled class files are generated at runtime. How can I add the class path to the classpath in the code? – Maria Isla May 24 '22 at 13:45
  • Where are you storing the class file of the runtime-compiled class? On the file system? In memory? – Slaw May 24 '22 at 13:46
  • I think it's on the file system – Maria Isla May 24 '22 at 13:53
  • Then you should be able to place the class file's location on the class-path (take the package into account) when configuring the compiler for the other source files. – Slaw May 24 '22 at 14:07
  • I've just tried and still can't solve the problem, so I'm thinking maybe the class file is in memory. Let me show you the code for compiling java files: JavaCompiler compiler= ToolProvider.getSystemJavaCompiler(); int result =compiler.run(null,null, null, javaFile.getCanonicalPath()); javaFile is a file object that points to the automatically generated java file path. – Maria Isla May 24 '22 at 14:15
  • I have solved this problem. I can successfully put the generated class file in the target directory. Thank you very much for your help – Maria Isla May 24 '22 at 14:32

0 Answers0