wood burning stoves 2.0*
The moose likes Spring and the fly likes Problem not loading a properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Problem not loading a properties file" Watch "Problem not loading a properties file" New topic
Author

Problem not loading a properties file

Ryan Raina
Greenhorn

Joined: Jun 16, 2010
Posts: 28
Hi everybody
I am developing an application which reads a file known as key.properties .
When i run the application on tomcat6 and jboss 4.2.3 it's working fine
But when I run the application on jboss 5.1.0 it's not working.
It is saying that keys .properties doen't exist but it is present in the class path..

I am having a servlet WebTest which calls a keysutil class for other operations
my error is

15:40:39,698 ERROR [STDERR] java.io.FileNotFoundException: class path resource [keys.properties] cannot be resolved to absolute file path because it does not reside in the file system: vfsfile:/C:/jboss-5.1.0.GA/jboss-5.1.0.GA/server/default/deploy/estw.war/WEB-INF/classes/keys.properties


********************************************************************
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public final class WebTest extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
/*
* System.out.println(getClass().getResource("/"));
* System.out.println(getClass().getResource("/").getPath());
* System.out.println(getClass().getResource("/").getFile());
* System.out.println(request.getRequestURI());
* System.out.println(request.getRealPath("/"));
* System.out.println(request.getPathInfo());
* System.out.println(getClass().getResource("").getPath()); String name
* = this.getClass().getName().replace('.', '/');
* System.out.println(this.getClass().getResource("/").toString());
*/
System.out.println("calling path method");

KeysUtil util = new KeysUtil();
try {
createDefaultFolder("xyz");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("ending path method");
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
String path = new java.io.File(".").getCanonicalPath();
System.out.println("************************");
System.out.println(path);
writer.println("<html><head><title>Simple Web Application</title></head>");
writer.println("<body><h1>A Simple Web Application</h1>");
writer.println("Here's some text...<br>...and a bit more text");
writer.println("</body></html>");
}

private void createDefaultFolder(String vendorName) throws Exception {
KeysUtil util = new KeysUtil();
util.createDefaultKeyFolder(vendorName);
}

}


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.springframework.core.io.ClassPathResource;

public class KeysUtil {
private File classBaseDirectory;
private Properties keyProps = null;

public KeysUtil() {
this.classBaseDirectory = returnClassPath();
}

private File returnClassPath() {
System.out.println("In returnClassPath method");
String tempPath = getClass().getResource("/").getPath();//this is returning / only
if (tempPath != null && tempPath.length() != 0) {
tempPath = cleanFilePath(tempPath);
}
return new File(tempPath);
}

public static String cleanFilePath(String filepath) {
return filepath.replaceAll("%20", " ");
}

public boolean checkDefaultFilesExists() throws Exception {
ClassPathResource res = null;
File defaultKeyPropertyFile = null;
res = new ClassPathResource("keys.properties");//here i am calling key.properties
if (res.exists()) {
defaultKeyPropertyFile = res.getFile();//here defaultKeyPropertyFile is coming to null
System.out.println(defaultKeyPropertyFile);
setKeyProps(loadPropertyFile(defaultKeyPropertyFile));
if (keyProps != null) {
String keyFile = keyProps.getProperty("keystore.filename");
System.out.println("keyFile" + keyFile);
if (keyFile != null) {
res = new ClassPathResource(keyFile);
if (res.exists()) {
return true;
}
}
}
}
return false;
}

public void createDefaultKeyFolder(String vendorName) throws Exception {
if (checkDefaultFilesExists()) {
String path = createVendorFolder(replaceSpace(vendorName));
File vendorFolder = new File(path);
if (vendorFolder.exists()) {
File defaultFolder = new File(vendorFolder, "Default_Keys");
if (!defaultFolder.exists()) {
defaultFolder.mkdir();
// allowGroupAccessToFolder(defaultFolder.getName());
}
// transferDefaultKeyFiles(defaultFolder.getAbsolutePath());
}
}
}

private Properties loadPropertyFile(File file) {
Properties keyProps = new Properties();
InputStream keyStream;

if (file != null && file.exists()) {
try {
keyStream = new FileInputStream(file);
keyProps.load(keyStream);
keyStream.close();
} catch (FileNotFoundException e) {
System.out.println("Key property file not found");
} catch (IOException e) {
e.printStackTrace();
}
}
return keyProps;
}

public void setKeyProps(Properties keyProps) {
this.keyProps = keyProps;
System.out.println("In setKeyProps method ...value of keyProps is "
+ keyProps);
}

public String replaceSpace(String file) {
return file.replaceAll(" ", "_");
}

public String createVendorFolder(String vendorName) throws Exception {
File tempBaseDirectory = null;
this.classBaseDirectory = returnClassPath();
if (this.classBaseDirectory.exists()) {
File classFolder = new File(this.classBaseDirectory.getPath());
String vendor = replaceSpace(vendorName);
tempBaseDirectory = new File(classFolder, vendor);
if (!tempBaseDirectory.exists()) {
tempBaseDirectory.mkdir();
// allowGroupAccessToFolder(tempBaseDirectory.getName());
} else {
System.out.println("tempBaseDirectory already created");
}
} else {
System.out.println("classBaseDirectory does not exist.");
}
return tempBaseDirectory.getAbsolutePath();
}
}



this code is working fine with jboss4.2.3 and tomcat 6

But it's not working in jboss 5.1.0


here is my keys.properties
keystore.password=
keystore.key.alias=
keystore.filename=replace_with_real_private_key.p12


her is my web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>Some dummy name</servlet-name>
<servlet-class>WebTest</servlet-class>

</servlet>

<servlet-mapping>
<servlet-name>Some dummy name</servlet-name>
<url-pattern>/start-web-test</url-pattern>
</servlet-mapping>

</web-app>

it can now be easily set up in any local.
help wud be nice ...thanks in advance
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

So are you saying that the property file is present at this location
C:/jboss-5.1.0.GA/jboss-5.1.0.GA/server/default/deploy/estw.war/WEB-INF/classes/keys.properties

and Spring is still not picking it up?


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Ryan Raina
Greenhorn

Joined: Jun 16, 2010
Posts: 28
yes the spring is not able to pick it up....It's working for tomcat 6 and jboss 4.2.3 ...but not for jboss 5.1.0...basically i am doing url deployment i.e editing profile .xml and giving a url to the deploy artifact..
spring documentation says that classpathresourse doesn't work if element you are trying to search exists in jar or war...but i am not giving any war file to jboss..i am doing url deployment..that means it should work because it is in exploded form....Also it should not work for jboss4.2.3 but it is working there....I am doing url deployment in case of jboss 4.2.3 also...any suggestions...Thanks in advance
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Seems like Jboss 5 has a problem --> http://forum.springsource.org/showthread.php?76234-Jboss-5-vfsfile-protocol-problem.

cannot be resolved to absolute file path because it does not reside in the file system Jboss is what I searched for in google.
Ryan Raina
Greenhorn

Joined: Jun 16, 2010
Posts: 28
ok...he has this problem because classpathresource doesn't work with jar files i.e if you are searching the file in a jar or war file using classpathresource of spring it cannot be found...spring documentation says that...but if you do exploded deployment it should be able to find it right...the reason I say it because in case of jboss 4.2.3 it's working in exploded war....do you have ant idea how can i do exploded deployment using jboss 5.1.0 ..of course one way would be through admin console of jboss ...but any other way....like editing an xml in jboss....any idea?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem not loading a properties file