PropertiesLoaderUtils.loadAllProperties is not able to load the property file from src/main/resources - spring-boot

I am using PropertiesLoaderUtils.loadAllProperties(abc.properties);
and have placed abc.properties in src/main/resources folder but still it's not able to get this property file.
Code that we tried ::
try {
props = PropertiesLoaderUtils.loadAllProperties("abc.properties");
} catch (IOException e) {
logger.error("Unable to load file", e);
}
Can someone help me out with the possible cause of not picking up abc.properties whereas if I change it to application.properties, its able to load.

Related

Why is everything inside a BOOT-INF folder and how to get rid of it in case it's breaking my classpath?

I am getting a classic well-known error in my Springboot app when it tries to load a static resource using getResourceAsStream():
java.io.FileNotFoundException: class path resource [blah blah blah] cannot be opened because it does not exist
While doing some basic troubleshooting, I opened the contents of the generated jar file and found that everything in the jar is contained inside a folder named BOOT-INF.
Why is everything inside BOOT-INF?
How do I get rid of this?
UPDATE
Here's the actual relevant part of the code
ClassPathResource cpr = new ClassPathResource(RESOURCE_CLASSPATH_PREFIX + url);
try (InputStream is = cpr.getInputStream()) {
return DigestUtils.sha1Hex(is);
} catch (IOException e) {
throw new RuntimeException(e);
}
Additional Clue
The failing code is inside a ServletFilter. I think this has something to do with class loaders.

FileAlreadyExistsException for a deleted directory

For the purpose of making a backup, I copy a directory with tests scripts to another location. For this I'm using Files.walkFileTree(fromDir, SimpleFileVisitor).
This works as expected and I can copy it multiple times without any problems.
But when I delete the target directory via Windows File explorer I get a java.nio.file.FileAlreadyExistsException on the target directory when I try to make a back up.
I'm pretty sure that the target directory is not there.
When I close the Java program and start it again, it works again. It is as if manually deleting the directory locks the 'non-existing' target directory and Java throws a FileAlreadyExistsException.
Here are the relevant parts of the SimpleFileVisitor
I'm using these options when copying the files
StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES, LinkOption.NOFOLLOW_LINKS
#Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException
{
Path copyDir = toDir.resolve(fromDir.relativize(dir));
try {
Files.createDirectories(copyDir);
}
catch(IOException ioe) {
LOG.error("Failed to create directory " + copyDir.toString());
throw ioe;
}
return FileVisitResult.CONTINUE;
}
#Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
{
Path toFile = toDir.resolve(fromDir.relativize(file));
try {
Files.copy(file, toFile, opts);
}
catch(IOException ioe) {
LOG.error("Failed to create file " + toFile.toString());
throw ioe;
}
return FileVisitResult.CONTINUE;
}
I was expecting that deleting the target directory via Windows File Explorer would not cause this problem, but apparently it does.
Is there anything I could do to make the copy action possible in case someone deletes the directory via Windows File Explorer.

How to create a permanent directory for my files with spring boot

I am working with spring boot and spring content. I want to store all my pictures and videos in one directory but my code continues to create different dir every time I rerun the application
I have such bean and when I run the app again it shows null pointer because the dir already exists but I want it to create it just once and every file is stored there
every time i run this tries to create the dir again
#Bean
File filesystemRoot() {
try {
return Files.createDirectory(Paths.get("/tmp/photo_video_myram")).toFile();
} catch (IOException io) {}
return null;
}
#Bean
FileSystemResourceLoader fileSystemResourceLoader() {
return new FileSystemResourceLoader(filesystemRoot().getAbsolutePath());
}
One solution, would be to check if the directory exists:
#Bean
File filesystemRoot() {
File tmpDir = new File("tmp/photo_video_myram");
if (!tmpDir.isDirectory()) {
try {
return Files.createDirectory(tmpDir.toPath()).toFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return tmpDir;
}
You can use isDirectory() method first to check if the directory already exists. In case it does not exist, then create a new one.
Meanwhile there is another way to achieve this, when you use Spring Boot and accordingly spring-content-fs-boot-starter.
According to the documentation at https://paulcwarren.github.io/spring-content/refs/release/fs-index.html#_spring_boot_configuration it should be sufficient to add
spring.content.fs.filesystemRoot=/tmp/photo_video_myram
to your application.properties file.

JavaFX: Trying to run the command line 'pdflatex myfile.tex'

My question is two parts.
First part:
I am writing a series of latex files, and I would like to compile them.
The command works if I type it manually. so the syntax should be correct. But when I run it from java application, it won't compile.
This code, in Java creates a latex file correctly--as expected.
File latexFile = fileChooser.showSaveDialog(window.stage);
if (latexFile != null) {
try {
writeContentToFile(content, latexFile);
} catch (IOException ex) {
Logger.getLogger(DocumentViewPort.class.getName()).log(Level.SEVERE, null, ex);
}
}
I can compile the generated file in the command line if I type pdflatex and then add the file path and get a PDF file.
but it does not seem to work when doing it from java itself like in here:
try {
Runtime.getRuntime().exec( "pdflatex " + latexFile.getPath());
} catch (IOException ex) {
Logger.getLogger(FileAndContentCreation.class.getName()).log(Level.SEVERE, null, ex);
}
any ideas why?
I tried to read online on many places, I can't find out what part is the problematic one. Any help would be greatly appreciated. Thanks.
Second part
My second part of the question is changing the directory, I am using:
Runtime.getRuntime().exec( "cd " + myNewdirectory.getPath());
But it fails with an error=2
Any ideas how to change the current directory?

BlueJ picture in jar

I've just finished a GUI project and I want to figure out how to get an image that I reference into the completed jar file. As of right now I just reference it as:
try {
img = ImageIO.read(new File("<name>.png"));
} catch (IOException e) {
}
in my project.
What do I need to do to get it to show up in my program?
By the time of Jarring, those resources will have become an embedded-resource. That being the case, the resource must be accessed by URL instead of File. See the info page for the tag, for a way to form an URL.
General Tip
Change code of the form
catch (Exception e) {
// ..
To:
catch (Exception e) {
e.printStackTrace(); // very informative!
// ..

Resources