Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

class path for properites file 2

Status
Not open for further replies.

abyinsydney

Programmer
Feb 17, 2004
99
AU
Greetings fellow citizens
Can any one be kind enough to address my query.I 'm reading from my properties file below



Line no1 //String propFile = "\\..\\JRun\\servers\\default\\archiving\\writeprop.properties";
Line 2 String propFile = "writeprop.properties";
Line 3 //String propFile = "\\..\\..\\servers\\default\\archiving\\writeprop.properties";
System.out.println("we are here in this directory "+propFile);

FileInputStream inputfile;
inputfile = new FileInputStream(propFile);

Properties properties = new Properties();
properties.load(inputfile);
String dns=properties.getProperty("dns");
String value=properties.getProperty("value");
String dbusername=properties.getProperty("username");
String dbpassword=properties.getProperty("password");
System.out.println("Value for dns is "+dns);
System.out.println("Value for value is "+value);

//dns=propsettings.getProperty("email");
System.out.println(dns);
//value= "168.217.20.68";
Class.forName("org.gjt.mm.mysql.Driver");
String url="jdbc:mysql://"+value+"/"+dns+"";
conn = DriverManager.getConnection (url,dbusername,dbpassword);
Statement stat=conn.createStatement();



My application works fine when i use line number 1 cause that is hard coded but when
i use line two i get an error . Exception3: java.io.FileNotFoundException: writeprop.properties (
The system cannot find the file specified)
I've added the path to writeprop in the class path hnd exessingit

Can any body please let me know the right menthod to read from the properties file .I can read fro mthe file using line 1
but i would like to make it generic

please help

aby
 
In a standalone prgram, this is pretty much impossible - as you need to tell the program the precise path. Adding it to your CLASSPATH won't help. However, you could pass it in as a parameter in the main() method. Or if it is a servlet, have it as an init-param or a context init-param for the webapp ...

 
sedj

this is a jsp when im hard coding line 1 it works perfectly fine but does not when i use line 3

I'm prettty sure there is a solution to it

i'm still searching and not being able to reach to the solution

aby
 
If you have your context's web.xml file like this :

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "[URL unfurl="true"]http://java.sun.com/dtd/web-app_2_3.dtd">[/URL]

<web-app>
	<context-param>
		<param-value>directoryPath</param-name>
		<param-value>/usr/local/some/path/</param-value>
	</context-param>
	
</web-app>

and then you can get this path like this :

Code:
ServletContext sc = getServletConfig().getServletContext();
String path = sc.getInitParameter("directoryPath");

This is about as non-hardcoded as you will get it ...
 
sedj
This would mean that i simply have to create an web.xml file and insert my path i.e
String propFile = "\\..\\JRun\\servers\\default\\archiving\\writeprop.properties";
in place of directory structure and write
ServletContext sc = getServletConfig().getServletContext();
String path = sc.getInitParameter("directoryPath"); in my jsp

i will try the same in office tommorrow lets see wheatehr it works

aby
 
Sedj

Below are the contents of web.xml adn here is the code i've embedded in my jsp code


ServletContext sc = getServletConfig().getServletContext();
String propFile = sc.getInitParameter("C\JRun\servers\default\archiving\writeprop.properties");
<web-app>

xml contents
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"
<web-app>
<context-param>
<param-value>C\JRun\servers\default\archiving\writeprop.properties</param-name>

</context-param>

</web-app>

My application seems to be hanged up .Ami adopting the right approach as suggested by you

aby
 
Aby, you have not followed the sugggestion I made syntactically, try this :

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "[URL unfurl="true"]http://java.sun.com/dtd/web-app_2_3.dtd">[/URL]

<web-app>
    <context-param>
        <param-value>directoryPath</param-name>
        <param-value>C:/JRun/servers/default/archiving/writeprop.properties</param-value>
    </context-param>
    
</web-app>
 
If you put your property file in a directory within your class path, then you can use ResoureBundle instead, and save you in locating the correct path.

For example: say you have a package "mypackage" for your application. Then just put your property file, say "writeprop.properties", in the mypackage package. The properties file can then be load/read by a ResourceBundle with one line of code:

Code:
  java.util.ResourceBundle myproperties = ResourceBundle.getBundle("mypackage.writeprop");
  String dns=myproperties.getString("dns");
  String value=myproperties.getString("value");
  String dbusername=myproperties.getString("username");
  String dbpassword=myproperties.getString("password");
 
greetings Byam
i've done exactly what you have said but it seems like its not picking up from the class path
thanx
aby
 
Greetings sedj
i'll try your suggestion in office tommorrow
thanx
aby
 
sedj
I followed your code but the application seems to stop processsing.the lgin page is not getting authenticated it.it simply hangs up

could you throw some ight on it

aby
 
sedj
on clicking on the submit button after putting the user name and password it goes to th eauthentication page it gives me the following error cannt find server ,seems lke there is something wrong in the class path

aby
 
greetings bryam
i've tried your code it gives me the following exception

Exception3: java.util.MissingResourceException: Can't find bundle for base name writeprop.properties, locale en_AU<BR>
please help buddy

aby
 
Greetings fellow citizens

Finally i'm able to write a java programme which reads from a properties file and displays the result
The only hitch being i want the string to be displayed in my jsp

Cana any one let me know how to do the same.

My Approach isi've included the class in my jso.Now the question is how do i call string name at the end of this programme in my jsp.
Any suggestions people

thanx
aby

public class PropertiesReader
{

private Locale locale = null;
private String propertiesFile = null;
private ResourceBundle resourceBundle = null;

public PropertiesReader (String propertiesFile, Locale locale)
{
this.setLocale (locale);
this.setPropertiesFile (propertiesFile);
this.setResourceBundle ( ResourceBundle.getBundle(this.getPropertiesFile(), this.getLocale()) );
}

public PropertiesReader (String propertiesFile)
{
this (propertiesFile, Locale.getDefault());
}

public PropertiesReader ()
{}

public void setLocale (Locale locale)
{
this.locale = locale;
}

public Locale getLocale ()
{
return this.locale;
}

public void setPropertiesFile (String propertiesFile)
{
this.propertiesFile = propertiesFile;
}

public String getPropertiesFile ()
{
return this.propertiesFile;
}

public void setResourceBundle (ResourceBundle resourceBundle)
{
this.resourceBundle = resourceBundle;
}

public ResourceBundle getResourceBundle ()
{
return this.resourceBundle;
}

public String getProperty (String propertyName)
{
String propertyValue = this.getResourceBundle().getString(propertyName);
return propertyValue;
}


public static void main (String[] args)
{
//create new PropertiesReader using a file called testfile.properties that must be located in the current directory
//testfile.properties must have a property named name in it for this example to work
//this example uses the default locale for the system [Locale.getDefault()]
PropertiesReader pr = new PropertiesReader("testfile");
String name = pr.getProperty ("name");
System.out.println (name);
}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top