System.getProperty("user.dir") gives different path in console and web application - java

I want to understand working of System.getProperty("user.dir") in different applications like console application , web application etc.
It's giving different path while running from console application and web application. Here are the examples-:
While running in console application it prints as below:
D:\eclipse workspace mars\ResearchProject
Which is root directory of project in which java class files lies having System.getProperty("user.dir") line of code.
On the other hand if I run System.getProperty("user.dir") code from some servlet/ service/ business/ dac java class then it prints as below:
D:\eclipse-jee-mars-2-win32\eclipse
Which is root folder of eclipse.
How System.getProperty("user.dir") works for different kind of application?
Is there any way to get root directory of web application?

How System.getProperty("user.dir") works for different kind of application?
It works exactly the same way in all cases. It returns the "current directory" that was specified by the application that launched the application.
In the case of an interactive shell, it will be the current directory of the shell (or subshell) that launched the application.
In the case of a shell script or a native launcher, it will be the current directory set by the script or the launcher. (For example, when you start Tomcat using the "catalina.sh" script, the script sets the current directory.)
In the case of an application launched by Eclipse, it will be the current directory set by the launch configuration you are using. The default is the project directory, but you can override this in the launch config.
Is there any way to get root directory of web application?
I assume that you mean the root directory of the deployed webapp in the web container.
String path = request.getServletContext().getRealPath("/");
For more details:
Get the root directory name in Java web application if context and root names are different
However, there are various "traps" with accessing webapp files via the file system. One is that they may be clobbered at any time by a redeployment. A better approach is to access "files" in the deployed webapp using getResourceAsStream.

Related

Is there any way to configure tomcat servlet working directory?

When I'm running my web-application in tomcat and of course it's classes having tomcat working directory, because the JWM was started there. But is there any way to configure tomcat, as all classes of each deployed application, will have the application directory as their working directory?
You don't have to configure Tomcat working directory. You can get your application root directory from your servlet by calling :
String root = getServletContext().getRealPath("/");
Note that this folder is accessible from client browser.
If you want a working directory that is inaccessible from client, create a folder inside WEB-INF
String privateRoot = getServletContext().getRealPath("/WEB-INF");
You can change Tomcat's startup scripts. The tomcat process inherits whatever the current directory was for the startup script. 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.
You can look here on how to setup the working directory.

user.dir is incorrect while using tomcat web app in eclipse

I have a configuration project that few projects are using it.
All of my project under the same workingspace.
workingspace/configuration
workingspace/webapp1
workingspace/mongoDB
workingspace/model
mongoDB and the model project are using the configuration jar project and able to read the xml files using relative path ../configuration/conf/....xml
when using the
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
inside both of them, I'm getting the correct path (workspace)
while trying to do it from the web app servlet i'm getting the folder of eclipse.exe that causing some problems.
How can i fix the user.dir defalte path for the web app?
At runtime, e.g. when you're not running from inside eclipse, you probably want to work in a defined directory as well - I'd suggest to explicitly configure a specific directory. When you're running within an appserver, you might run as an unpriviledged user that doesn't have a home directory writeable at all (when the account is properly administered with minimal permissions). This differs from applications that are launched ad hoc. In fact, you probably can't assume that your application server runs as the same user as your standalone apps do.
Another alternative is to utilize the system's temp directory - if these are truly temporary files. This could be assumed writeable, or complaints if it isn't do make sense.

How to change path for web project in eclipse

I wrote the web application using Spring MVC. When I deploy the app to the server path is localhost:8080/projectName/. It is possible to remove projectName from path from eclipse? I found path in project properties but I cannot find the way how to change it.
I found that I have to change context root to : "/". I changed it but that has no effect.
Thank you for any help.
Regards,
Sebastian
I would break down your question into two parts :
A. Changes to the context root not taking any effect.
For changes to the context root to take effect, you must clean and republish you webapp on your server for the context root changes to get activated. To run "Clean" in context of the server from within eclipse,
Stop the Server
Window -> Show View -> Server -> Right click on your server configuration -> Clean. (Note: To "Clean" the server outside of eclipse, you need to go to the "webapps" directory of your server on your local filesystem and delete the .war file as well as the "project-name" folder which holds the exploded WAR file. )
Deploy your webapp to the server and restart the server.
B. Trying to run your webapp at the context root / - localhost:8080
From what it looks like, you are trying to run your web application at the "Root" of your application server. You haven't mentioned the application server that you are using, but let us for example assume that the server you are using is tomcat. For tomcat, to deploy an application which will run at localhost:8080/ you need to either deploy the exploded war under the "ROOT" directory at $CATALINA_HOME/webapps/ROOT , or name your war file to be root.war.
The $CATALINA_HOME/webapps/ROOT and $CATALINA_HOME/webapps/root.war are special keywords which tell tomcat to deploy the application at content root /.
If you are NOT using tomcat as your application server, then provide more details on the application server being used to see if I can help.

Locating created file with Java EE and Tomcat

I have created a dynamic web project, and use Apache Tomcat as a server.
In my servlet I'm creating a text file and want to reuse that in a JSP. However they are by default created in the installation folder of Eclipse when I do something as simple as the following:
File f = new file("test.txt").
I don't know why this happens. Is there a way to create the file in the WebContent directory as I want to make that file available for download in my JSP.
Java has a concept of the "current directory". When you start an application via Eclipse, this may indeed point to your installation directory. If you don't specify any path, a file will be created in this current directory. Hence the reason why your test.txt ends up there.
The WebContent directory is a something that is specific to Eclipse. Your code should not depend on putting anything there. You only start your application via Eclipse when you're developing it, not when you're deploying it to a live server.
The content of this directory will become the root of your .war, which is a well known location independent of how you start & deploy you app, BUT you still cannot depend on writing anything to this location at run-time. You might deploy your application as a packaged .war (likely for live deployments) or you may deploy your application unpackaged but then your application server may simply not pick up any changes done at run-time.
What you can do if you are sure your application only runs on a single server is writing the files to a well known location on your file system, such as /tmp, or /var/yourapp/files, etc. The code serving up those files can then pick them up from that location.
If you want to play it 100% safe according to the Java EE rules, you'd store your files on something like an FTP server that has a configurable address. Technically your war could be shipped between nodes on a cluster and requests could end up going to different machines, so depending on a local filesystem wouldn't work then.
Executing this statement this.getServletContext().getRealPath (""), you'll obtain the path where Tomcat WebServer is pointing at at runtime. You could add a folder "MyFolder" and call this statement:
new File(this.getServletContext().getRealPath ("") + "/MyFolder/test.txt");
Anyway, the default path looks something like:
...\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\<NameOfYourProject>
Note that when you create a new file, it won't appear in your immediate workspace (check the .metadata path), unless you change the runtime location tomcat should point at.

How can I add an external directory structure to my Java EE web application classpath

I am trying to set up my dev environment and we use ant script to build and deploy our application.
In my environment I have some configuration file (ADF model specific ) being created which are external to our web container (web context). Currently we zip that external directory and put it as a jar under WEB-INF/lib. But this is a time taking process for dev.
What I want is when my web context starts up it should have that external directory structure in class path.
I am sure it can be done but what is the best way to do so. I am using weblogic.
Add the full path as a JVM property eg.
-DmyPath=/var/somePath
Then reference it from your code
String path = System.getProperty("myPath");

Categories