简体   繁体   中英

Properties added to .jar not found

I have a simple application of desktop. The aplication has a .jar, called MyApp.jar, of other aplication and in this jar there is a properties file with one key(infor.properties) . The aplication must generate a own MyApp.jar where the key of the properties file is the String in the inputText, so I must change que properties file. My problem is when I generate the new .jar, this jar doesn't find the properties file. I use this for get the content of infor.properties:

InputStream in = this.getClass().getClassLoader().getResourceAsStream("infor.properties");
Properties prop = new Properties();
prop.load(in);
in.close();

I use ZipFile and ZipOutputStream for copy every files to the new .jar, and add a new. The new structure of files the new .jar is correct, but when I run the new jar, doesn't find the file infor.properties. Does anyone know why?

It depends where is your file in the jar and how do you specify your class path. Unfortunately you did not give us much info on your structure etc. So I have created this sample which you hopefully can apply to yours project:

Lets say you have class Test in package testapp (full name of class is there fore testapp.Test ):

package testapp;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test {

    private static final Logger logger = Logger.getLogger(Test.class.getName());

    public static void main(String[] args) {

            Properties properties = new Properties();
            try {
                properties.load(new FileInputStream("testapp/infor.properties"));
                System.out.println("" + properties.getProperty("a"));
            } catch (IOException e) {
                logger.log(Level.SEVERE, "", e);
            }

    }

}

You are in some directory PROJECT_ROOT. Now in this directory you have the above compiled class (in directory testapp, because your full name of the class is testapp.Test ). Your structure looks like this:

PROJECT_ROOT
  testapp
    Test.class
    infor.properties

Note, that the class is already compiled (you probably know how to do that right?). When your working dir is PROJECT_ROOT you can run:

 jar -vcf myjar.jar *

which will create a jar named myjar.jar . Now you can run the jar file using the following command

java -cp myjar.jar testapp.Test

which simply says (using -cp myjar.jar ) that the class path is myjar.jar (for JVM is the jar like directory).

As you could see in the Test.java you have "testapp/infor.properties" . The path to the property file must be specified in regard to used class path. The class path is the root of myjar.jar content. Therefore testapp/infor.properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM