Tenho este properties. D:/ide/workspace_luna/erp/erp-modelo/src/main/resources/APP_MSG_AUTH.properties. Mas logico que quando eu colocar no servidor vai dar erro.
Como faze para pegar o caminho absoluto até chegar neste arquivo, indepedente de onde esteja incluído ?
eu pego assim:
nomeArquivoFull = getApplicationPath() + “/configuracao.xml”;
public String getApplicationPath() {
String url = getClass().getResource(getClass().getSimpleName() + ".class").getPath();
File dir = new File(url).getParentFile();
String path;
if (dir.getPath().contains(".jar")) {
path = findJarParentPath(dir);
} else {
path = dir.getPath();
}
try {
path = URLDecoder.decode(path, "UTF-8");
} catch (UnsupportedEncodingException e) {
path = path.replace("%20", " ");
}
System.out.println("Path:" + path);
//verificar se site svwsistemas
int xs = path.indexOf("xxxsistemas");
int x = 0;
if (xs > -1) {
x = path.indexOf("xxx", (xs + 7));
} else {
x = path.indexOf("xxx");
}
path = path.substring(0, x);
return path;
}
private String findJarParentPath(File jarFile) {
while (jarFile.getPath().contains(".jar")) {
jarFile = jarFile.getParentFile();
}
return jarFile.getPath().substring(6);
}
Neste exemplo ai o nome da aplicação é xxx e o arquivo de configuração(configuracao.xml) fica junto com o arquivo .war na pasta webapps do tomcat.
E o teste onde tem xxxsistemas é porque o dominio em que esta hospedado pode conter o nome da aplicacao no inicio dele, em resumo creio que isto pode lhe dar uma ideia de como resolver teu problema.
Não consegui pegar a ideia.
Meu código está assim:
package br.eti.netsoft.erp.bundle;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class BundleErpMensagem extends BundleErp {
private static final String CAMINHO_PROPERTIES_MSG = "D:/ide/workspace_luna/erp/erp-modelo/src/main/resources/APP_MSG_AUTH.properties";
public BundleErpMensagem() {
iniciaPropriedade();
}
private Properties getPropertiesMensagem() throws IOException {
Properties props = new Properties();
FileInputStream file = new FileInputStream(CAMINHO_PROPERTIES_MSG);
props.load(file);
return props;
}
private void iniciaPropriedade() {
try {
setProp(getPropertiesMensagem());
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
A ideia é pegar o path da própria classe que esta sendo executada e a partir dele definir o local do teu arquivo de configurações para tua variável
CAMINHO_PROPERTIES_MSG.
neste caso que te passei a ideia foi cortar o final do path para ficar no mesmo nivel do arquivo xxx.war
Entendi. Eu só não consegui implementar.
Se for uma aplicação web :
crie uma pasta com nome classes dentro de WEB-INF e coloque seus properties nessa pasta.
Ex : WEB-INF/classes/exemplo.properties
E leia com
InputStream file = getClass().getClassLoader().getResourceAsStream("exemplo.properties");
Properties properties = new Properties();
properties.load(file);
String valor = properties.getProperty("chave");
Porque está em um subprojeto. É um JAR, o projeto aonde está o arquivo properties.