I had a similar problem and in this case I recommend that you use this.getClass().getResourceAsStream("")
to handle it smoothly.
Example
Stack Config.java:
package stack;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
public class Config {
private String usuario;
private String db;
private String website;
private String path;
public void setConfigPorStream(boolean flag){
try{
InputStream is;
if(flag){
// stack.res/conf.txt
is = this.getClass().getResourceAsStream("res/conf.txt");
}
else{
//res/conf.txt
is = ClassLoader.getSystemResourceAsStream("res/conf.txt");
}
//O tamanho do arquivo provavelmente será o mesmo sempre
// e por isso BufferedReader é desnecessário.
byte[] b = new byte[1024];
is.read(b);
String config = new String(b,"UTF-8");
this.db = parseConfig(config, "dbUrl");
this.website = parseConfig(config, "website");
this.usuario = parseConfig(config, "usuario");
}catch(Exception e){
e.printStackTrace();
}
}
public void setConfig_PastadoJar(boolean flag) {
URL arquivo;
if(flag)
arquivo = ClassLoader.getSystemResource("res/conf.txt");
else{
arquivo = this.getClass().getResource("res/conf.txt");
}
FileInputStream fis = null;
try {
fis = new FileInputStream(arquivo.getFile());
byte[] b = new byte[1024];
fis.read(b);
String config = new String(b,"UTF-8");
this.db = parseConfig(config, "dbUrl");
this.website = parseConfig(config, "website");
this.usuario = parseConfig(config, "usuario");
} catch (Exception e) {
e.printStackTrace();
}
}
public String getUsuario(){
return this.usuario;
}
public String getDB(){
return this.db;
}
public String getWebsite(){
return this.website;
}
public static String parseConfig(String linha,String campo){
linha = linha.replaceAll("\"","");
int indice = linha.indexOf(campo) + 3 + campo.length();
int ultimo_char = linha.indexOf(",", indice)!=-1?linha.indexOf(",", indice):linha.indexOf("}", indice);
return linha.substring(indice,ultimo_char);
}
}
Stack. StackOverflow
package stack;
public class StackOverflow{
public static void main(String[] args) {
Config conf = new Config();
System.out.println("-----------------------\n\tPasta onde está o jar\n-----------------------\n");
conf.setConfig_PastadoJar(true);
printConfig(conf);
System.out.println("");
conf.setConfig_PastadoJar(false);
printConfig(conf);
System.out.println("-----------------------\n\tDentro do jar\n-----------------------\n");
conf.setConfigPorStream(false);
System.out.println("StackOverflow.jar\!res\conf.txt");
printConfig(conf);
System.out.println("");
System.out.println("StackOverflow.jar\!stack\res\conf.txt");
conf.setConfigPorStream(true);
printConfig(conf);
}
public static void printConfig(Config c){
System.out.println("Usuario: " + c.getUsuario());
System.out.println("DB: " + c.getDB());
System.out.println("Website: " + c.getWebsite());
}
}
res. conf.txt & & res.stack. conf.txt
{eg{usuario = "dbAdmin", dbUrl = "http://db.pt.stackoverflow.com", website = "pt.StackOverflow"}}
Note that in this example there will be no errors when running in an IDE (Netbeans / Eclipse) but the first two settings will not run directly if I start the Jar only i.e.
java -jar StackOverflow.jar
If you want to download the project, you can find it at GitHub