1

I have a spring boot application generating code dynamically at runtime and then compiling when the application is run. When App is run in IntelliJ the generated code is compiled without any errors but when I create a fat jar and generate and compile the code I get a package missing error and few other errors

error: package javax.validation.constraints does not exist
import javax.validation.constraints.Size;
error: package javax.validation.constraints does not exist
import javax.validation.constraints.Size;

this is how I am compiling the code

 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
          StandardJavaFileManager sjfm = compiler.getStandardFileManager(null, null, null);
          Iterable fileObjects = sjfm.getJavaFileObjects(entityFiles.toArray(new Path[] {}));
          List<String> optionList = new ArrayList<String>();
          JavaCompiler.CompilationTask task =
              compiler.getTask(null, null, null, optionList, null, fileObjects);
          task.call();
          sjfm.close();

Here is how generated code looks like

package com.abc.def.entities;

import java.io.Serializable;
import java.lang.String;
import java.time.LocalDate;
import java.time.LocalDateTime;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@Table(
    name = "TABLE123"
)
public class EntityV1 implements Serializable {



  @Size(
      min = 1,
      max = 16
  )
  private String anumber;

  @Size(
      min = 15,
      max = 16
  )
  @Id
  private String cnumber;

  @NotNull
  private LocalDateTime acdt

  @Size(
      min = 1,
      max = 16
  )
  private String cu_number;

  @Size(
      min = 1,
      max = 16
  )
 
}

I extracted the fat jar to see if the dependencies are in the jar and they are there, Seems like the compiler is not able to recognise any annotation. Can anyone help?

arshid dar
  • 1,355
  • 2
  • 15
  • 23

0 Answers0