Issue
I'm having a problem with calling method Files.write(Path.of(fileForDocumentContent.getAbsolutePath()), htmlDocument.getContent()); It throws java.nio.file.NoSuchFileException. even if file really exists.
I want to point that on my local machine it works perfect but it doesn't work on DEV machine which is located on linux.
My code is:
@Override
public Document convert(Document document, DocumentFormat documentFormat) {
Document htmlDocument = htmlDocumentConverter.convert(document, documentFormat);
File fileForDocumentContent = null;
File fileForConvertedContent = null;
try {
log.info("Converting document from {} to {}", getSourceFormat().toString(), getTargetFormat().toString());
CalibreConfigData calibreData = calibreConfig.getConfigurationData(CalibreConversion.HTML_TO_DOCX);
fileForDocumentContent = new File(calibreData.getSourceFilePath());
fileForConvertedContent = new File(calibreData.getConvertedFilePath());
// \/ doesn't work \/
Files.write(fileForDocumentContent.toPath(), htmlDocument.getContent());
Runtime.getRuntime().exec(calibreData.getCalibreCommand()).waitFor();
byte[] convertedFileAsBytes = Files.readAllBytes(fileForConvertedContent.toPath());
return new Document(convertedFileAsBytes);
} catch (InterruptedException | IOException e) {
log.error("Conversion failed due to problem: " + e);
throw new MetanormaConversionException("Conversion failed due to problem: " + e);
} finally {
deleteFilesIfPresent(fileForDocumentContent, fileForConvertedContent);
}
}
fileForDocumentContent = new File(calibreData.getSourceFilePath());
creates file under path: ile:/usr/src/pok-document-service.jar!/BOOT-INF/classes!/5b1138f8-1805-4db8-a5ed-acab8509adc4.html
and exact error looks:
"Conversion failed due to problem: java.nio.file.NoSuchFileException: ile:/usr/src/pok-document-service.jar!/BOOT-INF/classes!/5b1138f8-1805-4db8-a5ed-acab8509adc4.html"
As you can see both paths to the file looks the same and yet it throws an exception. I've tried to change from .toPath() to .getAbsolutePath() but still the same problem appears.
Solution
Please take note that NoSuchFileException can also mean the process running your application does not have enough privileges to create/write on that file, look at the entire trace of the error
That would make sense for me if for example in your local environment you are using eclipse/vscode or similar and your code is writing into the compiled "target" folder
Looking at the path of the error, it looks like you are executing your application using spring boot, and you are trying to modify a file inside your JAR file, in that case you should have an external folder to write into
Hope it helps
Answered By - Int Answer Checked By - Senaida (WPSolving Volunteer)