Java Spring project environment property file for both Linux and Window - java

I have a java spring project that contains property files. In the file, there are lots of folder path definitions. But these path are Linux paths. i.e. /home/share/Document. I would like to develop the project under both Linux and Windows so I have created a network share that I can access Linux file under Window environment. However, I have to add a prefix in front of the Linux path
such as \network\...\home\share\Document in order to make the path work.
What is the standard/normal way to handle this? I can image I should have two environment property files. For example, one is called DEV_unix.properties and the other is call DEV_win.properties. But this doesn't look perfect.
Could someone share his/her insights please ?

You can use profile in this case. Set active profile in your application.{profile}.properties. You can set this profile as a environment variable or VM arguments so that you need to set the environment variable in different machines based on the Operating system. You can check the below link for more details.
Springboot not loading application.dev.properties file

Related

How to add Azure Monitor OpenTelemetry-based auto-instrumentation to Spring Boot Application?

The documented way to add Azure Monitor OpenTelemetry to Java application is by downloading applicationinsights-agent-3.2.11.jar and using the following:
-javaagent:path/to/applicationinsights-agent-3.2.11.jar.
So in Spring Boot, the way it could possibly be run:
java -javaagent:path/to/applicationinsights-agent-3.2.11.jar -jar <jar-file.jar>.
But what happens if this path varies?
The problem is depending on the system it is running, and using maven (pom.xml) to get the artifact, how do we enable opentelemetry with applicationinsights-agent-3.2.11.jar since the location Maven stores the artifact changes from user accounts and computer?
Additionally, how do I specify a relative path to applicationinsights.json file for configuration (as Azure looks for this file inside the applicationinsights-agent-3.2.11.jar directory)?
If you specify a relative path, it will be resolved relative to the
directory where applicationinsights-agent-3.2.11.jar is located.
UPDATE
Regarding suggestions, How to define a relative path in java and this other suggested question
I am not looking to read files from relative paths.
The spring boot application needs to be invoked with a specific argument where it needs to be made aware of the location where maven downloads the the appinsights jar file.
Then when the app starts, the appinsights autoconfigure based on applicationinsights.json file, which once again, may vary by location.
Note that the latest applicationinsights-agent*.jar is available through GitHub and not through Maven repository. Therefore, I don't think that it can be downloaded as dependency to the project using the pom.xml
Now, the question here is not specific to ApplicationInsights agent, but for any java agent used for monitoring. The -javaagent parameter is supplied to JVM (java commandline) along with the path. Therefore, it will have to be supplied when JVM starts. How it is being setup would depend on the Server (or the standalone application) being used and its starting mechanism. One such solution is discussed here: How to attach a java agent on to a running spring-boot application. As there are many ways that the application can be deployed/run, the relative path would vary based on it.
If you are running standlone springboot application, you may also modify the mvnw or mvnw.cmd scripts to include %MAVEN_OPTS% with the -javaagent=agent path, where path could be relative to one of the variables defined in it, like %MAVEN_PROJECTBASEDIR%
Regarding the applicationinsights.json file, you can either have it in the same directory as the agent jar, OR set up environment variables to get the settings from there (instead of having the json file). You may refer to this link for details on available Environment variables to configure the agent. These environment variables can be set based on how/where the application is being deployed/run before the JVM initializes to make it available to javaagent.

How to create Environment variable (space) in AWS?

I am new to Amazon Web Services. I have used Elastic BeanStalk to host our web application. In web application, I have used the environment variable assigned with some location to push the files into that.
I have set the VENDOR_HOME as,
D:\Applications\spring-boot\Resume_folder
and accessing through java using System.getenv in as
String path = System.getenv("VENDOR_HOME");
It is working fine in the local tomcat server, I want the same functionality should achieve in AWS too? any help will be appreciated.
In your BeanStalk application, go to the Configuration page. Locate Software Configuration. Click on edit. You have the option to add Environment Properties at the bottom of this page.
Tomcat typically has a setenv.sh file where admins can set environment variables. In the setenv.sh file you can put a line for your VENDOR_HOME variable as such:
export VENDOR_HOME=/Applications/spring-boot/Resume_folder
For AWS BeanStalk specifically you can look at this answer for details on how to set the env variables.
In general you can set Linux environmental variables using the export command from within the script you are using to execute your application. The final answer will depend on how you are executing your application.
I typically use Spring Boot Maven Plugin that creates executable JAR files. The executable JAR file allows for a config file with the same name as the JAR file (.config instead of .jar) where admins can export environmental variables from.

Inno Setup constant equivalent for System.getProperty("user.home")

I'm storing a preferences file for bundled JavaFX app under user directory, which I create at Runtime using
System.getProperty("user.home")
I want to order InnoSetup to delete that file at uninstall, but I can't find appropriate Inno Constant.
Please, advice.
On Windows, user.home maps to %USERPROFILE% environment variable, what is typically C:\Users\username.
There's no equivalent constant for that in Inno Setup. Probably because you are not supposed to store any files there. You should store your files to C:\Users\username\AppData\Roaming\app (or ...\Local\app). In Inno Setup that is {userappdata}\app (or {localappdata}\app). It Java, you can use System.getenv("APPDATA") (or System.getenv("LOCALAPPDATA")).
Anyway, to answer your question, you can use {%USERPROFILE}.
See also Inno Setup User Home Path.
Also in general, it's a bad idea to try to access a user profile in Inno Setup (un)installer, as the (un)installer can be executed using a different local account (typically Administrator), than the one you are interested in.
This is covered in Installing application for currently logged in user from Inno Setup installer running as Administrator.

How do I change the Tomcat default working directory?

In my web application, when I do new File(".").getAbsolutePath(), the path returned is <tomcat_home>/bin. I want to change it to <tomcat_home>/webapps/<app_name>.
The default directory for HTML ./path works fine and is the path I want. But the Java path ./Path is different in the same project.
I have tried to add a parameter like workDir="Path" in the <Host> area of the server.xml file on my Tomcat server, but it doesn't work.
How do I change it?
The general question of how to change the working directory of a java process has been asked before. The simple answer is that the java language and the java virtual machine don't provide a way to change the working directory of the JVM process. You won't be able to change the working directory after tomcat has started.
Tomcat's startup scripts (bin/startup.sh and so on) don't set a working directory. The tomcat process will normally inherit whatever the current directory was for the startup script. See this question. To make tomcat start in a different working directory, you'll have to figure out what is launching tomcat, and change that process to change to the desired directory before it runs startup.sh.

Java Servlet absolute paths outside webfolder [ Windows & Linux ]

Disclaimer : At the moment, due to lack of a Linux dev/test server, I am currently unable to test this myself. Hence me asking the question here. I will have a Linux box eventually but am currently confined to using Windows.
I am designing a webapp that will run in Tomcat 7 on Windows & Linux.
Supposing on both systems the WAR is deployed in the following locations (respectively)
/opt/Tomcat/webapps/MyApp
C:/opt/Tomcat/webapps/MyApp
The Webapp has a Servlet which needs to process files from the OS file system in the following locations
/work/logs/<logfiles>
C:/work/logs/<logfiles>
On Windows I can specify C:/work/logs and the Servlet knows to pick up this absolute path and it works fine. I suspect because the C: at the start. I know this because I have tested this.
My real question is...
On Linux, in the absence of a drive letter, if I ask it to look in /work/logs will it try to look at a relative path :
/opt/Tomcat/webapps/MyApp/work/logs
or will it look in the file system (/work/logs) as I'd like it to?
I'm asking this now because it will ultimately affect the overall design.
Leading slash in Linux means "absolute path", so you can be sure that if you use path like /work/logs/<something> it will understand it as an absolute path.
BTW if you use the same path in Windows it will work and use current disk, i.e. if tomcat's working directory is on C: it will use C: drive, however if tomcat is running on D: this drive will be used.
I can recommend using CATALINA_HOME environment variable to find a folder in which you place your config. For my projects I have the following structure:
$CATALINA_HOME/appconfig/ <-- config
$CATALINA_HOME/webapps/somewar.war <-- your webapps
Inside appconfig I will place a somewar.properties which functions as the configuration for that server. (I also place log4j.properties, and any special certificates / other things that are specific to the server instance you run on, but I try to keep it to a minimum)
As there will always be a CATALINA_HOME set for your project it is reusable.
inside the somewar.properties I would then list
work.path=/work/logs
Using /work/logs will work on both linux and windows as #AlexR mentions (in his much more to the point answer) but you can get into trouble as on linux you would need root access to be allowed to create the /work folder. It depends on who is managing the server and how strict they are.
I prefer the configuration solution so if you end up working on a machine that won't let you use a certain location, you can switch. Ofcourse you do need to be allowed to write inside CATALINA_HOME ;)

Categories