How to get deploy root directory in servlet based project? - servlets

I am trying to get deploy root directory of my servlet based project from java. I am using the following lines of codes to get the path details.
Type 1:
File directory = new File (".");
try {
System.out.println ("Current directory's canonical path: "
+ directory.getCanonicalPath());
System.out.println ("Current directory's absolute path: "
+ directory.getAbsolutePath());
}catch(Exception e) {
System.out.println("Exceptione is ="+e.getMessage());
}
Type 2:
String currentDir = System.getProperty("user.dir");
System.out.println("Current dir using System:" +currentDir);
While executing the above codes from main class i am getting user directory. When i executes from server side, gets as, "Current dir using System:D:\Apache Tomcat 6.0.16\bin". But my project is located in D:\Apache Tomcat 6.0.16\wepapps\SampleStructs.
Please give me any suggestions for this and help me out of this.

First of all, the main cause of your problem is the difference between current working directory and the location of your executable. You should know that current working directory in Linux is not the directory where the executable is, but instead the current directory where the program was started from.
As an example, let's say you have a program current which prints out the current directory and it is located in /home/user/scripts/.
If you do this:
cd /home/user/scripts
./current
It will print out: /home/user/scripts/
But, if you do this:
cd /home/user/
scripts/current
The output will be: /home/user/
As to the possible solutions, some of them I found useful are:
Refer to your project resources relative to the classpath, see ClassLoader.getResourceAsStream() for more info
Refer to your configuration resources, like properties files and such, relative to the user home directory.
Put all other locations, such as media directory path and similar to the configuration file from the point above.
If all other options are not available or practical use getClass().getProtectionDomain().getCodeSource().getLocation().getPath(). See more about this approach and some possible issues here: How to get the path of a running JAR file?

It because when you execute from main class everything is fine, but this code runs on server it looks into current directory and current the directory structure is Apache 'bin' from where you have started the server(run.bat).

you can use this code
String absolutePath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("/"));
this code is working before for me!
it will return the full path of folder in windows or linux.

There are different context we are talking about here.
1. Running the application in standalone mode.
2. Running the application in container on server side.
In #1, The application is run from the directory it is invoked.
But #2 case, the application is run relative to the container, so you see the location of server directory. This also shields the application code.

Related

The output directory is under your source tree in meteor

how do I solve this
The output directory is under your source tree
warning:the output directory is under your source tree
your generated files may get interpreted as source code
consider building into a different directory instead meteor build ../output
I apologize for my ignorance on this matter, I am new in the world of programming and meteor. about my case. 1. I have a folder on Desk called “Findme” where I have the structure of my project (the code and everything which forms the application which works) 2. Then through the console I access that directory findme and then run the command meteor build/Desktop/MyApp --server = https: //findme.com, and start downloading. 3. But inside the console I also get the message indicated before and when it is finished, and I check the folder MyApp, it is empty. 4. And when I check the Findme folder it has created a folder named ~ and inside displays a file called Desktop/MyApp but it also doesn’t have any useful files, only winrar and nothing useful. 5. I am trying to generate the apk, could you please let me know what I might be doing wrong? Is there another way to generate the apk? I would appreciate it if you could help me!
Your should specify a path for where to build your application that is outside of your project directory.
Otherwise this can lead to problems with Meteor's file watcher and as your error already pointed out:
your generated files may get interpreted as source code consider
building into a different directory instead
So if your command uses a relative path, as used in meteor build ../output then it is important to call this command at the most upper project folder.
Consider the following project structure:
/myapp
/client
/import
/server
If you call meteor build ../output from within /myapp it will generate the output folder as expected outside of the project:
/output
/myapp
/client
/import
/server
However if you call it from within a subfolder, say /myapp/imports it may generate the output within the project like so:
/myapp
/client
/import
/output
/server
So keep this in mind when building your app.
Further readings:
https://guide.meteor.com/deployment.html#custom-deployment
https://docs.meteor.com/commandline.html#meteorbuild

Uploads in Symfony's var directory not accessible

I'm trying to upload files into 'var' directory (successfully). But when I want to take this one file from 'var' I only can observe No route found for "GET /var/uploads/images/....
Structure:
- app
- bin
- src
- var
- cache
- logs
- uploads
- images
- vendor
- web
For saving my files I'm using: '%kernel.project_dir%/var/uploads/images'
For order to take file: '/var/uploads/images/' . $fileName;
Where're my mistake?
P.S. Moreover I try to use volume (I'm using docker) in my docker-compose file like this: - ./data/cabinet/uploads/images:/data/www/cabinet/var/uploads/images
And, unfortunately, no one file didn't copy to this directory. What's wrong?
Thank you!
Since the document root is pointing to the web directory, there's no possibility for path ../var to be accessible. Either store uploads inside the web directory or create a symlink inside it pointing to the var/data/uploads:
// since I don't know your path tree inside the container
// I assume whole application is installed inside /var/www/html
// and this is the default working directory
docker-compose exec YOUR_CONTAINER_NAME \
ln -rs /var/www/html/var/data web/var
Bear in mind that if you are using Apache webserver, the FollowSymLinks option has to be enabled, but most probably is.
As for the second question, most likely you mapped an invalid directory, I guess this should be:
// yet again I assume whole application is inside the /var/www/html
./data/cabinet/uploads/images:/var/www/html/data/cabinet/uploads/images
Is this some kind of production anyway? If it is not and this is merely a work-in-progress, then you rather should map your whole local directory as container volume. This is how it's usually done in dev mode, so the mapping would be:
.:/var/www/html

Meteor reading a file in public folder using readFileSync not working when deployed

I'm trying to read a .json file in /public folder. Relative path I'm using is '../../../../../public/data.json' and it works when I run with meteor run.
However, when I deploy to meteor subdomain by running meteor deploy MyApp.meteor.com, it crashes and the logs say
Error: ENOENT, no such file or directory '../../../../../public/data.json'
I tried using 'data.json', 'public/data.json', etc. but I couldn't get it to work. What am I missing here?
Put the data in a "private" directory off of your app root instead of "public" (assuming that you're only going to read it on the server, which is what you seem to be doing). When you want the file (regardless of if you are deployed or not), it is at the path "assets/app/".
For example, if your app had the directory structure:
myApp.css
myApp.js
myApp.html
server/
serverCode.js
private/
data.json
You could use something like "peerlibrary:fs", which exposes fiber aware fs sync functions, and do the following in "serverCode.js"
var data = JSON.parse(fs.readFileSync("assets/app/data.json"));
If you put the file data.json in the root of the public folder you can just use the path "/data.json"

What is the Meteor server-side path to /public?

On the Meteor client-side, I know that files in the project's public directory are referenced at '/'.
How are they referenced on the server-side?
I am trying to get a directory listing with fs.readdir, but I don't know how to construct the path to get to the server side equivalent of the client side '/images/gallery'.
Any advice?
The accepted "./public/" answer does not work for me in Meteor 1.1.
However, Meteor supplies the server path via the meteor_bootstrap.serverDir variable, so to get the public folder path I use the following line:
path.join(__meteor_bootstrap__.serverDir, "../web.browser/app");
This works on my local Windows machine and on meteor.com.
Note that this is the "running" version of your public folder, so - at least in development, I haven't checked this part in production - it's actually a merge of your development "public" folder and all of your client-side JS files. If you have a "config" folder in your project, and a "config" folder in your public directory, the "running" path will include the contents of both.
there's an upgrade since the 0.6.5 version of meteor, main.js now chdirs into programs/server in your bundle. So the content of the public directory is here : ../client/app/
the detail on github
I got the absolute path for Meteor project directory using below line of code.
var absPath = process.env.PWD;
I have used this with Meteor 1.4.3.2 and it works perfectly.
When I use the fs-module I just use './public' for my public folder, works fine on my local install.
And then I set it to whatever's correct at the production server using environment vars.
Edit (an example):
This method will return all .HTML files from the public folder:
getHtmlFilesInPublicFolder: function() {
var files = fs.readdirSync('./public/');
var cleanedUpFiles = _(files).reject( function(fileName) {
return fileName.indexOf('.html') < 0;
});
return cleanedUpFiles;
}
If you are using nodes file system library on the client then you are going to be working with your local file system structure and you're files will be referenced by the local path to where ever they reside on your local disk.
For example.. if your project is located at /home/bob/meteor_projects/project1 then your files are located at /home/bob/meteor_projects/project1/public

Problem in creating text file(Qt application) in installation directory (using CMake to install)

I am new to programming. I am creating a small word jumble game to practice qt programming. In this application I am creating a text file (score.txt) to keep score of player. I have done this by:
QFile scoreFile("score.txt");
if (QFile::exists("score.txt"))
{
scoreFile.open(QIODevice::ReadWrite | QIODevice::Text)
// and update the score.
}
else
{
scoreFile.open(QIODevice::ReadWrite | QIODevice::Text);//create score file
//and write the score to it.
}
this code is working good here. Now I am using CMake to build and install generated binary (I am working on Ubuntu) using this code:
#set project name, version and build code here.
install(TARGETS wordJumbleGame DESTINATION bin)
I build project in /home/myname/project/build/
My source code is in /home/myname/project/src/
CMakeLists.txt is in /home/myname/project/CMakeLists.txt
I installed program using make install.
Till here all things working fine. But now problem is that when I run this program (I run it from terminal giving command wordJumbleGame) It creates score.txt in /home/myname/project/build directory. It is not being created in installation dir bin.
So please help me out, what am I doing wrong. And please also tell me how do I make my program to appear in application->game lists so I can run it from there not from command prompt.
Unless you prefix it with a slash (on unix) or a drive path (Windows), QFile's constructor parameter is a relative path - relative to the current working directory. score.txt is created in the build/ directory because that's probably where you're executing the binary from.
You can't store score.txt in the /usr/bin directory because, typically, you can't write there without root privileges.
What you want to do is get a path to some directory where you can store your score.txt file. To do that, you can use the QDesktopServices class. That will give you directory information on a per-user basis. Here's an example:
#include <QDesktopServices>
// this would go in main(), probably
QCoreApplication::setApplicationName( "word jumble game" );
// now when you want to read/write the scores file:
QString dataPath = QDesktopService::storageLocation( QDesktopService::DataLocation );
QFile scoreFile( dataPath + "score.txt" );
// on my system, this produces: "/home/adam/.local/share/data/word jumble game/score.txt"
// it will produce something similar for Windows and Mac too
You should set your appication name via QCoreApplication::setApplicationName before getting path information to keep the user data directory nice and organised.
As for getting your application in the games list, you'll need to create a menu entry that follows the freedesktop.org spec. I can't help you more with that, but this is a good starting point. Somebody else might have more info for you.
You need to create a .desktop entry file and install it using xdg-desktop-menu install. Here are two resources for you: freedesktop.org menu spec and adding .desktop files using CMake

Resources