Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reading from property file

 
Aleksandar Stojanovic
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys!

I sincerely hope that someone can help me. I am trying to use property file and my code looks like

package test.alex;

import java.util.*;
import java.io.*;

public class Main {

public Main() {

try{

Properties prop = new Properties();
File f = new File(getClass().getResource("test.properties").toURI());
FileInputStream inS = new FileInputStream(f);
prop.load(inS);
System.out.println(prop.getProperty("user"));

}
catch(Exception exp){
exp.printStackTrace();
}
}

public static void main(String[] args) {
Main m = new Main();
}

}

Property file is located in test.alex package. After compilation I have got test.jar with structure

META-INF/
META-INF/MANIFEST.MF
test/
test/alex/
test/alex/Main.class
test/alex/test.properties

and when I run jar like java �cp test.jar test.alex.Main I am getting next Exception

java.lang.IllegalArgumentException: URI is not hierarchical
at Java.io.File.<init>(Unkonw source)


Whould you be kind to excplain me where i am doing wrong

Thanks in advance
 
Aleksandar Stojanovic
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello folks!

Finally I found solution (thanks to sources on the net) , everything that I have done is to change from

try{

Properties prop = new Properties();
File f = new File(getClass().getResource("test.properties").toURI());
FileInputStream inS = new FileInputStream(f);
prop.load(inS);
System.out.println(prop.getProperty("user"));

}

to

try{

Properties prop = new Properties();
InputStream inS = Main.class.getResourceAsStream("test.properties");
prop.load(inS);
System.out.println(prop.getProperty("user"));

}

I would like to say thanks to all good people who try to find solution.
 
no wonder he is so sad, he hasn't seen this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!