My development environment is windows, using JSP,Apache server 5.5. I developed an application with the help of geolitecity provided by MaxMind. I have uploaded geolitecity.dat into my server in the same folder of my website(I dont know its the correct procedure, I am doing it first time).And I used
String systemPath=new java.io.File(".").getCanonicalPath();
to get the current directory path, so that I can read from it. But I am not getting the full path. am only getting upto tomcat5.5.3\bin. Is it possible to read the file with this path? I dont have much knowledge in linux.
In Servlet/JSP:
String path = getServletContext().getRealPath("/yourfilename.txt");
This will give you complete path of given file name.
Note: It will work when you will deploy it in tomcat and run from out side eclipse. As eclipse has its own internal structure when it deploy the web application [if not changed].
Related
I am writing a Quarkus application which reads data over http. In my application.properties file, I have this line:
my.resource=http://path/to/file
Every time I run the app, it has to download the file so I created a smaller version of the file locally for developing purpose. The problem is that I don't know how to put it in the properties file.
Ideally, I want something like this:
my.resource=http://path/to/file
%dev.my.resource=file://${project-dir}/sample_data/file
And I have to use the absolute path because I used new URI(resource).toURL() method which requires an absolute URI.
Thanks in advance.
Application properties is something that is used when your application is deployed to adopt your application to the target environment, does the user of the deployed application know anything about project directory? Project directory is something that makes sense when you are developing your application. having said that using project directory in that file does not make sense at all.
I have been testing my tomcat 8 setup on debian 9. Everything seems fine until I try to deploy a helloServlet WAR file through tomcat 8 manager. I created the very simple example program following the instruction here.
I use tomcat manager to upload the HellowServlet.war from my working directory. Then I can access it though http://localhost:8080/HelloServlet/sayhello
The program works just fine. However, no matter how I search (/etc/tomecat8 /usr/share/tomcat*), I just can not find the war file or any components inside of it. I really want to know the exact location of my application file. Anyone may tell me where the application files are stored by the tomcat 8 mananger?
More update:
I just tried to upload the same war file again, and I got following message from tomcat
FAIL - War file "HelloServlet.war" already exists on server
That means the war file is sitting somewhere on the server, but the path is unknown to me.
Found it.... it's inside /var/lib/tomcat8/webapps
However, I can not find any configuration file in tomcat 8 describing that it uses /var/lib/tomcat8 as one of its working directory.
I have written a very small java code on Eclipse which will automate a small process of logging into a web system. The employees of my company use this web system to connect to office network if they are working from home.
I have converted my java project on Eclipse into an exe file, my intention is to log into that system by just a double click on the exe file.I have parameterized the userID and password and have stored it in an excel file on my local machine.
The problem am having is, My exe file will not run in any other systems except mine as my code is referring to the excel file(which has userID and password) path on my local machine. I would greatly appreciate the developers on this forum who could help me out to come up with a solution for this problem.
What about looking for the excel file in a well defined folder like C:\Users\\my-tool\credentials.xls. Or maybe look for it in the same dir as the executable?
You can get the path of the home folder of the current user with this command:
String homeDir = System.getProperty("user.home");
With that you cann assemble your custom lookup path:
Path xlsPath = Paths.get(System.getProperty("user.home"))
.resolve("my-tool")
.resolve("credentials.xls");
I wrote a desktop java application with a class (say ClassA) that reads the content of a file, processes it and returns some results. The filename was specified relative to the project using
File input = new File("config.xml");
Now, I want to upgrade the project into a web project. I wrote a servlet which calls the same java class (i.e. ClassA) for reading the content of the same file but this time I get an error message saying file not found.
How do I refactor my code so that both the desktop and the web versions run smoothly.
Just copy the file config.xml into the proper location on the web server e.g. public_html/www/
The "working directory" of a web application is different - depends on the configuration of the web server you are deploying it to.
If you read a file without specifying a path, it is read from the current dir, which you can access with System.getProperty("user.dir");
So you can try to find out what value is returned by System.getProperty("user.dir") in your web app and place the file there.
But this may differ depending on the environment and servlet server (Tomcat etc.) and may be not a reliable solution.
Another way is to change your code, so it reads the file from the user.home directory and place the file there.
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.