0

java.io.FileNotFoundException: class path resource [] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/Users/msi/Desktop/spring-boot-tapcell-aws-exe.jar!/BOOT-INF/classes!/

In the spring boot Application Run It works fine. But After creating jar file when I run this it shows file not found exception

@Component public class FileUploadHealper {

public final String UPLOAD_DIR = new ClassPathResource("/static/").getFile().getAbsolutePath();
        //new ClassPathResource("static").getFile().getAbsolutePath();

public FileUploadHealper( ) throws IOException{
    
}
@Getter
@Setter
private long currDatefile;

public Boolean uploadProFile(MultipartFile multipartFile,String roll) {
    initFewDirectory();
    createStudentRollDirectory(roll);
    Date date = new Date();
    long currDate = date.getTime();
    boolean f=false;

    try {
        System.out.println("hello  sir");
        Files.copy(multipartFile.getInputStream(),Paths.get(UPLOAD_DIR+"/image/students/"+roll+"/"+File.separator+String.valueOf(currDate)+multipartFile.getOriginalFilename()) , StandardCopyOption.REPLACE_EXISTING);
        f=true;
        setCurrDatefile(currDate);
        System.out.println(new ClassPathResource("/static/image/").getFile().getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    
    return f;
    
}
public Boolean uploadNotice(MultipartFile multipartFile) {
    initFewDirectory();
    Date date = new Date();
    long currDate = date.getTime();
    boolean f=false;

    try {
        Files.copy(multipartFile.getInputStream(),Paths.get(UPLOAD_DIR+"/image/notice/"+File.separator+String.valueOf(currDate)+multipartFile.getOriginalFilename()) , StandardCopyOption.REPLACE_EXISTING);
        f=true;
        setCurrDatefile(currDate);
        System.out.println(new ClassPathResource("/static/image/notice/").getFile().getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    
    return f;
    
}

private void createStudentRollDirectory(String roll) {
    String path = UPLOAD_DIR+"/students/"+roll;
    File pathAsFile = new File(path);

    if (!Files.exists(Paths.get(path))) {
        System.out.println("success");
        pathAsFile.mkdir();
    }
}

public  void initFewDirectory() {
    System.out.println("this is test");
    String staticpathString = null;
    try {
        staticpathString = new ClassPathResource("/static/").getFile().getAbsolutePath();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String path[] = {staticpathString,UPLOAD_DIR,UPLOAD_DIR +"/students/",UPLOAD_DIR+"/notice/"};
    for(String x : path) {
    File pathAsFile = new File(x);

    if (!Files.exists(Paths.get(x))) {
        System.out.println("success");
        pathAsFile.mkdir();
    }
    }
}

}

  • Please show the relevant code. This issue normally happens when you try to access an inner filelresource using methods that only work for external files. To access inner files you need to use one of the getResource methods: [How to get a path to a resource in a Java JAR file](https://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file) The only reason it works in your IDE is that the files are not yet compiled so they can be accessed with normal File methods, so it's a bit of a trap for developers not aware of file loading details. – sorifiend Jun 27 '22 at 23:40
  • Please see the code i have added – Mukesh Mandal Jun 28 '22 at 04:51
  • 1
    You cannot upload to the classpath... When running from the IDE it is a file system based and will work, when running the jar you will be suddenly adding stuff to the jar. This will obviously not work. – M. Deinum Jun 28 '22 at 05:45
  • what should i do to work it properly – Mukesh Mandal Jun 28 '22 at 20:04

0 Answers0