Environment variables in Ubuntu Tomcat, Apache and Java - java

I have a java web app running in Windows enviroment and I want to move it to Ubuntu system.
In windows I use an environment variable to store the main path:
MAIN_PATH=C:\test
This variable is used in many configuration files and also by application, for example:
Apache and Tomcat configuration file
Application logging configuration (log4j)
Java application itself by System.getEnv(...)
examples:
tomcat config
<Host name="localhost" appBase="${MAIN_PATH}/webapps" unpackWARs="true" autoDeploy="true">
apache virtualhost
<VirtualHost *:80>
DocumentRoot "${MAIN_PATH}/www"
ServerName testmain
<Directory "${MAIN_PATH}/www">
...
I can not find some way to set it in ubuntu, I tried this places:
/etc/enviroment
/etc/profile
export MAIN_PATH=/opt/test in /etc/init.d/tomcat7.sh
All this solution didn't work or works just in one of my requirements (only in server configuration but not in application or vice versa).
Is nothing like windows global environment variable to use for all this cases?

Did you check ubuntu/linux documentation? It's fairly simple.
Refer to: https://help.ubuntu.com/community/EnvironmentVariables
This worked perfectly for me.
$ nano /etc/environment (or whatever file editor you want to use)
add 'export MAIN_PATH=/opt/test' to the file and save
$ source /etc/environment
$ echo $MAIN_PATH
Output: /opt/test

I would recommend you keep the environment variables separate for each layer of your application. For example it might seem like putting everything in one place makes sense, but I don't feel it's good design to share a configuration directory for Apache and Tomcat. Both have standard locations for config files (e.g., $CATALINA_HOME/conf).
I wouldn't think you're using log4j on Apache so why put it in a custom directory that's shared with Apache. Put it in a conf folder inside your webapp, or in the previously mentioned top level conf directory. Maybe you'll have more than one webapp on Tomcat -- in that case it makes even more sense to have logging config files within the particular webapp directory.
It's also cleaner to have the environment variables contained within the process. They don't gum up the global environment variables. For example when you start Tomcat, the setenv.sh script runs and sets all the enviroment variables just for the java process that's running Tomcat.

Related

Java Spring project environment property file for both Linux and Window

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

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.

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 ;)

Websphere Liberty 8.5: Setting Java classpath

I am running Websphere Liberty 8.5. My application reads some files from the file system that are obtained via the Java CLASSPATH and I can't seem to find a way to append a directory from the file system to the CLASSPATH Liberty reads from. I've tried manually changing JAVA_CMD and JAVA_DEBUG but none of them take.
Does anyone know how this is possible?
It is not possible to modify the JVM application classpath (normally specified via the -classpath JVM argument or the CLASSPATH environment variable). I would recommend using a <library> to give your applications visibility to the resources. If you need a directory, you should use <library><folder dir="..."/></library>.
(As an aside, modifying the wlp/bin scripts or setting those "internal" script variables is not a supported external.)
ANOTHER WARNING! It only reads JAR files! I had the same issue with endless frustration. Even if you use the <library><folder dir="..."/></library> method above, it will only read jar files from the classpath. I had a bunch of properties files that I read from the classpath in JBOSS, WebSphere (full), and Glassfish, but the same method doesn't work for Websphere Liberty. What I ended up doing is the above, and putting my properties files all in a .jar file. Very annoying but a work-around, in case you need to read non-jar files.

Categories