jar and file.properties

P

palmis

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
 
N

nikita777

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
 
P

palmis

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
 
T

Thomas Fritsch

palmis said:
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();
// 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();
}
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
[...]

what do you think?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,998
Messages
2,570,242
Members
46,835
Latest member
lila30

Latest Threads

Top