1

I'm developing a java swing application and it works fine, but after building my app , when I run the jar file , my app doesn't works as I want. so to know what's the problem I used this test :

FileReader reader;
Properties props;
    try{
         reader = new FileReader("src\\inputs.properties");
         props = new Properties();
         props.load(reader2);

         JOptionPane.showMessageDialog(null,reader.getClass());
  }catch(Exception e){
       JOptionPane.showMessageDialog(null,e.getMessage());
  }

so when i run the app it works fine and i get this message : message before building the app

And that means that my properties file is loaded.

But when I build the app, when I run it I get this message :

message after building the app

My problem is how to make my properties file works after building may app?

I'm using netbeans and this is my project structure :

-source Package
--default package
-- inputs.properties
--myapppackage
--myapppackage.java

thanks in advance.

James
  • 1,190
  • 5
  • 27
  • 52
  • Can you please post error messages as text instead of links to screenshots? – khelwood Jan 01 '16 at 14:34
  • yes , this the first message : class java.io.FileReader second message src\inputs.properties (The system cannot find the Path specified) – James Jan 01 '16 at 14:42
  • Possible duplicate of [How to use Java property files?](http://stackoverflow.com/questions/1318347/how-to-use-java-property-files) – Ravindra babu Jan 02 '16 at 15:04

2 Answers2

1

Try to use this to load the file of properties:

        File file = new File("inputs.properties);//use the right path
        FileInputStream fileInput = new FileInputStream(file);
        Properties properties = new Properties();
        properties.load(fileInput);
        fileInput.close();
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
  • Hey , abdelhak, thanks for your response , i change the path to "inputs.properties" but now get this message : The system cannot find the path specified. before and after building the app. – James Jan 01 '16 at 14:36
  • @Ali you the message in both cases? – Abdelhak Jan 01 '16 at 14:38
  • @Ali take alook at the answer above – Abdelhak Jan 01 '16 at 14:46
  • thanks Abdelhak for your response and your time , but now im sure that i have a problem with the right path to my properties file. so how to load the file, File file = new File("path"); i put the properties file in my sources package, and i want my properties file be included in main jar of my app, because i want to run the app in other machine. thanks – James Jan 01 '16 at 14:57
1

Please create a folder "config" in your project and place the inputs.properties into it.

 reader = new FileReader("config/inputs.properties");

for details, you can also go through this thread: Netbeans FileReader FileNotFound Exception when the file is in folder?

Or: When your resourse is in JAR file, it's not a File anymore. A File is only a physical file on the filesystem. Solution: use getResourceAsStream. Something like this:

try {

        Properties props;
        Property p=new Property();
        InputStreamReader  in=new InputStreamReader(p.getClass().getResourceAsStream("/config/" + "inputs.properties"));
        props = new Properties();

        props.load(in);

        JOptionPane.showMessageDialog(null, in.getClass());
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage());
    }
Community
  • 1
  • 1
Abdul Razak
  • 253
  • 2
  • 3
  • 14