Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

jar and file.properties

Thread view: 
palmis - 09 Jan 2006 14:53 GMT
I have created my jar file.
The main get some configuration value from a EIF.properties. This file
is contained into jar archive, but when I run my application, I receive
this error:

java.io.FileNotFoundException: EIF.properties (The system cannot find
the file s
pecified)
       at java.io.FileInputStream.open(Native Method)
       at java.io.FileInputStream.<init>(FileInputStream.java:91)
       at java.io.FileInputStream.<init>(FileInputStream.java:54)
       at config.LoadProperties.load(LoadProperties.java:24)
       at snmpManager.GetClass.main(GetClass.java:73)

Why?

Thanks
nikita777@web.de - 09 Jan 2006 15:37 GMT
Hi,

it's difficult to say without the code.

1. Is the LoadProperties class in the jar? I think yes
2. is the properties-File in same package like the
LoadProperties-class?
3. You should use the getResourceAsStream-Method.

Similar to that:

public static void main(String[] args) {

InputStream in = this.getClass.getClassLoader("EIF.properties");

}

You can also use the getClass.getResourceAsStream, but if you use that,
the Loader will search the File in the Package (see also API for
Class.getResourceAsStream and Classloader.getResourceAsStream());

Greetings

Joe
palmis - 09 Jan 2006 16:04 GMT
Hi joe,

1. Yes

2. No,properties file is out of package of LoadProperties-class; but if
it run with eclipse, it function correctly!

3.this is code of LoadProperties-class

package config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import snmpManager.GetClass;
import udp.SenderUdp;

public class LoadProperties {

    public LoadProperties(){

    }

    public void load(){
        Properties myProp = new Properties();
       // load key-value pairs from a file
       FileInputStream fis;
        try {
            fis = new FileInputStream("EIF.properties");
            myProp.load(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

       // Getting values from the loaded key-value pairs is done
       // by calling getProperty on the Properties Object
       String myPortAgent = "PORT_NUMBER_AGENT";
       String myIpAddressAgent = "IPADDRESS_AGENT";
       String myMibDirectoryAgent = "MIB_DIRECTORY";

       String myPortUdp = "PORT_NUMBER_CCFH1";
       String myIpAddressUdp = "IPADDRESS_CCFH1";
       // Use this key to get the corres. value as a **String**
       // if the key does not exist, myValue will be null
       String myPortValue = myProp.getProperty(myPortAgent);
       String myIpValue = myProp.getProperty(myIpAddressAgent);
       String myMibValue = myProp.getProperty(myMibDirectoryAgent);
       GetClass.setPort(myPortValue);
       GetClass.setHost(myIpValue);
       GetClass.setMib(myMibValue);

       String myPortUdpValue = myProp.getProperty(myPortUdp);
       String myIpUdpValue = myProp.getProperty(myIpAddressUdp);
       SenderUdp.setPort(myPortUdpValue);
       SenderUdp.setHost(myIpUdpValue);
       
     

    }
}

what do you think?

thanks
Thomas Fritsch - 09 Jan 2006 19:47 GMT
> 1. Yes
>
> 2. No,properties file is out of package of LoadProperties-class; but if
> it run with eclipse, it function correctly!
>
> 3.this is code of LoadProperties-class

[...]

> public void load(){
>     Properties myProp = new Properties();
[quoted text clipped - 8 lines]
>        e.printStackTrace();
>     }
You can't use FileInputStream, because that is usable only for plain files,
not for members inside a jar. (It works in eclipse, because eclipse loads
from the plain files, not from your jar file.)
As nikita777 (Joe) already said, use getResourceAsStream:
     InputStream is;
     try {
         is = getClass().getResourceAsStream("/EIF.properties");
         myProp.load(is);
     } ...
Note especially the "/" in the path. You need it, because as you say your
properties file is out of any package in your jar file (i.e. it is in the
root directory inside the jar). See also
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#getResourceAsStream
(java.lang.String
)>

[...]

> what do you think?

Signature

"TFritsch$t-online:de".replace(':','.').replace('$','@')



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.