I WebSphere를 사용하는 자바 EE이 오류 메시지가 있어요 :내 속성 파일을로드하는 방법?
Exception reading propertiesfile in init java.lang.NullPointerException
java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java(Inlined Compiled Code))
at java.io.InputStreamReader.<init>(InputStreamReader.java(Inlined Compiled Code))
at java.util.Properties.load(Properties.java(Compiled Code))
at se.prv.register.admin.util.MessageHandler.loadDictionary(MessageHandler.java:90)
at se.prv.register.admin.util.MessageHandler.<clinit>(MessageHandler.java:15)
이 내 메시지가 같은 디렉토리에 messages.properties 파일 넣었습니다
package se.prv.pandora.arendeprocess.util;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
public class MessageHandler extends AbstractTextHandler{
private static Properties dictionary;
private final static String dictionaryFileName="messages.properties";
private final static String className="se.prv.pandora.arendeprocess.util.MessageHandler";
private static Logger logger = Logger.getLogger(MessageHandler.class);
static {
loadDictionary();
}
public static void main(String[] args) {
String str = getParameterizedMessageTest("TEST",new String[]{"PETER","GÖRAN"});
System.out.println(str);
}
public static String getMessage(String key){
return getMessage(key,true);
}
public static String getMessage(String key,boolean useReference){
logger.debug("!!!! getMessage "+ key);
if (key==null || key.length()==0){
throw new RuntimeException("GetMessage without key");
}
if (dictionary==null){
loadDictionary();
}
if (useReference){
return getFullMessage(getMessage(dictionary,key));
}
else {
//String str = getMessage(dictionary,key);
//logger.debug(str);
String str2 = getCoreMessage(getMessage(dictionary,key));
//logger.debug(str2);
return str2;
}
}
public static String getFirstPartOfMessage(String key){
if (key==null || key.length()==0){
throw new RuntimeException("GetMessage without key");
}
if (dictionary==null){
loadDictionary();
}
String msg = getMessage(dictionary,key);
int pos = msg.indexOf('$');
if (pos==-1){
return msg;
}
else {
return msg.substring(0,pos);
}
}
public static String getLastPartOfMessage(String key){
if (key==null || key.length()==0){
throw new RuntimeException("GetMessage without key");
}
if (dictionary==null){
loadDictionary();
}
String msg = getMessage(dictionary,key);
int pos = msg.lastIndexOf('$');
if (pos==-1){
return msg;
}
else {
return msg.substring(pos+1);
}
}
public static String getParameterizedMessage(String key, String [] params){
if (dictionary==null){
loadDictionary();
}
return getParameterizedMessage(dictionary,key,params);
}
private static void loadDictionary(){
String fileName = getPropertiesPath()+dictionaryFileName;
//String fileName = "se/prv/register/admin/dao/sql-map-config.xml";
try {
dictionary=new Properties();
//InputStream fileInput = Class.forName("se.prv.register.admin.util.MessageHandler").getClassLoader().getResourceAsStream(fileName);
InputStream fileInput = Class.forName(className).getClassLoader().getResourceAsStream(fileName);
dictionary.load(fileInput);
fileInput.close();
}
catch(Exception e) {
System.err.println("Exception reading propertiesfile in init "+e);
e.printStackTrace();
dictionary=null;
}
}
private static String getCoreMessage(String str){
StringBuffer buff = new StringBuffer();
boolean doCopy=true;
for (int i=0;i<str.length();i++){
if (str.charAt(i)=='$'){
doCopy=!doCopy;
continue;
}
if (doCopy){
buff.append(str.charAt(i));
}
}
return buff.toString();
}
private static String getFullMessage(String str){
int pos = str.indexOf('$');
if (pos==-1){
return str;
}
StringBuffer buff = new StringBuffer(str);
int originalLength = buff.length();
for (int i=pos+1;i<buff.length();i++){
if (buff.charAt(i)== '$'){
String key = buff.substring(pos+1,i).trim();
String msg = getMessage(dictionary,key);
buff.replace(pos,i+1,msg);
if (buff.length()!=originalLength){
i += (buff.length()-originalLength);
originalLength=buff.length();
}
pos = buff.indexOf("$",i+1);
if (pos==-1){
break;
}
else {
i = pos+1;
}
}
}
return buff.toString();
}
// private static void loadDictionary(){
// loadDictionary(dictionary,dictionaryFileName,className);
// }
}
package se.prv.register.admin.util;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
import se.prv.framework.general.PRVWebSphere;
import se.prv.framework.util.Strings;
public abstract class AbstractTextHandler {
private static final String propertiesPath="//se//prv//register//properties//";
protected static Logger logger = Logger.getLogger(AbstractTextHandler.class);
// static {
// loadProperties();
// }
protected static String getPropertiesPath(){
return propertiesPath;
}
protected static String getMessage(Properties dictionary,String key){
if (dictionary==null){
return "ERROR";
}
String msg = dictionary.getProperty(key);
//System.out.println("KEY="+key);
if (msg==null){
return "?!Meddelande " +key + " saknas!?";
}
return msg;
}
protected static String getParameterizedMessage(Properties dictionary,String key,String []params){
if (dictionary==null){
return "ERROR";
}
String msg = dictionary.getProperty(key);
if (msg==null){
return "?!Meddelande " +key + " saknas!?";
}
if (params==null){
return msg;
}
StringBuffer buff = new StringBuffer(msg);
for (int i=0;i<params.length;i++){
String placeHolder = "<<"+(i+1)+">>";
if (buff.indexOf(placeHolder)!=-1){
replace(buff,placeHolder,params[i]);
}
else {
remove(buff,placeHolder);
}
}
return buff.toString();
}
public static String getParameterizedMessageTest(String key,String []params){
String msg = "HEJ <<1>>!HUR MÅR DU? HEJ DÅ FRÅN <<2>>";
if (msg==null){
return "?!Meddelande saknas!?";
}
if (params==null){
return msg;
}
StringBuffer buff = new StringBuffer(msg);
for (int i=0;i<params.length;i++){
String placeHolder = "<<"+(i+1)+">>";
if (buff.indexOf(placeHolder)!=-1){
replace(buff,placeHolder,params[i]);
}
else {
remove(buff,placeHolder);
}
}
return buff.toString();
}
private static void replace(StringBuffer buff,String placeHolder,String param){
int pos = buff.indexOf(placeHolder);
if (pos==-1){
return;
}
buff.replace(pos,pos+placeHolder.length(),param);
}
private static void remove(StringBuffer buff,String placeHolder){
int pos = buff.indexOf(placeHolder);
if (pos==-1){
return;
}
buff.replace(pos,pos+placeHolder.length(),placeHolder);
}
protected static void loadDictionary(Properties dictionary,String fileName,String className){
//String fileName = "se/prv/register/admin/dao/sql-map-config.xml";
try {
dictionary=new Properties();
InputStream fileInput = Class.forName("se.prv.register.admin.util.AbstractTextHandler").getClassLoader().getResourceAsStream(fileName);
dictionary.load(fileInput);
fileInput.close();
}
catch(Exception e) {
logger.error("Exception reading propertiesfile in init "+e);
e.printStackTrace();
dictionary=null;
}
}
}
내 코드입니다 log4j.properties 파일을 작성해야합니다. 내가 도대체 뭘 잘못하고있는 겁니까?
는
Eclipse 기반의 IBM RAd 내부에서 프로그램을 실행합니다. 디스크에서 속성 파일을로드하려고 시도하지만 파일 이름이 실제 파일을 가리키는지를 검사 할 때에도 여전히 예외가 발생합니다. –
디스크에서 파일을로드하려는 경우 'dictionary.load'를 사용하여 Reader (예 : FileReader)를 전달해야합니다. 'getResourceAsStream()'메소드는 자원 내의 경로를 찾는다. '/'를 사용하여 경로를 앞에 넣으려고 했습니까? 질문에 인용 된 코드에는 경로가'/'로 시작하지 않는다는 인상을주는 주석이 있습니다. 이 경우 경로는 클래스 위치와 관련하여 해석됩니다. [cont.] – Attila
[cont] 예 : 클래스가 "se/prv/register/admin/dao /"폴더에 있고 리소스 경로를 "se/prv/register/admin/dao/sql-map-config.xml"으로 지정하면 클래스 로더는 "se/prv/register/admin/dao/se/prv/register/admin/dao/sql-map-config.xml"에서 파일을 찾으십시오 (디렉토리 계층 구조의 중복에주의하십시오). 경로를 "/se/prv/register/admin/dao/sql-map-config.xml"로 지정하면 로더는 "se/prv/register/admin/dao/sql-map"에서 파일을 찾습니다. -config.xml " – Attila