I have a Java program that converts a multi-page file into separate image files. The code works perfectly fine when I run it locally, but when I deploy it into Kubernetes, it doesn't work at all. There are no error messages given, so I'm not sure what's going on.
Here's the code I'm using:
private void convertMultipageFile(File file) throws IOException {
String directoryPath = file.getParent();
log.info(directoryPath);
log.info(String.valueOf(file));
String fileType = "";
ImageIO.scanForPlugins();
ImageInputStream input = ImageIO.createImageInputStream(file);
Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
if (!readers.hasNext()) {
throw new IOException("No image reader found for file: " + file);
}
ImageReader reader = readers.next();
reader.setInput(input);
int numPages = reader.getNumImages(true);
String fileName = file.getName().substring(0, file.getName().lastIndexOf('.'));
String fileExt = file.getName().substring(file.getName().lastIndexOf('.') + 1);
for (int i = 0; i < numPages; i++) {
BufferedImage image = reader.read(i);
ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageIO.write(image, "tif", output);
byte[] pageBytes = output.toByteArray();
output.close();
fileType = calculateOCR(pageBytes);
log.info(fileType);
if (fileType.contains("AnaBelge")) {
String belgeName = fileType.split(",")[1] + "." + fileExt;
String folderName = fileType.split(",")[1];
directoryPath = file.getParent() + "/" + folderName;
File directory = new File(directoryPath);
if (!directory.exists()) {
boolean created = directory.mkdir();
if (created) {
File newFile = new File(directoryPath, belgeName);
ImageIO.write(image, fileExt, newFile);
} else {
log.warn("Failed to create directory.");
}
} }
else {
String newFileName = fileName + "_page" + (i + 1) + fileType + "." + fileExt;
File newFile = new File(directoryPath, newFileName);
ImageIO.write(image, fileExt, newFile);
}
}
And this is the error I got from logs but sometimes it just doesnt give any error ->
java.io.IOException: No image reader found for file: /file/25-04-2023/001.tif
at com.test.ms.test.Scheduled.ScheduledOcrJob.convertMultipageFile(ScheduledOcrJob.java:161) ~[classes!/:0.0.1]
at com.test.ms.test.Scheduled.ScheduledOcrJob.convertFilesInDirectory(ScheduledOcrJob.java:130) ~[classes!/:0.0.1]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_342]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_342]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_342]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_342]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) [spring-context-5.2.3.RELEASE.jar!/:5.2.3.RELEASE]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_342]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_342]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_342]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_342]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_342]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_342]
at java.lang.Thread.run(Thread.java:750) [na:1.8.0_342]
I'm using Kubernetes to deploy my application, and I'm not sure what the issue is. Any help would be greatly appreciated!
It seems that my intellij project version was jdk11 but it was 8 in dockerfile and imageio doesn't support tiff files in versions before 9 :D when I upgraded to java11 the problem resolved.