Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-23-2009, 09:53 AM   PM User | #1
satyambdvp
New to the CF scene

 
Join Date: Sep 2007
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
satyambdvp is an unknown quantity at this point
Angry Getting web context path into java class

Hi,

I created a web application in Netbeans 6.8.In that I created a dao which is a normal java class.In my project I have a folder named /xmls in that folder I kept my xml files.But I am not able to access those files.It is taking the default into tomcat's bin folder and there it is saying that no xml file is there.

When I used struts(servlets) in before projects it is no problems at all as we have the request.getServletContext(); method and from there we can use the getRelaPath("/xmls/names.xml"); methods to get file.

As I am now using the normal java class and the file should be accessible to any person on his system,i kept the files in my project,I want to get the file to access in the java class.How it is possible?

Can we get the file into a normal java class?Please help me it is urgent.

my code is as below

my file is names.xml which is in xmls folder which is in my project root folder.

File xml = new File("./xmls/names.xml");
JAXBContext jContext = JAXBContext.newInstance("com.coredb");
Unmarshaller unmarshall = jContext.createUnmarshaller();
Name name = (Name) unmarshall.unmarshal(xml);


it is showing the below error

[java.io.FileNotFoundException: C:\apache-tomcat-6.0.18\bin\.\xmls\names.xml (The system cannot find the path specified)]

it is taking the below path

C:\apache-tomcat-6.0.18\bin\.\xmls\names.xml
satyambdvp is offline   Reply With Quote
Old 12-23-2009, 06:20 PM   PM User | #2
brad211987
Regular Coder

 
brad211987's Avatar
 
Join Date: Sep 2005
Location: Ohio
Posts: 631
Thanks: 10
Thanked 50 Times in 50 Posts
brad211987 is an unknown quantity at this point
Is this file static? or does it change?

If it doesn't change, you could put it on your classpath instead and load it from there.

You could also get your current directory with a file object, here is a quick guess:

Code:
File file = new File(".");
String currentDirectoryPath = file.getCanonicalPath();
Then you could use relative paths from there. If the location of the file changes from one execution to the next, I would suggest a file chooser or a properties file that points to the location in a property.

Hope that gives you some options.
brad211987 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:43 AM.


Advertisement
Log in to turn off these ads.