Avatar of jaggernat
jaggernat

asked on 

path

hi guys

In my .properties file
i am reading some constants which equals to a location and trying to put
some files in that location

In my .properties file i have

REPORT_EDITION_DESTINATION=U:\VOB\Development\WebProject\WebContent\reportfileserver

I am reading the above line and putting the files in the above location U:\...
the above line works fine but i want to avoid giving the entire path.

i want some thing like
REPORT_EDITION_DESTINATION=\WebContent\reportfileserver OR
REPORT_EDITION_DESTINATION=\WebProject\WebContent\reportfileserver

so that i am not dependent on U:\ directory.

any ideas
thanks
Java

Avatar of undefined
Last Comment
CEHJ
Avatar of ksivananth
ksivananth
Flag of United States of America image

you have to tell the root folder or drive always, otherwise it will be considered from the current running folder.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

in that case you'd need to specify the base location somewhere, or perhaps use the users home directory. System.getProperty("user.home")
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

For maximum flexibility, add the path containing reportfileserver to the classpath, then you can load e.g.:

InputStream resourceStream = getClass().getResourceAsStream("/x.properties");
Avatar of jaggernat
jaggernat

ASKER

ok, wanted to ask onething.

in my java class how can i get the location of

U:\VOB\Development\WebProject\WebContent\reportfileserver.

with a java statement. I dont want to hard code the path.

instead i want something like getClass or getRoot.. or something which returns
U:\VOB\Development\WebProject\WebContent\reportfileserver.

any ideas?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

path = servletContext.getRealPath("/reportfileserver");
Avatar of jaggernat
jaggernat

ASKER

when i do something like
String path = servletContext.getRealPath("/reportfileserver");

it says servletContext  cannot be resolved. also the above line is in a java cllass which starts up when the server starts
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you need to get access to your ServletContext for your app
Avatar of jaggernat
jaggernat

ASKER

i cant really do that because when the server starts , the above code starts up which reads the path
("/reportfileserver or U:\VOB\Development\WebProject\WebContent\reportfileserver) .  ServletContext
comes into the picture once the server has started running , right.
SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Mick Barry
Mick Barry
Flag of Australia image

depends what your 'server' is, we don't have any details to give you an answer sorry.
To get your webapp's location it needs to be started (and it probably should be the first thing started)
Avatar of ksivananth
ksivananth
Flag of United States of America image

how is your web app loaded? if it is loaded from a servlet, the init method will work as I have suggested. All you have to is get the servlet context and get the value of the path before initializing your other classes. If not let knwo how your other classes loaded.
Avatar of jaggernat
jaggernat

ASKER

well, its a j2ee project using websphere, and it has a web project and an ejb project.
i can access the init method of the servlet in my web-project only after the server starts and a request is made by the user.

the problem is i am loading a java class which gets the path("/reportfileserver") on server start up. and this process is done before the application is loaded.

In my java class which gets the path, if i do something like

ServletContext sc = new ServletContext();
String path = sc.getRealPath("/reportfileserver");

it doesnt seem to work. says  ServletContext cannot be instantaited.

any ideas?

thanks
J
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Why do you need to know the location of the file/directory? If you use the method i gave you, you'll simply be able to load it
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>If you use the method i gave you, you'll simply be able to load it

but actually if there's a good reason you need to know the full path, that can be done as well
Avatar of jaggernat
jaggernat

ASKER

you mean

InputStream resourceStream = getClass().getResourceAsStream("/application.properties");
Avatar of jaggernat
jaggernat

ASKER

>>but actually if there's a good reason you need to know the full path, that can be done as well
how?
Avatar of jaggernat
jaggernat

ASKER


I am able to read the application.properties file.and application.properties file has

REPORT_EDITION_DESTINATION=U:\VOB\Development\Web\WebContent\reportfileserver

the above line works great. I am able to get a hold of the above U:\ directory, but i want to avoid the entire path and just say
REPORT_EDITION_DESTINATION=WebContent\reportfileserver. thats where i am stuck
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>but i want to avoid the entire path and just say

You can do that by using the method in my comment
Avatar of jaggernat
jaggernat

ASKER

ok, are you saying something like this

InputStream resourceStream = getClass().getResourceAsStream("/x.properties");

>>getResourceAsStream("/x.properties");
what is  x.properties ??

thanks
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Yes

>>what is  x.properties ??

Just an example of something to be read
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

In your case you need to add the full path of reportfileserver to the classpath to be able to read a properties file as above
Avatar of jaggernat
jaggernat

ASKER

>>>your case you need to add the full path of reportfileserver to the classpath

i want to avoid that. because each system has its own full path different from the other system
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of jaggernat
jaggernat

ASKER

>>No, the context is available as soon as server is started

i think you are right
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

:-)
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo