• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reading from property file

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic